I want to make a table, and then for each 6 rows should have a tr, and then the rows are inside td.
So example:
<tr>
<td><?php echo $show[id]; ?></td> // 1
<td><?php echo $show[id]; ?></td> // 2
<td><?php echo $show[id]; ?></td> // 3
<td><?php echo $show[id]; ?></td> // 4
<td><?php echo $show[id]; ?></td> // 5
<td><?php echo $show[id]; ?></td> // 6
</tr> <tr> // start new tr after 6 rows
...repeat the tds
How can i do something like this?
I have tried myself doing
<tr>
<?php
while ($show == mysql_fetch_array($query)){ ?>
<td><?php echo $show[id]; ?></td>
<?php } ?>
</tr>
But as you can see this just inserts everything in one tr..
Thank you
<tr>
<?php
$c = 0; // Our counter
$n = 6; // Each Nth iteration would be a new table row
while ($show = mysql_fetch_array($query))
{
if($c % $n == 0 && $c != 0) // If $c is divisible by $n...
{
// New table row
echo '</tr><tr>';
}
$c++;
?>
<td><?php echo $show[id]; ?></td>
<?php
} ?>
</tr>
Related links:
Modulus operator (%) on php.net
Count the number of lines if the modulo 6 is null then echo the </tr><tr>
<tr>
<?php
$i=0;
while ($show == mysql_fetch_array($query)){ ?>
<td><?php echo $show[id]; ?></td>
<?php if(++$i%6 == 0) echo '</tr><tr>'; ?>
<?php } ?>
</tr>
You can do:
<tr>
<?php
$count = 0;
while ($show = mysql_fetch_array($query)){
if($count == 6) {
$count = 0;
echo "</tr> <tr>";
}
echo "<td>".$show[id]."</td>";
$count++;
}
</tr>
You’re accidentally using the comparison operator == instead of an assignment operator =.
And to always put six cells into each row, I’d do this:
$perRow = 6;
$counter = 0;
echo '<tr>';
while ($show = mysql_fetch_array($query)) {
if ($counter % $perRow === 0 && $counter !== 0) {
echo '</tr><tr>';
}
echo '<td>', $show['id'], '</td>';
$counter++;
}
while ($counter++ % $perRow !== 0) {
echo '<td></td>';
}
echo '</tr>';
This will ensure that each row is properly filled with six cells.
<?php
$countRows = 0;
while ($show == mysql_fetch_array($query)){
if($countRows == 0) echo '<tr>';
?>
<td><?php echo $show[id]; ?></td>
<?php
$countRows++;
if($countRows == 6){
$countRows = 0;
echo '</tr>';
}
?>
<?php } ?>
<?php if($countRows < 6) echo '</tr>'; ?>
Related
I have an array which is populated from a MySQL query:
if ($result) {
$foundResult = true;
foreach ($result as $row) {
array_push($searchResultAccount,$row->Account);
array_push($searchResultUsername,$row->Username);
array_push($searchResultPassword,$row->Password);
array_push($searchResultCreated,$row->Created);
array_push($searchResultStrength,$row->Strength);
}
} else {
array_push($error,"No results found");
}
Further down in my page, I want to create a table and populate the bootstrap table with the results from the array. However, each cell in the table prints all sets of results for that given field. I suspect I have a problem with my loop logic. I tried changing from a foreach to a for loop but still no luck:
<tbody>
<?php for ($x = 0; $x < sizeof($result); $x++) { ?>
<tr>
<td><?php for ($i = 0; $i < sizeof($searchResultAccount); $i++){echo($searchResultAccount[$i]);}?></td>
<td><?php for ($i = 0; $i < sizeof($searchResultUsername); $i++){echo($searchResultUsername[$i]);}?></td>
<td><?php for ($i = 0; $i < sizeof($searchResultPassword); $i++){echo($searchResultPassword[$i]);}?></td>
<td><?php for ($i = 0; $i < sizeof($searchResultCreated); $i++){echo($searchResultCreated[$i]);}?></td>
<td><p>ExpiresHere</p></td>
<td><?php for ($i = 0; $i < sizeof($searchResultStrength); $i++){echo($searchResultStrength[$i]);}?></td>
</tr>
<?php } ?>
</tbody>
The outcome looks like below:
I have 2 results in the array, and as you can see they're printing together in each cell. Any ideas on where I've went wrong ?
Edit: Full PHP code as from the query string below:
$sql = "SELECT * FROM Accounts WHERE User_ID = :userid AND Account = :account";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':userid',$userid);
$stmt->bindValue(':account',$account);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_CLASS);
if($result) {
$foundResult = true;
foreach($result as $row) {
array_push($searchResultAccount,$row->Account);
array_push($searchResultUsername,$row->Username);
array_push($searchResultPassword,$row->Password);
array_push($searchResultCreated,$row->Created);
array_push($searchResultStrength,$row->Strength);
}
} else {
array_push($error,"No results found");
}
Work like this:
foreach($result as $row) {
?>
<tr>
<td><?php echo $row->Account; ?></td>
<td><?php echo $row->Username; ?></td>
<td><?php echo $row->Password; ?></td>
<td><?php echo $row->Created; ?></td>
<td><?php echo $row->Strength; ?></td>
</tr>
<?php
}
with this approach you create table row for each result row
You have to remove your for loops inside of td elements
foreach($result as $row) {
<tr>
<td><?php echo $row->Account;?></td>
<td><?php echo $row->Username;?></td>
<td><?php echo $row->Password;?></td>
<td><?php echo $row->Created;?></td>
<td><?php echo $row->Strength;?></td>
</tr>
}
The problem is occurring because of lines like this.
for($i = 0; $i < sizeof($searchResultAccount); $i++){echo($searchResultAccount[$i]);}
I'm "guessing" that the problem is occurring because this is the length of the rows you want to print, not the count of data you want the print on the table.
Try changing the rows from:
<?php for($i = 0; $i < sizeof($searchResultAccount); $i++){echo($searchResultAccount[$i]);}?>
into
<td><?php echo($searchResultAccount[$x]) ; } ?></td>
You are now looping trough each result in each tablerow (tr). With this code, you are only focussing the result that belongs to the tr.
<tbody>
<?php for($x = 0; $x < sizeof($result); $x++) { ?>
<tr>
<td><?php echo($searchResultAccount[$x]);?></td>
<td><?php echo($searchResultUsername[$x]);?></td>
<td><?php echo($searchResultPassword[$x]);?></td>
<td><?php echo($searchResultCreated[$x]);?></td>
<td><p>ExpiresHere</p></td>
<td><?php echo($searchResultStrength[$x]);?></td>
</tr>
<?php } ?>
</tbody>
Glad you are here. I need solution and I am kinda newbie in these things.
Right now I have page looking like this
1.text 2.text 3.text 4.text
but I need it to be like this
1.text 2.text
3.text 4.text
Code I have
<table >
<?php
for ($x = 1; $x <= 2; $x++): ?>
<tr>
<?php while($row = $choices->fetch_assoc()): ?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php endwhile; ?>
</tr>
<?php endfor; ?>
</table>
The for() loop is useless in this, because the while() loop inside it will process every row returned by the query.
You should just use the while loop, and then use a counter to tell whether to start a new <tr>.
<table>
<?php
$counter = 0;
while ($row = $choices->fetch_assoc()) {
if ($counter % 2 == 0) { // Start a new row before event elements
echo "<tr>";
}
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if ($counter % 2 == 1) { // End row after odd elements
echo "</tr>";
}
$counter++;
}
if ($counter % 2 == 1) { // End last row if it only had 1 column
echo "</tr>";
}
?>
</table>
<table >
<?php while($row = $choices->fetch_assoc()):
if(($row['id_var'] % 2) == 1) echo '<tr>';
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if(($row['id_var'] % 2) == 0) echo '</tr>';
endwhile; ?>
</table>
I want to recapitulate report that grouped by month. But the results of sum is incorrect. It's just sum data in one month and show in all rows.
<?php
$query = mysql_query("SELECT * FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php
$Masuk= mysql_query("SELECT SUM(barang_in) AS masuk FROM barang GROUP BY arsip");
if($Masuk){
$data = mysql_fetch_assoc($Masuk);
echo $data['masuk'];
}
?></td>
<td><?php
$keluar= mysql_query("SELECT SUM(bijih_out+htm_out+pth_out) AS keluar FROM barang GROUP BY arsip");
if($keluar){
$data = mysql_fetch_assoc($keluar);
echo $data['keluar'];
}?></td>
<td><?php
$efisiensi= mysql_query("SELECT SUM((bijih_out+htm_out+pth_out)-barang_in) AS efisiensi FROM barang GROUP BY arsip");
if($efisiensi){
$data = mysql_fetch_assoc($efisiensi);
echo $data['efisiensi'];
}
?></td>
<td><?php
$persen= mysql_query("SELECT SUM(barang_in/(bijih_out+htm_out+pth_out))*1 AS persen FROM barang GROUP BY arsip");
$simbol = "%";
if($persen){
$data = mysql_fetch_assoc($persen);
echo number_format($data['persen'],2); echo $simbol;
}
?></td>
<?php
$i++;
}
?>
so that's my code. what's wrong with that? There are some columns for displaying income items, outcome items, efficiency ( difference of outcome and income items ) and percent of efficiency.
finally I found out the right code. Thankyou for #RandomSeed who helps me. This is my final code and it works!
<?php
$query = mysql_query("SELECT arsip, SUM(barang_in) AS income, SUM(bijih_out+htm_out+pth_out) AS outcome, SUM((bijih_out+htm_out+pth_out
)-barang_in) AS efficiency, SUM((barang_in / ( bijih_out + htm_out + pth_out))*100) AS percent FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php echo $data['income']; ?></td>
<td><?php echo $data['outcome']; ?></td>
<td><?php echo $data['efficiency']; ?></td>
<td><?php $simbol = "%"; echo number_format($data['percent'],0); echo $simbol; ?></td>
<?php
$i++;
}
?>
After submitting a mysqli_query to select open/unfulfilled orders from a cart database the results is output to the screen with a standard WHILE LOOP.
while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}
orders-open.php is simply the internal part of a TABLE:
<tr style="color:#FFF;">
<td><?php echo $row['Buyer']; ?></td>
<td><?php echo $row['Cart_Date']; ?></td>
<td><?php echo $row['Item_Number']; ?></td>
<td><?php echo $row['Item_Title']; ?></td>
<td><?php echo $row['Item_FPrice']; ?></td>
<td><?php echo $row['Item_Qty']; ?></td>
</tr>
So here is my question, I want to apply a simple HTML "<HR>" tag between the records at the point where the value in $row['Buyer'] changes, so by example:
John Doe 9/11/13 123456 Item 1 $5.99 5
John Doe 9/11/13 123654 Item 2 $8.99 3
John Doe 9/9/13 321456 Item 3 $4.99 2
(HR - Horizontal Rule Tag here)
Mike Doe 9/7/13 123555 Item 1 $9.99 2
Mike Doe 9/7/13 123777 Item 2 $2.99 6
What would be the best way to write the conditional statement inside the WHILE LOOP to compare the $row[Buyer'] result to the previous $row['Buyer'] result?
$first_run = TRUE;
$previous_buyer = NULL;
while($row = mysqli_fetch_array($oresult)) {
if($first_run) {
$first_run = FALSE;
$previous_buyer = $row['Buyer'];
}
include('orders-open.php');
}
And then in your include file:
<?php if($previous_buyer != $row['Buyer']) {
echo '<HR width="100%">';
$previous_buyer = $row['Buyer'];
} ?>
<tr style="color:#FFF;">
<td><?php echo $row['Buyer']; ?></td>
<td><?php echo $row['Cart_Date']; ?></td>
<td><?php echo $row['Item_Number']; ?></td>
<td><?php echo $row['Item_Title']; ?></td>
<td><?php echo $row['Item_FPrice']; ?></td>
<td><?php echo $row['Item_Qty']; ?></td>
</tr>
$prev_buyer = '';
while($row = mysqli_fetch_array($oresult))
{
if($prev_buyer !== $row['Buyer'])
{
//Do Something
}
include('orders-open.php');
$prev_buyer = $row['Buyer']
}
Just as Elon said above - change this:
while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}
to
$old_buyer = null;
while($row = mysqli_fetch_array($oresult)) {
if ($row['Buyer'] != $old_buyer) {
echo '<hr>';
}
$old_buyer = $row['Buyer'];
include('orders-open.php');
}
You could do it like this:
$buyer = null ; //Cache buyer in a variable, because $row is reset in the loop.
while($row = mysqli_fetch_array($oresult)) {
include('orders-open.php');
if ($buyer !== $row["Buyer"]){
echo ($buyer !== null) ? "<hr/>" : "" ;
$buyer = $row["Buyer"] ;
}
}
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.