how to loop depend <td> on table - php

I have problem with looping in table. i want to loop a <td>, when it reach 10 , it build <tr> automatically.
Here my code:
$sd=mysql_query("SELECT a.*,b.* FROM surat_jalan a inner join packing_list b on a.id_surat = b.identitas_packing WHERE b.identitas_packing = '$ben[id_surat]' ORDER BY netto_packing ASC");
while($pack=mysql_fetch_array($sd)){
$komapack = number_format($pack['netto_packing'],2);
echo"<td>$komapack</td>";
}
I am stuck on here and don't know what to do but I know how to use mod but " don't know how to loop it.

You can use a counter.
$i = 0;
echo "<tr>";
while($pack=mysql_fetch_array($sd)){
$komapack = number_format($pack['netto_packing'],2);
echo"<td>$komapack</td>";
$i++;
if($i % 10 == 0) // check
echo "</tr><tr>";
}
echo "</tr>";

Related

PHP trying to print in 3 columns of 8 rather than 1 column of 24

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.

Adding ID's to dynamically created table

With the help of another Stack Overflow post I've now got dynamic tables working correctly.
I'm looping though an array and creating multiple tables. This works well.
This is the code I'm using :
foreach ($id as $k => $v) {
$maxcols = $cols[$unit[$k]];
$maxid = $cells[$unit[$k]];
$startid = 1;
// maxcols = 4
// maxid = 8
echo "<table id='table$v'><tr>\n";
for ($i = 1;$i<=ceil($maxid/$maxcols);$i++) {
echo "<tr>\n";
for ($j=1;$j<=$maxcols;$j++) {
if ($startid <= $maxid)
echo " <td class='mark'>ID".$startid++."</td>\n";
else
echo " <td class='mark'> </td>\n";
}
echo "</tr>\n<tr>\n";
for ($j=1;$j<=$maxcols;$j++) {
$p = ($i==1) ? $j : ($j+$maxcols);
echo "<td id='$p'></td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
}
If maxcols = 4 and maxid = 8 I get a table that looks like this :
Viewing the code for that table I can see each TD has an ID that matches the header ID value. eg : row 1 column 2 TD ID =2 header = ID2
The ID value is being created using this :
$p = ($i==1) ? $j : ($j+$maxcols);
If I change maxcols = 2 then I get a table that looks like this :
However the TD ID values are not matching the Header ID. They go
1,2
3,4
3,4
3,4
How do I make each cell have the correct TD ID ?
Simple solution:
$maxcols = $cols[$unit[$k]];
$maxid = $cells[$unit[$k]];
$startid = 1;
$idCounter = 1;
…
…
for ($j=1;$j<=$maxcols;$j++) {
echo "<td id='$idCounter'></td>\n";
$idCounter++;
}
There's probably a way to do it using $j, $i, and $maxcols, but why complicate things.
If you really only want to assign ids to cells that will have values, you could make this change
echo "<td id='".($idCounter<=$maxid ? $idCounter : "")."'></td>\n";

Breaking up a long line of values in a box

The array $carry_over returns a really long list of entries, too long for my printout page. I would love to make it in such a way that after 4 entries, it breaks and goes to the next line and breaks at the next four entries until all entries are in.
How can i do this?
Thanks
echo "<table class=\"altrowstable\" bgcolor = gold >\n";
$count = 0;
echo "<tr align= \"center\">\n";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<td>"."Failed: ";
if($score_count !== 0){
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>".$row8['course_code']."</th>";
if ( $count == 7 ){
echo "</tr>\n";
echo "</table>";
}
}
}
Update : Now only the first 7 entries are covered inside the table tags, the subsequent ones are outside the table tags. How can i put them in the tabke tags?
Thanks
Not sure if this is what you are looking for, but you could try:
$count = 0;
echo "<tr>";
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>" . $row8['course_code'] . "</th>";
$count++;
if(count == 4) {
echo "</tr><tr>";
$count = 0;
}
}
echo "</tr>";
It looks like you're using a table for list data. Instead of using a <table>, you should be using a <ul> with <li> for each element. This can then be styled so that each li has display: inline-block;, for example.
I cannot tell what the table looks like from the code above, but you can put an iterator in to count to four then wrap. You also may want to check out Wordwrap

SQL Image Database assembling rows into HTML cells and rows

