[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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

0
befidled
Asked:
befidled
1 Solution
 
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now