Help running a loop in a piece of code

Hi all, its a bit hard to explain what i’m trying to do in one sentance. I’m trying to create a forum index menu using a sql query and joining some data up.

I have managed to get it to list all the forum halls that are shown as being online, but inside each forum hall there are sub-forums. Now this is where i am currently having a problem. I can get it to show the first sub-forum in each hall, but no more. now i have several sub-forums in my first forum hall (noting else in the other halls yet).

so you know what i have done its easier for me to show you the whole page of code (ignore the include files they aren’t important), what is important is the SQL query and the code that follows it.


<?php
/******************************************/
/* Content Management System              */
/* ©2010 Tdf Enterprises                  */
/*                                        */
/* Dynamically Sourced Forum Index File   */
/*                                        */
/* Please Leave This Notice Intact        */
/* As Part Of The Copyright - ©Tdf 2010   */
/******************************************/
session_start();

// gather required file: auth-user 
require 'authentication/users.php';

// set the page name
$pageTitle = 'The Forum Halls';

// gather required files: header.inc, sidebar.inc
require 'includes/header.inc.php';
require 'includes/sidebar.inc.php';
?>
<!--
/******************************************/
/* Content Management System              */
/* ©2010 Tdf Enterprises                  */
/*                                        */
/* The Dynamic Forum Index File           */
/*                                        */
/* Please Leave This Notice Intact        */
/* As Part Of The Copyright - ©Tdf 2010   */
/******************************************/
-->

<?php
// gather forum hall info
$sql = <<<EOS
SELECT h.hall_id as id, h.hall_name as fhall, 
    h.hall_description as description,
    u.name as admin, s.subforum_id as subid,
	s.subforum_name as subname, s.subforum_desc as subdesc,
    s.subforum_moderator as submod
  FROM cms_forum_halls h
  LEFT JOIN cms_hall_subforums s
  ON h.hall_id = s.hall_id
  LEFT JOIN cms_user_info u
  ON h.hall_administrator = u.id
  WHERE hall_status = 1
  GROUP BY h.hall_id
EOS;
$result = mysql_query($sql, $conn)
or die ('Could not retrieve indexable information for the forum halls: ' .mysql_error());
if (mysql_num_rows($result) == 0) { ?>

<div class="feature"> <img src="images/defaults/RCLamenColour.gif" height="200" width="200" />
<h3>Forums Notification</h3>
<p> It appears the forums are currently offline.<br>
This may be a temporary issue that the administrators are working on.<br>
Please check back later!.
</div>

<?php } else {
				while ($row = mysql_fetch_array($result)) {
					
?>
<div class="story">
<ul class="accordion">
<li id="<?php echo $row['id'];?>">
<h2><a href="#<?php echo $row['id'];?>"><?php echo $row['fhall'];?></a></h2>
<p><?php echo $row['description'];?></p>
<p>Hall Administrator: <?php echo $row['admin'];?></p>

<div class="links">
<h3>Sub-Forums</h3>
<ul>

<li><a href="viewforum.php?hall=<?php echo $row['id'];?>&subforum=<?php echo $row['subid'];?>"><?php echo $row['subname']?></a><br><?php echo $row['subdesc'];?></li>

</ul>
</div>

</li>
</ul>
</div>
<?php
	}
}
// gather required file: footer.inc
require 'includes/footer.inc.php';
?>

now as you can see its this piece of code that i need help with:


<?php
// gather forum hall info
$sql = <<<EOS
SELECT h.hall_id as id, h.hall_name as fhall, 
    h.hall_description as description,
    u.name as admin, s.subforum_id as subid,
	s.subforum_name as subname, s.subforum_desc as subdesc,
    s.subforum_moderator as submod
  FROM cms_forum_halls h
  LEFT JOIN cms_hall_subforums s
  ON h.hall_id = s.hall_id
  LEFT JOIN cms_user_info u
  ON h.hall_administrator = u.id
  WHERE hall_status = 1
  GROUP BY h.hall_id
EOS;
$result = mysql_query($sql, $conn)
or die ('Could not retrieve indexable information for the forum halls: ' .mysql_error());
if (mysql_num_rows($result) == 0) { ?>

<div class="feature"> <img src="images/defaults/RCLamenColour.gif" height="200" width="200" />
<h3>Forums Notification</h3>
<p> It appears the forums are currently offline.<br>
This may be a temporary issue that the administrators are working on.<br>
Please check back later!.
</div>

<?php } else {
				while ($row = mysql_fetch_array($result)) {
					
?>
<div class="story">
<ul class="accordion">
<li id="<?php echo $row['id'];?>">
<h2><a href="#<?php echo $row['id'];?>"><?php echo $row['fhall'];?></a></h2>
<p><?php echo $row['description'];?></p>
<p>Hall Administrator: <?php echo $row['admin'];?></p>

<div class="links">
<h3>Sub-Forums</h3>
<ul>

<li><a href="viewforum.php?hall=<?php echo $row['id'];?>&subforum=<?php echo $row['subid'];?>"><?php echo $row['subname']?></a><br><?php echo $row['subdesc'];?></li>

</ul>
</div>

</li>
</ul>
</div>
<?php
	}
}

