tags:

views:

74

answers:

5

How do I get the array to increment to the next row? I am making a table of labels 3 wide and ten high. The result that I am getting is three repeats in each row. How can I bump to the next row for each table cell.

$i =0;
for ($i = 1; $i <= 3; ++$i){
while($row = mysql_fetch_array($result)){

$company = $row['company'];
$comp_strt = $row['comp_strt'];
$comp_city = $row['comp_city'];
$comp_state = $row['comp_state'];
$comp_zip = $row['comp_zip'];
$celldata= $company."<br>".$comp_strt."<br>".$comp_city.",&nbsp;".$comp_state."&nbsp;".$comp_zip;
if($i = 1){echo "<tr style='row-height: 5em;'><td>'".$celldata."'</td>";}
if($i = 2){echo "<td>'".$celldata."'</td>";}
if($i = 3){echo "<td>'".$celldata."'</td></tr>"; $i = 1;}
}}
A: 

If you want to draw 3 label next to each other, you just have to insert a </tr><tr> block in the right position. You can just have an incrementing variable, no need for the for loop:

$i = 0;
echo '<table><tr>';
while($row = mysql_fetch_array($result)){
    if($i == 3) {
        echo '</tr><tr>';
        $i = 0;
    }

    $company = $row['company'];
    $comp_strt = $row['comp_strt'];
    $comp_city = $row['comp_city'];
    $comp_state = $row['comp_state'];
    $comp_zip = $row['comp_zip'];
    $celldata= $company."<br>".$comp_strt."<br>".$comp_city.",&nbsp;".$comp_state."&nbsp;".$comp_zip;

    echo "<td>$celldata</td>";
    $i++;
}
echo '</tr></table>';

So now every time the $i counter reaches 3, it creates a table row break and sets the $i back to zero, resulting in 3 cells per row.

Tatu Ulmanen
A: 

get rid of the for cycle if you don't want to do it three times, ffs

just somebody
+1  A: 

Youre using = to check for equality. You're supposed to use ==

if($i == 1){echo "<tr style='row-height: 5em;'><td>'".$celldata."'</td>";}
if($i == 2){echo "<td>'".$celldata."'</td>";}
if($i == 3){echo "<td>'".$celldata."'</td></tr>"; $i = 1;}

Probably won't fix your problem, but it's a start.

Galen
+1  A: 

The problem is the while loop will not exit until the rows in mysql_fetch_array are exhausted. Just use the while loop and increment $i inside of while:

$i= 0;
while ($row = mysql_fetch_array($result)) {
 // process the row into $celldata
 if ($i==0 || $i%3==0) {
   if ($i > 0) // close out existing row
   // start a new row
 }
 // output cell data
 $i++;
}
// Output a closing '</tr>' tag if $i > 0
pygorex1
On that note, the modulus operator **%** used here is highly recommended as opposed to resetting the counter. +1
cballou
+1  A: 

I would do this:

$cellsPerRow = 3;
$i = 0;
echo '<table>';
while ($row = mysql_fetch_array($result)) {
    if ($i % $cellsPerRow == 0) {
        echo '<tr>';
    }

    // process $row and echo the table cell

    if ($i % $cellsPerRow == $cellsPerRow - 1) {
        echo '</tr>';
    }
    $i++;
}
if ($i > 0 && $i % $cellsPerRow != 0) { // finish off row if needed
    while ($i % $cellsPerRow != 0) {
        echo '<td></td>';
        $i++;
    }
    echo '</tr>';
}
echo '</table>';

This will always give you a proper table.

Gumbo
Gonna take me a little while (no pun intended) to digest this one. It seems that this solution leaves no room for possible error. Thank you Gumbo.
Tom
@Tom: Well there is one. You should test if there are any rows at all. Otherwise you’ll get the table tags printed but with no rows/cells. So take `mysql_num_rows` in the condition of an additional *if* statement that wraps around the whole `echo '<table>'; … echo '</table>';`.
Gumbo
Yeah, another if(!$result) should handle it.
Tom
@Tom: That’s not the same. `$result` is a resource if the query was successful (and thus not *false*) although you might not have selected a single row. It is only *false* if an error occurred.
Gumbo