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
HaVoC
Forum Commoner
Posts: 83 Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA
Post
by HaVoC » Wed May 26, 2004 9:53 pm
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?
d3ad1ysp0rk
Forum Donator
Posts: 1661 Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA
Post
by d3ad1ysp0rk » Wed May 26, 2004 9:57 pm
What's wrong with it?
maqmus
Forum Commoner
Posts: 30 Joined: Mon Mar 08, 2004 1:10 pm
Post
by maqmus » Thu May 27, 2004 12:40 am
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().
Lord Sauron
Forum Commoner
Posts: 85 Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL
Post
by Lord Sauron » Thu May 27, 2004 12:49 am
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.
}
?>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu May 27, 2004 1:10 am
wouldn't that run through the row's twice as fast?
HaVoC
Forum Commoner
Posts: 83 Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA
Post
by HaVoC » Thu May 27, 2004 1:53 pm
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>";
?>
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Thu May 27, 2004 2:09 pm
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
}