#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Problem with Div's


    I'm trying to create div's dynamically, say every 5 rows. I can do this, but not when I add the rest of the code i'm using (menu script). My code is below,

    PHP Code:

    function display_children($parent$level) {     $result mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" $parent);

    $count 0;          

    echo 
    '<div>';     
    echo 
    "<ul id='menu'>";          
    while (
    false !== ($row mysql_fetch_array($result))) {                                             if ($row['Count'] > 0) {             
    echo 
    "<li><a href='" $row['link'] . "'>" $row['label'] . "</a>";             
    display_children($row['id'], $level 1);             
    echo 
    "</li>";        
     } elseif (
    $row['Count']==0) {             
    echo 
    "<li><a href='" $row['link'] . "'>" $row['label'] . "</a></li>";
             } else;
         }           

    ++
    $count; if (($count 5) == 0) { 
        echo 
    "</ul>";     echo "</div><div>";
       }      
    }               

    display_children(01); 
    Please help can't work it out,
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,617
    Rep Power
    595
    Please edit your post and enclose your code in [ PHP ] tags. See the sticky at the top of this forum.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,617
    Rep Power
    595
    1) Don't use the deprecated mysql extensions. Switch to PDO.
    2) You don't say specifically what the problem is but I can see that you end </ul>, end </div> and start <div> but you are not starting <ul> again.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by gw1500se
    1) Don't use the deprecated mysql extensions. Switch to PDO.
    2) You don't say specifically what the problem is but I can see that you end </ul>, end </div> and start <div> but you are not starting <ul> again.


    I'm trying to achieve having say 5 records returned for every <Div>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,617
    Rep Power
    595
    So what are you getting and what is wrong?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by gw1500se
    So what are you getting and what is wrong?
    Each div is a page in a jquery flip book. I want to use a database driven menu system for the index pages, in the flip book.

    this basic code works and returns 2 records per div

    PHP Code:
    $con = mysql_connect("localhost","user","pass");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("my_tbl", $con);

    $result = mysql_query("SELECT * FROM poop");

    $count = 0;


    echo '<div style="border: thin solid #333;">'; 
    while (false !== ($row = mysql_fetch_array($result)))
      {
          
      //echo '<div style="border: thin solid #333;">'; 
      echo $row['id'] . " " . $row['text']; 
      ?>
      <a href="<?php echo $row['link']; ?>"><?php echo $row['text']; ?></a>
      
      <?php
      
    echo "<br />";
      
      
       ++
    $count;
      if ((
    $count 2) == 0) {
        echo 
    '</div><div style="border: thin solid #333;">';
        
      }
     }

    mysql_close($con);

    but when I add the menu it the divs get broken ( current attempt)

    PHP Code:

    function display_children($parent$level) {
        
    $result mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" $parent);
        
        
        
    $count 0;
        
    $count_b 0;

        ++
    $count_b;
    if ((
    $count_b 1) == 1) {
        
        echo 
    "<div>";    
            
        }
            
        while (
    false !== ($row mysql_fetch_array($result))) {
            
            echo 
    "<ul>";
            if (
    $row['Count'] > 0) {
                echo 
    "<li><a href='" $row['link'] . "'>" $row['label'] . "</a>";
                
    display_children($row['id'], $level 1);
                echo 
    "</li>";
                } elseif (
    $row['Count']==0) {
                echo 
    "<li><a href='" $row['link'] . "'>" $row['label'] . "</a></li>";
            } else;
            
            echo 
    "</ul>";

      }
                    
        ++
    $count;
    if ((
    $count 2) == 0) {
        
        echo 
    "</div>";    
            
        }    
        
    }

    display_children(01);

    mysql_close($con); 
    returns 5 pages, first has 1 record returned, page 2 has 5 records returned and the rest one each.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,617
    Rep Power
    595
    Using recursive routines can be tricky. It appears that you are generating divs within divs. Is that what you want? What does the resulting HTML source look like?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by gw1500se
    Using recursive routines can be tricky. It appears that you are generating divs within divs. Is that what you want? What does the resulting HTML source look like?

    HTML source is

    <ul><li><a href='#home'>Home</a></li></ul><ul><li><a href='#code'>Code</a><ul><li><a href='#php'>PHP</a></li></ul><ul><li><a href='#css'>Css</a></li></ul><ul><li><a href='#scripts'>Scripts</a></li></ul><ul><li><a href='#help'>Help</a></li></ul></li></ul><ul><li><a href='#contact'>Contact</a></li></ul><ul><li><a href='#archive'>Archive</a></li></ul><ul><li><a href='#snippet'>Snippet</a></li></ul>
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,617
    Rep Power
    595
    No div's are being output at all. Obviously the problem is your 'if' blocks that are intended to generate them. Your 'count' variables are pretty much useless. You can eliminate them, including the 'if' blocks. They will always be 1. Its my guess that you really don't want to reinitialize them to 0 each time the function is called. You probably want to initialize them to 0 outside the function and then pass them to each call to be incremented and tested.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Using recursive routines can be tricky, your so right. I think I'll try a different approach, and display the content in a different manor.

    Thanks for helping, have learned a lot from you, will help others in this forum if I can.

    Kevin.

IMN logo majestic logo threadwatch logo seochat tools logo