And specifically, its this piece of code that i wish to be looping through however many sub-forums i have in each hall:


<li><a href="viewforum.php?hall=<?php echo $row['id'];?>&subforum=<?php echo $row['subid'];?>"><?php echo $row['subname']?></a><br><?php echo $row['subdesc'];?></li>

I appreciate any help that you can give me to solving this puzzle and thank you all in advance.

Without seeing your database structure, it appears you are using the Adjacency List Model for your database.

Imho The Nested Set Model is much better and easier for storing and handling hierarchal data in a database.

The above link includes some example code for handling data using both models.

If it’s not too late, I would recommend changing to the Nested Set Model after which life should become much easier.

thanks, i think i’ll try and figure it out though, it means changing so much database info… but thanks for the tip all the same my friend :wink:

Take out the GROUP BY clause.

EDIT: I should say, take out the GROUP BY clause, and then modify your loop to detect a new Hall.

Not entirely sure how the NSM would help in this situation, but it is helpful in some.

Thanks buddy, removing the group by clause alone, makes it detect and show everything, the snag is it shows me the hall they reside in for each subforum it picks up.

IE: Hall One - subforum 1

 Hall One - subforum 2

and so one, so the man menu ends up looking a bit of a mess. I do sort of see where your going with it though from simply removing the group by clause. Any suggestions how i should modify the next bit then ?

Lets see if i can nudge you in the right direction without doing it for you.
You now have a loop that iterates over all subforums.
So, let us say we start with Hall One, Subforum 1.
We display the hall information.
We display the subforum information.
We restart the loop.
Now we’re on Hall One, Subforum 2.
IF the Hall has not changed, we dont want to display the hall information anymore.
We display the subforum information.
We restart the loop.
Now Hall Two, Subforum 1.
IF the Hall has changed, we display the new hall information.
We display the subforum information.
We restart the loop.

I’m thinking you want me to run a couple of if statements and the operators != and ==

Am i thinking along the lines you want me too ?

I appreciate what your trying to do by making me figure it out without you just supplying me with some code :slight_smile:

You will need 2 IF’s. No elses. You will need 1 additional variable.

Try and figure out what conditions you need:
One of the if’s needs to check if the hall is the same as the current hall we’re working with.
One of the if’s needs to check if this is the first hall.

I’m going to be honest now, not sure how good i can do this like.

here goes, additional variable $currhall = something
this should be used with one of the ifs to check that current hall is the one we are working with.

as for anything else i am lost here, i tried to get it to show just one set of halls and didnt worry about the subforums for the hall at this point, but nothing i did even ewith if statements made any difference…
i’ll be honeest that i spent all day on this conundrum after you tried to nudge me with the two IF’s in your post… all i have done is sun myself round and round in circles getting nowhere at all…

You’ve got the right idea.
I’ll block it out for you, and lets see if you can put it into code. If not i’ll walk you the rest of the way.

While There is another record in the database:
If We’re talking about a new hall:
If this wasnt the first hall:
Output End of previous hall’s block of HTML.
EndIf
Output start of new hall’s block of HTML.
Update current hall variable.
Endif
Output Subforum information.
Endwhile

Hint: The first time you run it (IE: The first hall), the variable hasn’t been set to anything yet.

lost now possibly coz of the code itself being outputted in javascript accordians.


<?php
// gather forum hall info 
$sql = <<<EOS
SELECT h.hall_id as id, h.hall_name as fhall, 
    h.hall_description as description,
    u.name as admin, s.subforum_id as subid,
	s.subforum_name as subname, s.subforum_desc as subdesc,
    s.subforum_moderator as submod
  FROM cms_forum_halls h
  LEFT JOIN cms_hall_subforums s
  ON h.hall_id = s.hall_id
  LEFT JOIN cms_user_info u
  ON h.hall_administrator = u.id
  WHERE hall_status = 1 
EOS;
$result = mysql_query($sql, $conn)
or die ('Could not retrieve indexable information for the forum halls: ' .mysql_error());
if (mysql_num_rows($result) == 0) { ?>

<div class="feature"> <img src="images/defaults/RCLamenColour.gif" height="200" width="200" />
<h3>Forums Notification</h3>
<p> It appears the forums are currently offline.<br>
This may be a temporary issue that the administrators are working on.<br>
Please check back later!.</p>
</div>

<?php } else {
				while ($row = mysql_fetch_array($result)) {
					
?>
<div class="story">
<ul class="accordion">

<li id="<?php echo $row['id'];?>">
<h2><a href="#<?php echo $row['id'];?>"><?php echo $row['fhall'];?></a></h2>
<p><?php echo $row['description'];?></p>
<p>Hall Administrator: <?php echo $row['admin'];?></p>

<div class="links">
<h3>Sub-Forums</h3>
<ul>

<li><a href="viewforum.php?hall=<?php echo $row['id'];?>&subforum=<?php echo $row['subid'];?>"><?php echo $row['subname']?></a><br><?php echo $row['subdesc'];?></li><br>

</ul>
</div>

</li>
</ul>
</div>
<?php
	}
} ?>
<br />

