I need to select some data from mysql and echo them into a table,
I have 20 entries which I want to echo them into 5by4 table I can select them like this:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr> <td class="auto-style3">
<?php
while($row = $result->fetch_assoc()) {
?>
<?php echo "<br>". $row["player"].""; ?>
<?php
}}
?>
</td> </tr> </table>
it gives me something like this:
but I want it like this:
Can anyone help?
try it like this, using $count to count your item in array. and after 5 items have been echoed, then you put <tr> to echo in new row
<?php
$count=0;
echo "<table>";
while($row = $result->fetch_assoc()) {
if($count==0) {
echo "<tr>";
}
$count++;
echo "<td>".$row["player"]."</td>";
if($count==5) {
echo "</tr>";
$count=0;
}
}
echo "</table>";
?>
I changed your code to add a counter and close the tag every five records, this is the result:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr>
<?php
$counter=0;
while($row = $result->fetch_assoc()) {
if($counter == 4){
echo '</tr><tr>';
$counter=0;
}
?>
<td><?php echo $row["player"].""; ?></td>
<?php
$counter++;
}}
?>
</tr> </table>
Related
I have a table 'tblexam' which contains State,city,candidate.I want to fetch the data from this table using where clause.I am able to fetch the data but i want to add the sum of Candidate at the last row(As Shown In Picture) how can i do that .
$sql="SELECT *
FROM tblexam
WHERE state='UP'";
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result) {
$cnt=$cnt+1; ?>
<tr class="odd gradeX">
<td class="center"><?php echo htmlentities($cnt);?></td>
<td class="left"align="left"><?php echo htmlentities($result->state);?></td>
<td class="center" align="left"><?php echo htmlentities($result->city);?></td>
<td class="center"align="left"><?php echo htmlentities($result->candidate);?></td>
<?php } ?>
<td class="center"align="left"><?php echo htmlentities($result->Total);?></td>
</tbody>
</table>
Add total while iterating through rows and display it outside the loop, Sample code is given below
$sql = "SELECT * FROM tblexam WHERE city='UP'";
$result = $conn->query($sql);
$numRows = $result->num_rows;
if ($numRows> 0) {
$total = 0;
echo '<table border="1" cellpadding="5" cellspacing="0">';
echo '<tr>';
echo '<th>state</th>';
echo '<th>city</th>';
echo '<th>candidate</th>';
echo '</tr>';
while($row = $result->fetch_assoc()) {
$total+=$row['candidate'];
echo '<tr>';
echo '<td>'.$row['city'].'</td>';
echo '<td>'.$row['state'].'</td>';
echo '<td>'.$row['candidate'].'</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td>Total</td>';
echo '<td> </td>';
echo '<td>'.$total.'</td>';
echo '</tr>';
echo '<table>';
}
Before you start looping the data you can create a variable which you set to 0, inside the loop you can add the result's total to this variable, after the loop, the variable will contain the grand total:
$total = 0;
foreach($results as $result) {
$total += (int)$result->Total;
...
}
// $total = 1425
I want to display data after in static table just like an "Add to Cart".
<table>
<th>Item</th> <th>Description</th>
<tr>
<td>Monitor<td>
<td>Monitor Blue<td>
</tr>
<tr>
<td>Mouse<td>
<td>Mouse Wireless<td>
</tr>
<tr>
<td>Keyboard<td>
<td>Keyboard<td>
</tr>
My search form : This is no problem is searching item. Only in the add button every row. So when you search for item you have choice if you want to add in static table.
Search:
Mysql when search
<?
$sql = "select * from item_tb where name='".$search."'";
$result=$conn->query($sql);
echo "
<table cellspacing='20' border='0' ><tr> <th>Item</th><th>Description</th>
</tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "
<tr> <td>
".$row["name"]."</td> <td>
".$row["description"]."</td>";
echo" <form name='add' method='POST'>";
echo" <td><a href='item.php?id=".$row['Iid']."'><input type='submit' value='Add'></td></a>";
echo" </form>";
}
}
?>
Here is my main problem. i don't how to make table where i'll get the data i have search and will display as many as item i add. I'm really down in this.
<?
if($id != "")
{
$sql = "SELECT * FROM item_tb WHERE Iid='".$id."'";
$result=$conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo" ".$row["name"]." ";
}
}
}
?>
<?php
if($id != "") {
$sql = "SELECT * FROM item_tb WHERE Iid='" . $id . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?= $row['name'] ?></td>
</tr>
<?php
}
}
}
?>
I am using an HTML table to display data from a MySQL table using PHP. I need it so once the table has 10 columns, it will move on to the next row.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
while($row = mysqli_fetch_array($result))
{
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
How can this be done?
Untested but something like this should work or get you started in a good direction:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
$x=0;
while($row = mysqli_fetch_array($result))
{
if($x==0){
echo "<tr>\n";
}elseif($x%10){
echo"</tr><tr>\n";
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
$x++;
}
echo "</tr></table>";
mysqli_close($con);
?>
Add a counter to your loop starting at one.
Each time through the loop if the remainder after dividing the counter value by 10 is 1
add the <tr>. If the remainder is 0 then add a </tr> Then after the loop a </tr> if the remainder is not evenly divisible by 10.
<?php
echo '<table width="100%" border="1px"><tr width="100%">';
$i = 0;
while($row = mysqli_fetch_array($result))
{
$i++;
?>
<?php if ($i%10 ==1): ?><tr><?php endif; ?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php if ($i%10 ==0): ?></tr><?php endif; ?>
<?php
}
if ($i%10 != 0) echo "</tr>";
echo "</tr></table>";
Using modulo (%)
After each 10th cell, if a new cell is added, the current row is closed and a new row is opened first, before outputting the cell.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = 0;
while($row = mysqli_fetch_array($result))
{
if ($cell++ % 10 == 0 && $cell > 1)
{
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
The extra condition && $cell > 1 seems to be a little odd, but without it, you will get an empty row to start with. Eliminating it by putting ++ before $cell will cause the first row to be 9 cells instead of 10. Putting $cell > 0 && in front of the modulo will cause cell never to be incremented, because the first part of the expression is always false. Moving the if to execute it after outputting the cell, would cause the risk of ending with an empty row. It could be solved using a do..while loop, but you'd have to check up front if you have one row at least.
Long story short: use the code above. :)
Using a simple counter and reset it after each row
I think it's even more readable without the modulo, though you'd have to initialize $cell to -1 to prevent the first row to be 9 cells. Nevertheless, I think this is cleaner:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = -1;
while($row = mysqli_fetch_array($result))
{
if (++$cell == 10)
{
$cell = 0;
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
<table>
<tr>
<?php
$endRow = 0;
$columns = 10; // number of columns
$hloopRow1 = 0;
do {
if($endRow == 0 && $hloopRow1++ != 0) echo "<tr>";
?>
<td>
<?php echo $row['Name']; ?>
</td>
<?php $endRow++; if($endRow >= $columns) { ?>
</tr>
<?php $endRow = 0; }
} while ($row = mysql_fetch_assoc($result));
if($endRow != 0) {
while ($endRow < $columns) {
echo("<td> </td>");
$endRow++;
}
echo("</tr>");
}
?>
</table>
This should work fine. Hope this helps.
I have several MySQL tables that are dynamically generated into a html table one table at a time through the code below. However, the tables don't have the same columns. i.e. One table has a description column, whereas the other does not.
Is the following code the best way to have all the possible MySQL columns among the various tables in the script but only show the MySQL columns that exist for the selected table? I feel like I'm redundant by writing "isset" for every column. Thanks!
<?php
$query = " SELECT * FROM $tablename ";
$query_select = mysqli_query($con,$query);
while($row = mysqli_fetch_array($query_select)) {
?>
<table>
<tr>
<?php if(isset($row['name'])){ ?>
<td><?php echo $row['name'];?></td>
<?php } ?>
<?php if(isset($row['description'])){ ?>
<td><?php echo $row['description']?></td>
<?php } ?>
</tr>
</table>
You might want to make your code adapt to the fields in the result set:
<?php
$result = mysqli_query($con,$query);
$fields = mysqli_fetch_fields($result);
$myaliases = array(
'column_id' => 'id'
);
?>
<table>
<tr>
<?php foreach ($fields as $field): ?>
<th><?php echo $myaliases[$field->name] ?: $field->name; ?></th>
<?php endforeach; ?>
</tr>
<?php while($row = mysqli_fetch_array($result)): ?>
<tr>
<?php foreach ($fields as $field): ?>
<td><?php echo $row[$field->name]; ?></td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
Re comments:
I've added code above to print a table row for column headings.
I've also included an example of mapping a field name column_id to a table heading id in the output. If I define no alias for a given column, it defaults to the original field name by using the PHP 5.3 operator ?:
You could alternatively define column aliases in your SQL query like SELECT column_id AS id ...
See http://www.php.net/manual/en/mysqli-result.fetch-fields.php
You can foreach the array instead.
<?php
$query = " SELECT * FROM $tablename ";
$query_select = mysqli_query($con,$query);
?>
<table>
<?php while($row = mysqli_fetch_array($query_select, MYSQLI_ASSOC)) { ?>
<tr>
<?php foreach($row as $key => $value) { ?>
<td><?=$value?></td>
<?php } //Endforeach ?>
</tr>
<?php } //Endwhile ?>
</table>
If you need to print labels you can also use an associative array and an additional iteration to do that as well.
<?php
$query = " SELECT * FROM $tablename ";
$keys = array('name' => 'Name Label', 'description' => 'Description Label');
$query_select = mysqli_query($con,$query);
$i = 0;
?>
<table>
<?php while($row = mysqli_fetch_array($query_select, MYSQLI_ASSOC)) { ?>
<?php if($i == 0) { ?>
<tr>
<?php foreach($row as $key => $value) { ?>
<td><b><?=$keys[$key]?></b></td>
<?php } //Endforeach ?>
</tr>
<?php } $i++; //Endif ?>
<tr>
<?php foreach($row as $key => $value) { ?>
<td><?=$value?></td>
<?php } //Endforeach ?>
</tr>
<?php } //Endwhile ?>
</table>
You could just loop through the results. However, that would not perform any checks. Most likely, you'll have to do something like this, depending on what you're actually getting from the database.
<?php foreach ($row as $field): ?>
<?php if ($field): ?>
<td><?php echo $field; ?></td>
<?php endif; ?>
<?php endforeach; ?>
Edit: In keeping in line with the comment you added above, you could simply remove the if clause.
not sure if this is what you need,
but you can use indexes instead for the column instead of names:
<?php
$query = " SELECT * FROM $tablename ";
$query_select = mysqli_query($con,$query);
while($row = mysqli_fetch_array($query_select)) {
echo $row[0] ." ".$row[1]." ".$row[2]
}
?>
mysql_fetch_array
possible formating:
<table>
<?php
$query = " SELECT * FROM $tablename ";
$query_select = mysqli_query($con,$query);
while($row = mysqli_fetch_array($query_select)) { ?>
<tr>
<?php for(var $i=0; $i < count($row); $i++){
echo "<td>". $row[i] ."</td>";
} ?>
</tr>
<?php } ?>
</table>
I've created a PHP program for adding and viewing reminders. The add page works, but I'm having some trouble displaying them properly. How should I code this to only get the actual data? Also, how could I put this into an HTML table? (i.e. column for name, description, and date; rows are the retrieved data)
Thanks
When I open the file in my browser I get this as an output:
Array ( [reminderID] => 14 [reminderName] => Test [reminderDescript] => Test_Descript [reminderDate] => 2012 05 7 )
Code:
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table align ="center">
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Date</td>
</tr>
</thead>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
?>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>
print_r() is a diagnostic tool for debugging, not to be used for real output. Instead, output HTML to a table using the array keys fetched from your row.
// Open a table
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Output each row
echo "<tr>
<td>{$row['reminderName']}</td>
<td>{$row['reminderDescript']}</td>
<td>{$row['reminderDate']}</td>
</tr>";
}
// Close the table
echo "</table>";
Better still, escape each of the values for HTML output with [htmlspecialchars()] (http://us3.php.net/manual/en/function.htmlspecialchars.php) before output to prevent cross-site scripting attacks and broken HTML if characters like < > & are encountered in the values.
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Encode all values for HTML output
$name = htmlspecialchars($row['reminderName']);
$desc = htmlspecialchars($row['reminderDescript']);
$date = htmlspecialchars($row['reminderDate']);
// Then output the encoded values
echo "<tr><td>$name</td><td>$desc</td><td>$date</td></tr>";
}
echo "</table>";
Change:
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
To:
while($row = mysql_fetch_assoc($result)) {
echo $row['reminderID'];
echo $row['reminderName'];
echo $row['reminderDescript'];
echo $row['reminderDate'];
}
You can echo those values out in whatever HTML you'd like. So, for example, if you want it in a table you would do something like this:
echo "<table><tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<td>" . $row['reminderID'] . "</td>";
echo "<td>" . $row['reminderName'] . "</td>";
echo "<td>" . $row['reminderDescript'] . "</td>";
echo "<td>" . $row['reminderDate'] . "</td>";
}
echo "</tr></table>";
You can clean that up a bit to take some (or all) of the HTML out of the PHP.
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table>
<thead><tr><td>id</td><td>name</td><td>description</td><td>date</td></tr></thead>
<tbody>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['reminderID']; ?></td>
<td><?php echo $row['reminderName']; ?></td>
<td><?php echo $row['reminderDescript']; ?></td>
<td><?php echo $row['reminderDate']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>