I have a database of images which I want to output as a gallery ...
I want to display each image in its own cell <td>image1</td> and limit the number of cells to 7 per row.
<tr>
<td>image1</td><td>image2</td><td>image3</td><td>image4</td><td>image5</td><td>image6</td><td>image7</td>
</tr>
Then a new row would be created and continue to output all the images.
<tr>
<td>image8</td>
and so on ..
I know how to do a query, but I am lost as to how to assemble the rows into the format I am looking for.
Can anyone please help me it would be greatly appreciated. Thanks.
First run your query, then get the mysql_fetch_assoc in a variable. Then echo your beginning tags for the table and tr. Then create a foreach loop of the query assoc, as another variable such as row, and echo $row['image'] in a td. This will add a new td with the image for every entry in the database. Then echo your closing tags for the tr and table.
$query = mysql_query("SELECT image FROM table_name");
$query_a = mysql_fetch_assoc($query);
echo "<table><tr>"
foreach ($query_a as $row)) {
echo "
<td>" . $row['image'] . "</td>
";
}
echo "</tr></table>";
Not tested, but try something like this
<table>
<?php
// make sure there is at least 1 row first
$cnt = 0;
$while($row = mysql_fetch_assoc($query))
{
if(++$cnt == 1) {
echo '<tr>';
} elseif($cnt % 7 == 0) {
echo '</tr><tr>';
}
// show images in <td>'s
echo "<td>" . $row['image']. "</td>";
}
echo '</tr>';
?>
</table>
Keep it simple:
Query the database to get the desired information, loop through the results, construct a new row come each new loop.
$sql = "Select * from table";
mysql_query($sql) or die(mysql_error());
if(mysql_num_rows !=0)
{
// Found at least one record, create table
echo "<table>";
echo "<tr>";
while($row = mysql_fetch_assoc($sql)
{
$cell_count = 0;
// Create a new row for each record found
echo "<td>" . $row['image_column']. "</td>";
$cell_count++;
if($cell_count == 7)
{
echo "</tr>";
echo "<tr>";
$cell_count = 0;
}
}
// Close the table
echo "</tr>";
echo "</table>";
}

mysql query giving incorrect result in for loop

The code belows gives me only 18 records
<?php
$result4 = mysql_query("select Distinct Country from trendsmtable where WHORegionAC='Europe - all countries' GROUP BY Country ");
echo "<table width=880 align=center>";
echo "<tr><td colspan=4 style='font-family:Arial;'><b>European Region</b></td></tr>";
$num_columns4 = 4;
$num_rows4 = mysql_num_rows($result4);
$i=0;
while($row4 = mysql_fetch_assoc($result4)){
$results[$i] = $row4['Country'];
$i++;
}
unset($i);
$k=0;
for ($i=0;$i<=($num_rows4/($num_columns4+1));$i++){
echo '<tr>';
for($j=1;$j<=$num_columns4;$j++){
echo "<td width=220 style='font-family:Arial; font-size:12px'>".$results[$k].'</td>';
$k++;
}
echo '</tr>';
$k++;
}
echo "</table>";
?>
while the select statement
select Distinct Country from trendsmtable where WHORegionAC='Europe -
all countries'
returns 22 rows while I execute it in mysql which is correct!
Please help me to found the error.
Well, you have an extra $k++ in there that you don't need:
$k++; // keep this one
}
echo '</tr>';
$k++; // remove this one
}
Edit
I've gone through your code and I've made some edits. Hopefully they'll clean your issue along the way:
// got rid of group by. With a select distinct, it isn't necessary
$result4 = mysql_query("select Distinct Country from trendsmtable where ".
"WHORegionAC='Europe - all countries'");
// a good practice is to always double-quote your HTML attributes.
echo "<table width=\"880\" align=\"center\">";
echo "<tr><td colspan=4 style=\"font-family:Arial;\">".
"<b>European Region</b></td></tr>";
$num_columns4 = 4; // where does 4 come from.
// $results was never initialized before.
$results = array();
while($row4 = mysql_fetch_assoc($result4)){
// This is generally thought of as "cleaner" than using an index.
// And since there cannot be more than, say, 220 rows, cleanliness
// should be a high-priority standard.
$results[] = $row4['Country'];
}
// mysql_num_rows rarely provides any major benefit at all.
$num_rows4 = count($results);
foreach( $results as $key => $val )
{
// This happens every time we fill all columns and need a new row.
if( !( $key % $num_columns4 ) )
{
// makes it so that after the first time this closes the prev
// row before starting a new one.
if( $key ) echo '</tr>';
echo '<tr>';
}
// insert nag about CSS
echo '<td width="220" style="font-family:Arial; font-size:12px">'.
$val.
'</td>';
}
echo '</tr></table>';
I think the PHP looks a little odd, it looks like you are looping from 0 to (22 / 5)
then in the inner loop 4 times.
The group by should not make any difference as you are doing a select distinct
Your SQL code in the PHP file contains a GROUP BY clause, which can reduce the number of rows returned, if you have a country more than once in the db.

Categories