Page 1 of 1

While Loops <--- GRR!

Posted: Wed May 26, 2004 9:53 pm
by HaVoC
This is a forum script i'm making for a personal website.

Code: Select all

<?php
	$fcat = "SELECT * FROM forum_categories";
	$fcat = mysql_query($fcat);

	echo "<table width="100%" cellspacing="0" cellpadding="0" border="0">";

	While ( $cat = MySQL_fetch_array($fcat) ) {
	$cname = $cat['fcat_name'];
	$cid = $cat['fcat_id'];
	
	echo "<tr>";
	echo "<td colspan="3" align="left">$cname</td>";
	echo "</tr>";	
	
	$fforum = "SELECT * FROM forum_forums WHERE forums_catid = '".$cid."' ORDER BY forums_id ASC";
	$fforum = mysql_query($fforum);
	
	while ( $lf = MySQL_fetch_array($fforum) ) {
	$fid = $lf['forums_id'];
	$fname = $lf['forums_name'];
	$description = $lf['forums_description'];
	echo "<tr>";
	echo "<td align="left" width="55%"><a href="index.php?module=forum&action=forum&f=$fid">$fname</a><br>$description</td><td width="10%">Topics</td><td width="10%">Replies</td><td width="25%">Last Post</td>";
	echo "</tr>";
	}
	
	}
	echo "</table>";
?>
How come I can't execute a loop inside a loop? Well, hopefully you understand what i'm trying to do, anyone have any suggestions on how to better my situation?

Posted: Wed May 26, 2004 9:57 pm
by d3ad1ysp0rk
What's wrong with it?

Posted: Thu May 27, 2004 12:40 am
by maqmus
You don't say what the problem is, or how the loop inside the loop should work.

But usually, when I need to do something like this I convert the second loop to a function().

Problem found?

Posted: Thu May 27, 2004 12:49 am
by Lord Sauron
I suppose this is the problem:

Code: Select all

<?php
while ($lf = MySQL_fetch_array($fforum)) { 
?>
Try to change this (for both while loops!) like this:

Code: Select all

<?php
WHILE (!empty(mysql_fetch_array($fforum))) {
    $If = mysql_fetch_array($fforum);
     
    // Etc.
}
?>

Posted: Thu May 27, 2004 1:10 am
by feyd
wouldn't that run through the row's twice as fast?

Posted: Thu May 27, 2004 1:53 pm
by HaVoC
Lord Sauron, I tried what you said and came up with this:


Parse error: parse error, unexpected T_STRING, expecting T_VARIABLE or '$' in /home/tiger/public_html/pharaoh/pe/modules/plugins/forum/forum.php on line 9

Code: Select all

<?
	$fcat = "SELECT * FROM forum_categories";
	$fcat = mysql_query($fcat);

	echo "<table width="100%" cellspacing="0" cellpadding="0" border="0">";

	While ( !empty( mysql_fetch_array($fcat) ) ) {  //line 9
		$cat = MySQL_fetch_array($fcat);
		$cname = $cat['fcat_name'];
		$cid = $cat['fcat_id'];
	
		echo "<tr>";
		echo "<td colspan="3" align="left">$cname</td>";
		echo "</tr>";	
	
		$fforum = "SELECT * FROM forum_forums WHERE forums_catid = '".$cid."' ORDER BY forums_id ASC";
		$fforum = mysql_query($fforum);
	
		While (!empty(mysql_fetch_array($fforum))) { 
			$lf = MySQL_fetch_array($fforum);
			$fid = $lf['forums_id'];
			$fname = $lf['forums_name'];
			$description = $lf['forums_description'];
			echo "<tr>";
			echo "<td align="left" width="55%"><a href="index.php?module=forum&action=forum&f=$fid">$fname</a><br>$description</td><td width="10%">Topics</td><td width="10%">Replies</td><td width="25%">Last Post</td>";
			echo "</tr>";
		}
	
	}
	echo "</table>";
?>

Posted: Thu May 27, 2004 2:09 pm
by Weirdan
hey, you can't execute the query on the same connection until you fetched results from the previous one, or you'll lose it. That was the matter, not the while loop. here is simple trick to overcome this issue:

Code: Select all

//....
$result = mysql_query("some query");
while($q = mysql_fetch_array($result)) $first_result[] = $q;
foreach($first_result as $first_result_row) {
   // here you can execute more queries over the same connection
}