Need help displaying dynamic list

I have a navigation which is based on my categories table, and a subnav of article titles which are related to a category through a category_id field.

I'm trying to loop through the categories and while looping through each category, loop through and display each article for that category using the following code...

but I can't seem to get it to work...what am I missing?
<?php do { ?>
	<li><a href="../contact/index.php?section_id=<?php echo $row_rsNavContact['section_id']; ?>&nav=<?php echo $row_rsNavContact['navigation']; ?>"><?php echo $row_rsGetCategories['category']; 
	$myCurrentID = $row_rsGetCategories['id'];?></a></li>
    <ul>
    <?php do { ?>
    <li><?php echo $row_rsGetArticles['head']; ?></li>
    <?php } while ($row_rsGetArticles['category_id']=$row_rsGetCategories['id']); ?>
 
    </ul>
    
    
<?php } while ($row_rsGetCategories = mysql_fetch_assoc($rsGetCategories)); ?>
</ul></div>
<?php } ?>

Open in new window

LVL 1
befidledAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hube02Commented:
This is just a guess, but it appears to me that you are trying to echo values before you have assigned them. For instance in line 3 (of your attached code above) you echo $row_rsGetCategories['id'] but $row_rsGetCategories does not have a value until line 12.

for a do while loop you need to set a value first (unless you have and did not include it in your code snippet.)

Try something like this
<?php
  while ($row_rsGetCategories = mysql_fetch_assoc($rsGetCategories)) {
    ?>
    <li><a href="../contact/index.php?section_id=<?php
        echo $row_rsNavContact['section_id']; ?>&nav=<?php
        echo $row_rsNavContact['navigation']; ?>"><?php
        echo $row_rsGetCategories['category']; 
        $myCurrentID = $row_rsGetCategories['id'];?></a></li>
      <ul>
      <?php
        while ($row_rsGetArticles['category_id']=$row_rsGetCategories['id']) {
          ?>
          <li><?php echo $row_rsGetArticles['head']; ?></li>
          <?php
        }
  }
?>

Open in new window

0
befidledAuthor Commented:
the values for $row_rsGetCategories was set much earlier in the php document. All of my variables have been set and I've verified this by echoing their value during tests.

I'm trying to figure out why  my nested loop isn't working properly. Knowing that the variables are already set, do you see anything in the nested loop that is a problem?

This following code is the portion I am most interested in. Without this code, my top level navigation displays fine, but when I add this code, my page never gets to the browser and is just blank.
<ul>
    <?php do { ?>
    <li><?php echo $row_rsGetArticles['head']; ?></li>
    <?php } while ($row_rsGetArticles['category_id']=$row_rsGetCategories['id']); ?>
 
    </ul>

Open in new window

0
Hube02Commented:
This looks like it could be trouble. Again, I can't tell exactly what is happening in this portion of the code:

while ($row_rsGetArticles['category_id']=$row_rsGetCategories['id'])

are you testing to see if the left is equal to the right or are you setting the left equal to the right. If you mean to only test this value then you need a double ==
with only a single = you are setting the left equal to the right each time the loop occurs and the result will always evaluate to true.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

befidledAuthor Commented:
Here is my full code, hopefully this clarifies things...
<?php require_once('../Connection/dbconnection.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
mysql_select_db($database_mydb, $mydb);
$query_rsGetCategories = "SELECT * FROM categories ORDER BY categories.`order`";
$rsGetCategories = mysql_query($query_rsGetCategories, $mydb) or die(mysql_error());
$row_rsGetCategories = mysql_fetch_assoc($rsGetCategories);
$totalRows_rsGetCategories = mysql_num_rows($rsGetCategories);
 
mysql_select_db($database_mydb, $mydb);
$query_rsGetArticles = "SELECT articles_categories.article_id,  	articles_categories.category_id,  	articles.navigation,  	articles.navigationview,  	articles.navlevel,  	articles.navorder,  	articles.urltitle,  	articles.section_title,  	articles.section_id,  	articles.head,  	articles.content,  	articles.audioavailable,  	articles.created,  	articles.modified,  	articles.visible,  	articles.publishstatus,  	articles.notes,  	articles.author,  	articles.metadesc,  	articles.metakey,  	articles.metaauthor,  	articles.metacopyright FROM articles_categories INNER JOIN articles ON articles_categories.article_id = articles.id 	 INNER JOIN categories ON articles_categories.category_id = categories.id ORDER BY articles_categories.category_id, articles.navorder";
$rsGetArticles = mysql_query($query_rsGetArticles, $mydb) or die(mysql_error());
$row_rsGetArticles = mysql_fetch_assoc($rsGetArticles);
$totalRows_rsGetArticles = mysql_num_rows($rsGetArticles);
 
//mydb SPECIFIC VARIABLES
$mySectionId = $_GET['section_id'];
 
?>
<!-- CONTACT NAV -->
<?php if ($mySectionId == 6) { ?>
<div id="subnav">Article Categories
<ul>
<?php do { ?>
	<li><a href="../contact/index.php?section_id=<?php echo $row_rsNavContact['section_id']; ?>&nav=<?php echo $row_rsNavContact['navigation']; ?>"><?php echo $row_rsGetCategories['category']; 
	$myCurrentID = $row_rsGetCategories['id'];?></a></li>
    <ul>
    <?php do { ?>
    <li><?php echo $row_rsGetArticles['head']; ?></li>
    <?php } while ($row_rsGetArticles['category_id']==$row_rsGetCategories['id']); ?>
 
    </ul>
    
    
<?php } while ($row_rsGetCategories = mysql_fetch_assoc($rsGetCategories)); ?>
</ul></div>
<?php } ?>
 
<?php
mysql_free_result($rsGetCategories);
 
mysql_free_result($rsGetArticles);
?>

Open in new window

0
Hube02Commented:
This is what stands out to me when I look at the code. In your queries you are ordering the category results by 'order' and in the next query, the big one, you are ordering the results by 'category_id' first. If the order from the first query is not the same as the order from the second then when you start looping through the results it could fail on the first pass.

You might have the following results

Query 1: first row: category_id = 3, order = 1
         second row: category_id = 2, order = 2
              third row: category_id = 1, order = 3

Query 2: first row: category_id = 1
         second row: category_id = 2
              third row: category_id = 3

Try altering the order of the second query:


$query_rsGetArticles = "SELECT articles_categories.article_id,          articles_categories.category_id,        articles.navigation,    articles.navigationview,        articles.navlevel,      articles.navorder,      articles.urltitle,      articles.section_title,         articles.section_id,    articles.head,          articles.content,       articles.audioavailable,        articles.created,       articles.modified,      articles.visible,       articles.publishstatus,         articles.notes,         articles.author,        articles.metadesc,      articles.metakey,       articles.metaauthor,    articles.metacopyright FROM articles_categories INNER JOIN articles ON articles_categories.article_id = articles.id      INNER JOIN categories ON articles_categories.category_id = categories.id ORDER BY categories.order, articles.navorder";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CWS (haripriya)Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Accept: Hube02 {http:#20828013}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

cyberwebservice
Experts Exchange Cleanup Volunteer
0
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.