I grab data from my server and want them to display like this:
And instead, this happens:
I use PHP for the values:
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
Player Name
</td>
<td >
PK Level
</td>
<td>
Kills
</td>
<td>
Experience
</td>
</tr>
<tr>
<td >
<?php
$result = mysql_query("SELECT * FROM high ORDER BY Runecraftlvl DESC LIMIT 20");
while($row = mysql_fetch_array($result))
{
echo $row['Runecraftlvl'];
echo "<br />";
}
?>
</td>
<td>
<?php
$result = mysql_query("SELECT * FROM high ORDER BY Runecraftlvl DESC LIMIT 20");
while($row = mysql_fetch_array($result))
{
echo $row['Runecraftlvl'];
echo "<br />";
}
?>
</td>
<td>
<?php
$result = mysql_query("SELECT * FROM high ORDER BY Runecraftlvl DESC LIMIT 20");
while($row = mysql_fetch_array($result))
{
echo $row['Runecraftlvl'];
echo "<br />";
}
?>
</td>
<td>
<?php
$result = mysql_query("SELECT * FROM high ORDER BY Runecraftlvl DESC LIMIT 20");
while($row = mysql_fetch_array($result))
{
echo $row['Runecraftlvl'];
echo "<br />";
}
?>
</td>
</tr>
</table>
Why is this happening? How can I make the data go on separate rows, 4 data each row?
Before giving away any code, I'll give you a few hints.
Since you're not getting my point... You should add a counter yourself, and check whether or not you have to close the row.
But... Because you want 4 values in one cell, you will have to write them to temporary variables. You would need 4 variables: player_name, pk_level, kills and experience. When you've read out every 4th row of your query result, write them to the table.
($counter % 4) == 0 will be the main if-clause in your code. This will be true whenever the remainder of the division $counter / 4 is 0.
Also remember: you only need to perform your query once.
Give it a try first, and let us know what you come up with.
Related
I am fetching all the records from mysql but it split in to rows as shown in the image
I want that it display row wise means the column which are splitting in the column means "future of india" and GAURAV should come in line now row wise. When the body part full it auto split into new row. here is my code
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<table width="30%" border="0">
<tr>
<td><div id="rcorners2">'.$row["category"].' </div></td>
</tr>
</table>
Use <span> instead of<div> in the <td> and it will work
You need to move your loop.
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<table width="30%" border="0">
while($row = mysqli_fetch_assoc($result)) {
<tr>
<td>
<div id="rcorners2">'.$row["category"].' </div>
</td>
</tr>";
}
echo "</table>";
You should take table wrap all rows.
<?php
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<table width="30%" border="0">'
while($row = mysqli_fetch_assoc($result)) {
echo '
<tr>
<td><div id="rcorners2">'.$row["category"].' </div></td>
</tr>';
}
echo '</table>';
}
?>
If I understand correctly, I'll write pseudocode
<table>
<tr>
$query = "..."
while ($row = fetch()) {
<td>
print row data
</td>
}
</tr>
</table>
I have this table im trying to display users, being 2 users per 2 columns, then list down. Here is what i have so far:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'");
while($row = mysql_fetch_array($result)) { echo
" <table>
<tr>
<td width='85' align='left'><br><center>". $row['username'] . "</center>
</td>
<td align='right'><center></center>
</td>
</tr>
<td width='85' align='left'><center></center>
</td>
<td align='right'><center></center>
</td>
</table>";
} ?>
This just displays the members as rows going down, and missing out the other column completely. I was hoping that it would display the username in each of the table fields. I also did try putting ". $row['username'] ." in the other fields too, but that just duplicated it.
EDIT:
So iv'e changed it to this, I can't see going down as I only have 2 members, Would this work:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'"); ?>
<table>
<tr>
<?php while($row = mysql_fetch_array($result)) { echo
"<td width='85' align='left'><font size='1px'><center>". $row['username'] . "</font></center></td>
<td align='right'><center></center></td>";
} ?>
</tr>
</table>
example:
try something like this
<table>
<tr>
<th>name</th>
<th>name</th>
</tr>
<?php
$result = mysql_query("SELECT * from users WHERE adminlevel='5'");
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($i == '0') echo "<tr>";
echo "<td>{$row['username']}</td>";
if ($i == '1') echo "</tr>";
$i++;
if($i =='2')$i='0';
}
?>
</table>
I think you're asking why the other fields in your "user" mysql table aren't showing up.
They aren't there because you only asked for the username:
$row['username']
If you have first/last name in your mysql table, you can retrieve it in the same way:
$row['firstname']
$row['lastname']
In your code you got the row as a key/value array like this:
$row = mysql_fetch_array($result)
The "key" is the name of the mysql column, like username, lastname, firstname. And the value is what is stored in the mysql table under that row/column, like joecool, smith, joe.
I added pagination to a table a I created a while back and I have not ever been able to get it to work correctly since.
The table limit works, but that's it. If I select "First, Last or the page number" it just reloads the page, but the new page does not display.
If I set the page limit to a low number like 5 and select 'Last Page', when the page loads it shows =1 like it doesn't know there are other pages.
<?php
$con = mysqli_connect("localhost","root","","bfb");
$per_page=20;
if(isset($_POST["page"])) {
$page = $_POST["page"];
}
else {
$page = 1;
}
//Page will start from 0 and multiply per page
$start_from = ($page-1)*$per_page;
//Selecting the data from the table but with limit
$query = "SELECT * FROM orders LIMIT $start_from, $per_page";
$result = mysqli_query($con, $query);
?>
<table class="tableproduct">
<tr>
<th class="thproduct">Order ID</th>
<th class="thproduct">Customer Name</th>
<th class="thproduct">Product</th>
<th class="thproduct">Total Price</th>
<th class="thproduct"></th>
<th class="thproduct"></th>
</tr>
<?php
if( $result ){
while($row = mysqli_fetch_assoc($result)) :
?>
<form method="POST" action="orderhistory.php">
<tr>
<td class="tdproduct"><?php echo $row['order_id']; ?> </td>
<td class="tdproduct"><?php echo $row['customer_name']; ?> </td>
<td class="tdproduct"><?php echo $row['product_name']; ?> </td>
<td class="tdproduct"><?php echo $row['total_price']; ?> </td>
<td class="tdproduct"><a href='editorderhistory.php?id=<?php echo $row['id']; ?>'>EDIT</a></td>
<input type="hidden" name="product_id" value="<? echo $row['id']; ?>"/>
<td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
</tr>
</form>
<?php
endwhile;
}
?>
</table>
<?php
//Count the total records
if ($result != false) {
$total_records = mysqli_num_rows($result);
if ($total_records == 0) {
echo 'No results founds.';
}
}
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records /$per_page);
?>
<span class="spandarkblue">
<?php
//Going to first page
echo "<center><a href='orderhistory.php?page=1'>".'First Page'."</a> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='orderhistory.php?page=".$i."'>".$i."</a> ";
};
// Going to last page
echo "<a href='orderhistory.php?page=$total_pages'>".'Last Page'."</a></center>";
?>
Any ideas?
Found below issues in you code.
1) You are trying to get page value from URL using POST, where as you need to GET method to fetch values from URl. Using POST is returning null value, so $page value is always set to 1
So use $_GET["page"] instead of $_POST["page"]
2) You are preparing pagination by considering row count of the query which you are executing. But as you have added limits , your $total_records is always equals to $per_page value or less, resulting in only one page number.
Use the following code for generate pazination
$query1 = "SELECT count(*) as totalRecords FROM orders";
$result1 = mysqli_query($con, $query1);
if ($result1) {
$row1 = mysqli_fetch_assoc($result1);
$total_records = $row1['totalRecords'];
if ($total_records == 0) {
echo 'No results founds.';
}
}
Instead of below code
if ($result != false) {
$total_records = mysqli_num_rows($result);
if ($total_records == 0) {
echo 'No results founds.';
}
}
Hope it helps you.
You need to fetch all the data to know the total of your records, then you show only the desired data whithin a for() loop, so remove LIMIT $start, $per_page from your query, in this node you will always have 20 or less results
In your while() save the data in arrays like this:
$index = 0;
while($row = mysqli_fetch_assoc($result)) {
$ordID[$index] = $row["order_id"];
// The rest of your data...
$index++; // Increment the index
}
Then you chan show only the desired data:
for($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
echo $orderID[$i];
// And so on...
}
The min() function returns the lowest value between two vars, in this way in the last page, if you have 17 products instead of 20 you will not have errors.
<table>
<td>ID</td>
<td>Name</td>
<td>Date Added</td>
<tr>
<?php
$q = mysql_query("SELECT * FROM tab ORDER BY date_added DESC LIMIT 2");
while($d = mysql_fetch_array($q))
{
echo "<td>$d['id'];</td>";
echo "<td>$d['name'];</td>";
echo "<td>$d['date_added'];</td>";
echo "<tr>";
}
Example, I have 4 data.
I want to get 2 last row based on date_added DESC and display it in last row HTML table td.
Please help.
Try this
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Date Added</td>
</tr>
<?php
$q = mysql_query("SELECT id,name,date_added FROM table ORDER BY date_added DESC LIMIT 2");
while($d = mysql_fetch_array($q))
{
echo "<tr><td>".$d['id']."</td>";
echo "<td>".$d['name']."</td>";
echo "<td>".$d['date_added']."</td>";
echo "<tr>";
}
?>
</table>
Here is my code:
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><Center>Username:</Center><br /></td>
<td width="50%"><center>Numebr Of Warnings:</center><br /></td>
</tr>
</table>
<?
$query = mysql_query("SELECT * FROM warnings");
$numrows = mysql_num_rows($query);
if($numrows != "0"){
while($row = mysql_fetch_assoc($query)){
$warned = $row['username'];
$by = $row['by'];
$sql = mysql_query("SELECT * FROM warnings WHERE username='$warned'");
$warns = mysql_num_rows($sql);
?>
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><center><b><?php echo $warned; ?></b></center></td>
<td width="50%"><center><b><?php echo $warns; ?></center></b></td>
</tr>
</table>
<?php
}
}
else
echo "No Currently Warned Users";
?>
<hr />
And here is the result:
How can I make it so that instead of showing the 2 results for the user Mrg..... I just want it to show one result with the number or rows there are.
Help would be appreciated.
You can use DISTINCT in your query to avoid duplicate rows in your results:
SELECT DISTINCT username, `by` FROM warnings
Change the mysql_fetch_assoc with mysql_feth_array
You're getting the user with double record. Then for each user you are getting 2 lines
The best way to check it is to add pseudo-points to your code.
$query = mysql_query("SELECT * FROM warnings");
$arr=array();
while($row = mysql_fetch_assoc($query))
{
$arr[]=$row;
}
echo "<pre>";
print_r($arr);
echo "</pre>";
exit;
Here check if the $arr has double entries. Then change the mysql_fetch_assoc with mysql_fetch_array and try again
Here is the table
(by,id,username,date)
a1,1,u1,date
a2,2,u2,date
a3,3,u2,date
a4,4,u2,date
And here is the php code
<?php
$conn = mysqli_connect("localhost","username","password");
mysqli_select_db($conn,"dbname");
$warns = mysqli_query($conn,"select username, count(username) as warncount from dtest.warnings W group by username");
while($line = mysqli_fetch_array($warns))
{
echo $line['username']." has ".$line['warncount']. " warns <br/> ";
}
?>