?
Solved

looping problem

Posted on 2008-01-29
15
Medium Priority
?
261 Views
Last Modified: 2008-02-03
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

0
Comment
Question by:derrida
  • 9
  • 6
15 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20767418
>$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
 
LVL 1

Author Comment

by:derrida
ID: 20767513
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20767631
can you try without  the <p>, ie use some <div></div> instead? I fear that the <p> disrupts the <li> stuff a bit.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:derrida
ID: 20768143
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20768190
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
 
LVL 1

Author Comment

by:derrida
ID: 20768304
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20768371
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
 
LVL 1

Author Comment

by:derrida
ID: 20768475
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
 
LVL 1

Author Comment

by:derrida
ID: 20777192
hi

i hope you or someone else can keep helping me.


best regards

ron
0
 
LVL 1

Author Comment

by:derrida
ID: 20803606
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20805813
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
 
LVL 1

Author Comment

by:derrida
ID: 20807990
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
 
LVL 1

Author Comment

by:derrida
ID: 20808004
ho

and i forgot this:

how do i make them start close and not open?

best regards

ron
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20808233
>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
 
LVL 1

Author Comment

by:derrida
ID: 20808590
thanks thanks thanks!!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question