We help IT Professionals succeed at work.

looping problem

derrida
derrida asked
on
hi
i have 2 <ul> tags nested. the idea is a menu and itd drops (hope you understand me).
i have 2 problems with it:
1- when the page load i get the message that my mysql fetch array argument is not valid, i get only hte titles and not the subs. the error message replace the subs.
yet, when i click a title i do get the subs, but, and this bring me to my second problem:
2- all the subs appear after the first <li> and not with "their" <li>. the content does change to the proper content but the placement is wrong. i have tried to play with it but something does not work.

here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
$link=mysql_connect("localhost","","");
mysql_select_db("");
 
mysql_query("set names 'utf8'");
$catRS="SELECT * FROM categories";
$catResults=mysql_query($catRS);
$totalRows=mysql_num_rows($catResults);
//echo $totalRows;
 
 
$postsRS="SELECT * FROM posts WHERE post_cat=" .$_GET['cat'];
$postsResults=mysql_query($postsRS);
//$postsTotalRows=mysql_num_rows($postsResults);
//echo $postsTotalRows;
?>
 
<ul>
<?php
while($row=mysql_fetch_array($catResults)){
 
?>
<li><a href="ul2.php?cat=<?php echo $row['cat_id']; ?>"><?php echo $row['cat_name']; ?></a></li>
 
<!--if there are subs then a new ul-->
 
<?php
 
while($rowP=mysql_fetch_array($postsResults)){
if($_GET['cat'] == $rowP['post_cat'] || $_GET['cat'] == 0 ){
?>
<ul>
<li><?php echo  $rowP['post_title'];  ?><br/><i><?php echo  $rowP['post_date'];  ?></i>
<p><?php echo  $rowP['post_text'];  ?></p>
 
</li>
 
</ul>
<!--end of-----if there are subs then a new ul-->
<?php
}
}
}
?>
</ul>
 
</body>
</html>

Open in new window

Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>$postsRS="SELECT * FROM posts WHERE post_cat=" .$_GET['cat'];
this will eventually not be set the first time? ie the $_GET['cat'] being empty, making the query invalid
you will have to handle that some whay...

if (isset(.$_GET['cat']))
{
  $postsRS="SELECT * FROM posts WHERE post_cat=" .$_GET['cat'];
}
else
{
  // return the "root" posts...
  $postsRS="SELECT * FROM posts WHERE post_cat IS NULL" ;
}

Author

Commented:
hi

thanks for the answer and the explaination. i thought that on load there is no $_get value and i have tried other methods that did not worked, but yours does.

do you have any suggestion with the second problem?


best regards

ron
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
can you try without  the <p>, ie use some <div></div> instead? I fear that the <p> disrupts the <li> stuff a bit.

Author

Commented:
hi

thanks for the answer and sorry it took me some time (i had to be away).

i have changed it to a <div> (even thought i could not see why) and it does not solve the problem.

best regards

ron
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
I see now.
it is because you close the <il> of the outer query too early:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
$link=mysql_connect("localhost","","");
mysql_select_db("");
 
mysql_query("set names 'utf8'");
$catRS="SELECT * FROM categories";
$catResults=mysql_query($catRS);
$totalRows=mysql_num_rows($catResults);
//echo $totalRows;
 
 
$postsRS="SELECT * FROM posts WHERE post_cat=" .$_GET['cat'];
$postsResults=mysql_query($postsRS);
//$postsTotalRows=mysql_num_rows($postsResults);
//echo $postsTotalRows;
?>
 
<ul>
<?php
while($row=mysql_fetch_array($catResults)){
 
?>
<li><a href="ul2.php?cat=<?php echo $row['cat_id']; ?>"><?php echo $row['cat_name']; ?></a>
 
<!--if there are subs then a new ul-->
 
<?php
 
while($rowP=mysql_fetch_array($postsResults)){
if($_GET['cat'] == $rowP['post_cat'] || $_GET['cat'] == 0 ){
?>
<ul>
<li><?php echo  $rowP['post_title'];  ?><br/><i><?php echo  $rowP['post_date'];  ?></i>
<p><?php echo  $rowP['post_text'];  ?></p>
 
</li>
 
</ul>
<!--end of-----if there are subs then a new ul-->
<?php
}
}
?>
</li>
<?php
}
?>
</ul>
 
