How would I go about imposing a restriction on the number of HTML table columns when querying the database.
For example, I have a MySQL table with the following values:
myTable:
id color
1 red
2 blue
3 green
4 pink
5 purple
And when I run my query, instead of showing all rows in traditional table rows, e.g.
<table>
<?php
while {
echo "<tr><td>$row['color']</td></tr>;
}
?>
</table>
Instead, I would like to impose something where, every three td's, a new tr is created.
For example, it would output something like this:
<table>
<tr>
<td>red</td>
<td>blue</td>
<td>green</td>
</tr> <-- Notice how after 3 columns, a new table row is created.
<tr>
<td>pink</td>
<td>purple</td>
</tr>
</table>
Any way to achieve this?
In order to achieve this, you can use a combination of a counter and a modulus (%) operator:
<table>
<?php
$count = 0;
while($row = mysql_fetch_array($results)) {
$outputTr = ($count % 3) == 0;
if($outputTr) echo '<tr>';
echo '<td>' . $row['color'] . '</td>';
if($outputTr) echo '</tr>';
$count++;
}
?>
</table>
To achive this, put in a simple counter that resets every 3 table datas.
<?php
echo "<table><tr>";
$count = 0;
foreach($data as $key => $color)
{
if($count == 3)
{
$count = 0;
echo "</tr><tr>";
}
$count++;
echo "<td>".$color."</td>";
}
echo "</tr></table>";
?>
Related
I currently can print 8 records in one column, I am trying to print 16 records in 2 columns of 8 or 24 in 3 columns of 8. After several months of trying I have to admit it baffles me. Any help or guidance would be greatly appreciated. My code below gives me the first column of 8 perfectly, but after that I am stuck
$result1 = $db->prepare("SELECT * FROM table WHERE id='100' ORDER BY id ASC LIMIT 8 ");
$result1->execute();
print("");
$count = $result1->rowCount();
print("");
for($i=0; $row = $result1->fetch(); $i++) {
?>
<body topmargin="0">
<table border="0">
<tr>
<td>
<font size="5">ID:<?php echo $row['id']; ?></font><br>
<font size="5"><?php echo $row['field1']; ?></font><br>
<font size="5">Field1<?php echo $row['field2']; ?></font><br>
<font size="4">Field2<?php echo $row['field3']; ?></font><br>
<font size="3">Field3<?php echo $row['field4']; ?></font><br>
</td>
</tr>
<?php
}
?>
I would approach this by making a loop to output a table with a desired column count.
Within that you will need to iterate a number of rows, and then inside that loop, write out the table entry for each of the columns.
If your data doesn't exactly fill columns x rows then you'll get some blank items of course.
$rows = ceil($count / $columns)
for ($r = 0; $r < $rows; $r++) {
echo "<tr>";
for ($c = 0; $c < $columns; $c++) {
$value = $result1->getNext() ?? '';
echo "<td>{$value}</td>";
}
echo "</tr>";
}
The getNext() here is up to you of how best to implement. You could load all the values into an array and increment an index counter, or your $db class may give you a more helpful accessor for getting the next value.
I have following item list fetch from table, I need to add dynamic rowspan at the end of the row if item is from same supplier, but I have no idea how to work with this.
I tried:
foreach($items as $item){
/*get total num for rowspan*/
$group = $buyer->get_total_rowspan($obj->id, $obj->supplier);
echo '<tr>
<td>$item->id</td>
<td>$item->name</td>
<td>$item->supplier</td>';
if($group->countRow > 1){
<td rowspan="$group->countRow"><a>Manage</a></td>
}
if($group->countRow > 1){
echo '<td rowspan="'.$group->countRow.'"><a>manage</a></td>';
}else{
echo '<td><a>test</a></td>';
}
echo '</tr>';
}
but cell Manage will always appear at every row with mess format.
the idea results that I want:
You can try something like that:
$lastId = null;
foreach($items as $item){
/*get total num for rowspan*/
$group = $buyer->get_total_rowspan($obj->id, $obj->supplier);
echo '<tr>
<td>$item->id</td>
<td>$item->name</td>
<td>$item->supplier</td>';
if($lastId != $group->Id){
<td rowspan="$group->countRow"><a>Manage</a></td>
}
echo '</tr>'
$lastId = $group->Id;
}
Everytime there is a new group you can set the $i back to 0.
I have following PHP code contain 2 foreachs
echo "<table class='table table-bordered'>";
foreach($resultOld as $key=>$value)
{
foreach ($value as $key1 => $subjects) {
$checked = $subjects;
echo "<tr><tr class=\"".$subjects."\">$key1
<input type='checkbox' class=\"".$subjects."\" value='checked' name=\"".$key1."JAN\" $checked/> </tr>
</tr>" ;
}
}
echo "</table>";
$resultOld is a fetchAll(PDO::FETCH_ASSOC) output and it contains a two dimensional array. $subjects will return 661 words from the database which means the $resultOld array have 661 elements. And after every 12 cells I want to start a new line (tr). That means I need 55 rows in the table. How to achieve this using PHP?
If you want a new row after every 12 records, you need to use a counter and check the count -
$counter = 1;
echo "<table class='table table-bordered'>";
echo "<tr>"; //start the first row
foreach($resultOld as $key=>$value)
{
foreach ($value as $key1 => $subjects) {
// if the 13th cell, end last row, and start new row
if ($counter%12==1){
echo "</tr><tr>";}
$checked = $subjects;
echo "<td class=\"".$subjects."\">$key1
<input type='checkbox' class=\"".$subjects."\" value='checked' name=\"".$key1."JAN\" $checked/> </td>" ;
// increase counter
$counter++;
}
}
echo "</tr>"; // end last row
echo "</table>";
First, note that your HTML structure is wrong. I recommend thinking about this is a little more steps. Perhaps layout a single row <table> HTML structure for reference between slicing it up into PHP
Here's an example:
<table class="...">
<tbody> <!-- I recommend using the tbody tag -->
<tr>
<td>...</td>
</tr>
</tbody>
</table>
There's three basic things to think about:
The Table Body
The Row
The Columns (ie. Cells)
Step 1 is to print the table body. You're doing fine here:
echo '<table class="table table-bordered">';
echo '<tbody>';
//...
<echo '</tbody>';
echo '</table>';
Step 2 is to loop through your rows
echo '<table class="table table-bordered">';
echo '<tbody>';
// Loop Through Rows
foreach($resultOld as $key=>$value)
{
echo '<tr>'; // start a new row
// ...
echo '</tr>'; // end a row
}
<echo '</tbody>';
echo '</table>';
Step 3 is to loop through each column or cell of the table:
// STEP 1
echo '<table class="table table-bordered">';
echo '<tbody>';
// STEP 2
// Loop Through Rows
foreach($resultOld as $key=>$value)
{
echo '<tr>'; // start a new row
// STEP 3
foreach ($value as $key1 => $subjects) {
$checked = $subjects;
// Start a new column/cell
echo '<td class="' . $subjects . '">';
// Print cell contents
echo $key1;
echo '<input type="checkbox" class="' . $subjects . '" value="checked" name="' . $key1 . 'JAN" '. $checked . '/>';
// End column/cell
echo '</td>';
} // END STEP 3
echo '</tr>'; // end a row
} // END STEP 2
echo '</tbody>';
echo '</table>';
// END STEP 1
Some notes on your code:
You're printing 2 Table rows instead of printing a table row and a table column (ie: <tr><tr> instead of <tr><td>;
You need to print the row inside the first loop, not the second loop.
The second, nested, loop prints out your 2nd dimension or columns.
For me, I like to use single quotes (') with printing HTML. I do this because I use double quotes (") for HTML attributes. This allows me to avoid having to escape the double quote character and getting a hard to read '\""' situation, which can cause simple syntax bugs.
Also when running into problems printing HTML, braking the HTML into multiple echo/print statements can help you structure your code and troubleshoot the problems. Once it's working you can go back and refactor them into a single echo statement, however the performance difference would probably so minor that it's not worth the time.
I hope that helps!
I have a table that is read in from a postgresql database. I am having trouble adding checkboxes to a final row in the table so that I can then, using an add to basket function pass items to a basket.php file.
Firstly I need to add a checkbox to a final column on each row so that I can check against the items I want to add when I click add to cart. I am struggling massively with this. Any help would be greatly appreciated as my code is below. If you could explain what needs to be done that'd be awesome as I can learn from it.
<table border="1">
<tr>
<th>ref</th>
<th>title</th>
<th>platform</th>
<th>description</th>
<th>price</th>
<th>select</th>
</tr>
<?php $resource = pg_query ($connect, "select refnumber,title,platform,description,price from csgames");
while ($a = pg_fetch_array ($resource)) {
echo "<tr>";
for ($j = 0; $j < pg_num_fields ($resource); $j++) {
echo "<td>".$a[$j] ."</td>";
}
echo "</tr>";
}
?>
</table>
Try this
while ($a = pg_fetch_array ($resource)) {
echo "<tr>";
for ($j = 0; $j < pg_num_fields ($resource); $j++) {
echo "<td>".$a[$j] ."</td>";
}
echo '<input type="checkbox" name="items[]" value="'.$id.'" />'; //replace with item id
echo "</tr>";
}
Submitting the form will populate the items[] variable
$items = $_POST['items']; //array of itemid selected
output
---------
var_dump(items) = Array ( [items] => Array ( [0] => item123 [1] => item125 ) )
I'm trying to make a PHP photo gallery (yes, I've tried other solutions, none work with the setup I want) and I am trying to make this while loop stop after three times, because I want to show three thumbnails per table row. Here's my current code:
<table>
<tr>
<?php while($row2 = mysql_fetch_assoc($result)) { echo "
<td><a href='URL/".$row2['file']."'><img src='URL/".$row2['file']."' width='200' height='106' /></a></td>
"; } ?>
</tr>
</table>
I don't remember learning anything about stopping PHP loops, so please help! It'd be greatly appreciated.
So what you actually want is 3 images per row. I think this will work:
<table>
<?php
$i = 0;
while($row2 = mysql_fetch_assoc($result)) {
if($i%3==0)
echo "<tr>";
echo "<td><a href='URL/".$row2['file']."'><img src='URL/".$row2['file']."' width='200' height='106' /></a></td>";
if($i%3==2)
echo "</tr>";
$i++;
}
if($i%3!=0)
echo "</tr>";
?>
</table>
<table>
<tr>
<?php
$i=1;
while($row2 = mysql_fetch_assoc($result)) { echo "
<td><a href='URL/".$row2['file']."'><img src='URL/".$row2['file']."' width='200' height='106' /></a></td>
";
if($i>=3) break;
$i++;
} ?>
</tr>
</table>
try adding an AND to the while and compare to a counter
while($row2=mysql_fetch_assoc($result) && $i < 3)
set $i to 0 before the loop
why not limit the results returned in the original MySQL query? you can add limit to your Query like this:
limit 3
that way $result will only have the three rows so no need for extra PHP and less data from the DB in memory
you can add a counter, like:
$nr = 1;
while($row2 = mysql_fetch_assoc($result)) {
if ($nr < 4) {
do your thing;
}
$nr++;
}