looping problem

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

LVL 1
derridaAsked:
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.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>$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" ;
}

0
derridaAuthor 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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
can you try without  the <p>, ie use some <div></div> instead? I fear that the <p> disrupts the <li> stuff a bit.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

derridaAuthor 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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
derridaAuthor 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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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...
0
derridaAuthor 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
0
derridaAuthor Commented:
hi

i hope you or someone else can keep helping me.


best regards

ron
0
derridaAuthor 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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

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
derridaAuthor 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
0
derridaAuthor Commented:
ho

and i forgot this:

how do i make them start close and not open?

best regards

ron
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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 )



0
derridaAuthor Commented:
thanks thanks thanks!!!
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.