</body>
</html>

Open in new window

Author

Commented:
hi
can you be more specific? i have tried to close the tag in different places but with the same result.
the right content appear but not in the right place.

ron
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
the code suggestion above has already the suggested modification.
it moved the </li> from the main <li> below the list of <ul></ul> generated in the inner while loop...

Author

Commented:
hi

well the problem remains  even copy and pasted your code. instead of:

title
    sub
    sub
title
   sub
title
title

every time i chose a title all the subs appear after the first title.


ron

Author

Commented:
hi

i hope you or someone else can keep helping me.


best regards

ron

Author

Commented:
hi
i cannot understand why the help stopped but i hope that someone will help because it seem like it should be a small problem but i cannot fix it.

best regards

ron
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
here we go (tested code, hence the delay)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
$link=mysql_connect("localhost","","");
mysql_select_db("");
 
mysql_query("set names 'utf8'");
$catRS="SELECT * FROM categories";
$catResults=mysql_query($catRS);
$totalRows=mysql_num_rows($catResults);
 
 
?>
 
<ul>
 <?php
 while($row=mysql_fetch_array($catResults))
 {
  print "\n<li><a href=\"ul2.php?cat={$row['cat_id']}\">{$row['cat_name']}</a>\n";
 
  // if there are subs then a new ul
  
  if(@$_GET['cat'] == $row['cat_id'] || @$_GET['cat'] == 0 )
  {
    $postsRS="SELECT * FROM posts WHERE post_cat= " . $row['cat_id'];
    $postsResults=mysql_query($postsRS);
    ?>
    <ul>
    <?php 
    while($rowP=mysql_fetch_array($postsResults))
    {
       print "<li>{$rowP['post_title']}<br/><i>{$rowP['post_date']}</i><p>{$rowP['post_text']}</p></li>\n";
    }
    ?>
    </ul>
    <?php
  } // if(@$_GET['cat'] == $rowP['post_cat'] || @$_GET['cat'] == 0 )
 
  print "\n</li>\n";
 } // while ($row=mysql_fetch_array($catResults))
 
?>
</ul>
 
</body>
</html>

Open in new window

Author

Commented:
hi

first of all thanks and i really appriciate your help.

the code works, but i would appriciate if you add an explanation to it, because i want to learn from the situation and not just to fix the problem.

best regards

ron

Author

Commented:
ho

and i forgot this:

how do i make them start close and not open?

best regards

ron
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>but i would appriciate if you add an explanation to it,

the main problem was that you had the following 2 lines before the loop:
$postsRS="SELECT * FROM posts WHERE post_cat=" .$_GET['cat'];
$postsResults=mysql_query($postsRS);

that meant that you queried the posts table only once, with the parameter passed,
however, looking at the loop and the if() condition, I understood that you eventually wanted to get all the posts for all the categories.

so, you have to query that table, inside the if() which checks if you want to print out any posts details, based on the category your are currently looping ($row[cat_id]) instead:

  $postsRS="SELECT * FROM posts WHERE post_cat= " . $row['cat_id'];
  $postsResults=mysql_query($postsRS);
 
the rest of the modifications is merely cosmetics, and to make the code easier to read (at least to me)
 mixing the html and php code too much will not make it easy...




>how do i make them start close and not open?
well, when I tested it, with no cat= in the command argument, they were all closed.
with cat=0 they are all open, according to the if statement:
  if(@$_GET['cat'] == $row['cat_id'] || @$_GET['cat'] == 0 )



Author

Commented:
thanks thanks thanks!!!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.