now as to where i would put these new additional clauses into it i have no ideas… my heads now a little battered thinking about it :confused:

Hehe. Alright, let me point it out. I’ve commented in some stuff to help point out my moves.


<?php
// gather forum hall info 
$sql = <<<EOS
SELECT h.hall_id as id, h.hall_name as fhall, 
    h.hall_description as description,
    u.name as admin, s.subforum_id as subid,
	s.subforum_name as subname, s.subforum_desc as subdesc,
    s.subforum_moderator as submod
  FROM cms_forum_halls h
  LEFT JOIN cms_hall_subforums s
  ON h.hall_id = s.hall_id
  LEFT JOIN cms_user_info u
  ON h.hall_administrator = u.id
  WHERE hall_status = 1 
  ORDER BY h.hall_id 
EOS;
 //NOTE HERE: Added Order by clause to ensure they're bunched together!
$result = mysql_query($sql, $conn)
or die ('Could not retrieve indexable information for the forum halls: ' .mysql_error());
if (mysql_num_rows($result) == 0) { ?>

<div class="feature"> <img src="images/defaults/RCLamenColour.gif" height="200" width="200" />
<h3>Forums Notification</h3>
<p> It appears the forums are currently offline.<br>
This may be a temporary issue that the administrators are working on.<br>
Please check back later!.</p>
</div>

<?php } else {
				while ($row = mysql_fetch_array($result)) {
	 		//Here we go.
                       if($currhall != $row['id']) { //If a new Hall is detected... This is IF1.
                            if(isset($currhall)) { // If $currhall is set, we've been here before. This is IF2.
		               ?>
</ul>
</div>

</li>
</ul>
</div>
<?
} //EndIf2. Note that this was the 'end' of a hall segment. What follows is the beginning.
 
?>
<div class="story">
<ul class="accordion">

<li id="<?php echo $row['id'];?>">
<h2><a href="#<?php echo $row['id'];?>"><?php echo $row['fhall'];?></a></h2>
<p><?php echo $row['description'];?></p>
<p>Hall Administrator: <?php echo $row['admin'];?></p>

<div class="links">
<h3>Sub-Forums</h3>
<ul>
<?
  $currhall = $row['id']; //Set our variable so we don't re-trigger on this hall.
} //End If1
?>
<li><a href="viewforum.php?hall=<?php echo $row['id'];?>&subforum=<?php echo $row['subid'];?>"><?php echo $row['subname']?></a><br><?php echo $row['subdesc'];?></li><br>

<?php
	}
} ?>
<br />

well, it works as it should now… so thank you very very much for the help… I was on the right lines, but i wasn’t trying to break it up like you did.

my only prob now is the last segment of the last hall is for some reason displaying the site footer in there…

I know its something to do with how it was broke up and displayed in the bigger block of html/php

neh mind, something left for me to ponder and solve… not a great issue so not rush to fix it, but fix it i will…

Once again my friend, thanks very much for the help :smiley: :tup:

You need to close the final set once the loop’s complete.

hmmm, but everything was closed ie 4 { and 4 } brackets…

Think about the execution of the loop through 2 iterations.

Start Loop
Begin Hall1
Output Subforum
End Loop / Start Loop
End Hall1

Start Hall2
Output Subforum
End Loop

Note the order of the bold lines. Where does Hall 2 end, in that pattern?
You need the </ul>'s and </div>'s.

thought about it, tried to makes sure its all there, i can’t figure out why it produces the footer where it is though… and i am lost how this picks up the bottom segment of everything first in the code structure… no ideas how to solve it, tried lots of things and always failed to produce the correct desired effect of the footer being in its complete own div underneath the sites main sidebar and content…

to show you an idea of what the main pages look like, heres my custom error page (404 not found as forums page not yet uploaded) Brief idea of site look

Couldnt find the edit button to edit my last post, so please excuse the double post, and understand why:

Thanks lionstar, i ahve now managed to fix it. I most sincerely appreciate the help the gave me, and the thoughts about logical code structure… things i am trying to work on :wink:

Anyways thank you so much for helping me to create and display my forums as i intended them to be. Took a few days to do it, but thanks my friend, i shan’t forget it :smiley:

F.A.O Moderators:
This thread can now be locked! Thank you :wave: