Repeat after 2 entries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Repeat after 2 entries

Post 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>";

	}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Useful Posts thread: Multi-column formatted output


viewtopic.php?t=29816
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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;

}
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

RE

Post 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;
}
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Cheers Guys!
Post Reply