Page 1 of 1

Repeat after 2 entries

Posted: Wed Apr 20, 2005 6:52 pm
by Dale
Im using this piece of code to read from the database and show the information. However at the moment it shows it as:

Code: Select all

<tr>
<td>ENTRY</td>
</tr>
<tr>
<td>ENTRY</td>
</tr>
<tr>
<td>ENTRY</td>
</tr>
<tr>
<td>ENTRY</td>
</tr>
Now i wanted to know how can i make it so it uses the </tr> tag every 2 results? So it shows this:

Code: Select all

&lt;tr&gt;
&lt;td&gt;ENTRY&lt;/td&gt;
&lt;td&gt;ENTRY&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ENTRY&lt;/td&gt;
&lt;td&gt;ENTRY&lt;/td&gt;
&lt;/tr&gt;

Code: Select all

<?php
$conn = mysql_connect("***","***","***");
mysql_select_db("***",$conn);

	$sql = "SELECT * FROM categorys";
	$result = mysql_query($sql,$conn) or die(mysql_error());
	while ($r = mysql_fetch_array($result)) {
		$cat_id = $r['id'];
		$cat_title = $r['title'];
		$cat_description = $r['description'];
		$cat_supportsite = $r['supportsite'];
		$cat_tutcount = $r['tutcount'];
		$cat_latestid = $r['latestid'];

	echo "<tr>";
	echo "<td><font face=\"arial\" size=\"2\"><a href=\"./cats.php?cid=$cat_id\">$cat_title</a> (<i><b>$cat_tutcount</b> Tutorials</i>)";

	if ($cat_supportsite != "") {
		echo " [About <a href=\"$cat_supportsite\" target=\"_blank\">$cat_title</a>]";
	}

	echo "<br>$cat_description</font></td>";
	echo "</tr>";

	}
?>

Posted: Wed Apr 20, 2005 7:43 pm
by feyd
Useful Posts thread: Multi-column formatted output


viewtopic.php?t=29816

Posted: Wed Apr 20, 2005 11:12 pm
by Todd_Z

Code: Select all

$x = 1;

while ( $info = mysql_fetch_array($results) ) {

  if ( $x%2 == 1 )
    echo "<tr>";

  echo "<td>".$info["ID"]."</td>";

  if ( $x%2 == 1 )
    echo "</tr>";

  $x = 3 - $x;

}

RE

Posted: Thu Apr 21, 2005 5:26 am
by harrisonad

Code: Select all

$firsttime = true;
while($data = mysql_fetch($result)){
if($firsttime) echo "<TR>";
echo "<TD>$data[0]</TD>";
if(!$firsttime){
 echo "</TR>"
 $firsttime = true;
}else
 $firsttime = false;
}

Posted: Thu Apr 21, 2005 5:57 am
by Dale
Cheers Guys!