Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

complex horizontal loop

Status
Not open for further replies.

pugs421

Technical User
Joined
Nov 25, 2002
Messages
114
Location
US
I found a script to display output like this

1 2
3 4
5 6

Here is the code:

<?php
$columns = 2;
$count = 0;

$num_rows = mysql_num_rows($result);

echo "<table>";
echo "<tr>";

for($i=0; $i < $num_rows; $i++)
{
$row = mysql_fetch_array ($result);
if($i % $columns == 0 && $i != 0)
{
echo "</tr>";
echo "<tr>";
$count = 0;
}
echo "<td>" . $row['stuff'] . "</td>";
$count++;
}

// This makes sure all the columns are filled
// Without this, when $num_rows % $columns does not equal 0
// There would be less columns in the last <tr> then $columns
if($columns != $count)
{
for($i = 0; $i < ($columns - $count); $i++)
{
echo"<td bgcolor=red>&nbsp;</td>"; //Change this if you want to fill it with something else
}
}

echo "</tr>";
echo "</table>";
?>



How would one dispay the retrieved information in more than one cell for each result?


For example:

<Table>
<tr><td>result1 picture</td> <td>result2 picture</td> </tr>
<tr><td>result1 description</td><td>result2 description</td></tr>
<tr><td>result3 picture</td><td>result4 picture</td> </tr>
<tr><td>result3 description</td><td>result4 description</td></tr>

</table>
 
Instead of the result immediately printed accumulate the generated HTML in variables:
Code:
$line1 = "<tr>";
$line2 = "<tr>";
for($i=0; $i < $num_rows; $i++)
{
    $row = mysql_fetch_array ($result);
    if($i % $columns == 0 && $i != 0)
    {
        echo $line1 . "</tr>";
        echo $line2 . "</tr>";
        # reset counter and lines
        $count = 0;
        $line1 = "<tr>";
        $line2 = "<tr>";
    }
    $line1 .= "<td>" . $row['stuff1'] . "</td>";
    $line2 .= "<td>" . $row['stuff2'] . "</td>";
    $count++;
}
 
This might be useful:
I put the fields into arrays and then output.

Code:
<?php
$numcols = 2;
$imgs = array();
$desc = array();
$db = mysql_connect("", "", "");
mysql_select_db("menagerie",$db);
$query = "select * from pict";
$result = mysql_query($query,$db);
while ($row = mysql_fetch_row($result))
  {
	$i = 0;
	while($i < mysql_num_fields($result))
	{
	$imgs[] = $row[$i++];
	$desc[] = $row[$i++];
	}
   }
mysql_close($db);
while(count($imgs) % $numcols != 0) {
$imgs[] = "&nbsp;";
$desc[] = "&nbsp;";
  }
$imgind = 0;
$desind = 0;
while($imgind < count($imgs)) {
  print "<tr>";
    for($c = 0; $c < $numcols; $c++) {
	print "<td>".$imgs[$imgind++]."</td>"; }
  print "</tr>\n";
  print "<tr>\n";
    for($c2 = 0; $c2 < $numcols; $c2++) {
  print "<td>".$desc[$desind++]."</td>"; }
  print "</tr>\n";
}
?>

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top