Results 1 to 21 of 21

Thread: Problem looping through database.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448

    Problem looping through database.

    Im having problems looping through my database. Im useing fast template and i cant get it to loop through and add more than one item. here is what i have...
    PHP Code:
            $menu "SELECT * FROM menu_left ORDER BY menu_order";

              if (
    $complete != 1
              
    $menu .= " LIMIT 20"

              
    $menu_left mysql_query($menu); 
              while (
    $menurow mysql_fetch_array($menu_left)) {
               if (
    $menurow['menu_show'] == 1) {
                
    $menu_link $menurow['menu_link'];
                
    $menu_name $menurow['menu_name'];
               } else {
                echo 
    "";
               };
              }

    $tpl->assign(array(
                
    'menu_link'              =>  $menu_link,
                
    'menu_name'              =>  $menu_name,
            )
        ); 
    then i have in the template...
    Code:
    <a href="{menu_link}.php">{menu_name}</a>
    can you help me with this one?

  2. #2
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    that is becasue every time you loop throught the db it resets teh names

    if ($menurow['menu_show'] == 1) {
    $menu_link = $menurow['menu_link'];
    $menu_name = $menurow['menu_name'];
    } else {
    echo "";
    };

    that is saying if menu_show equals 1 then menu_link equal menurow[], so it comes to the next one and if it equals to 1 it will overwrite?

    so you hav eto put them into the array first like so

    if ($menurow['menu_show'] == 1) {
    $menu_link[] = $menurow['menu_link'];
    $menu_name[] = $menurow['menu_name'];
    } else {
    echo "";
    };

    that is the only way to do it if you want more than 1
    Last edited by phpman; Feb 2nd, 2003 at 01:13 PM.

  3. #3
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    actually you can do this
    PHP Code:
    $menu "SELECT * FROM menu_left ORDER BY menu_order";

              if (
    $complete != 1
              
    $menu .= " LIMIT 20"

              
    $menu_left mysql_query($menu); 
              while (
    $menurow mysql_fetch_array($menu_left)) {
                   if (
    $menurow['menu_show'] == 1) {
                       
    $tpl->assign(array(
                       
    'menu_link' =>  $menurow['menu_link'],
                        
    'menu_name' =>  $menurow['menu_name']));

                   } 
              } 

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    thats not printing anything out to where there was one item from the database.

  5. #5
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    do you get any errors?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    nope not one.

  7. #7
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    I don't see anything wrong with it. what is the assigns function?

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Code:
    $menu = "SELECT * FROM menu_left ORDER BY menu_order";
    
    if ($complete != 1) 
        $menu .= " LIMIT 20"; 
    
        $menu_left = mysql_query($menu) or die(mysql_error());
        while ($menurow = mysql_fetch_array($menu_left)) {
            if ($menurow['menu_show'] == 1) {
                $tpl->assign(array(
                     'menu_link' =>  $menurow['menu_link'],
                     'menu_name' =>  $menurow['menu_name']));
    
            } 
         }
    Always throw that line on there. PHP will not always report a MySQL error.

    I always do this to check to see if the SQL worked properly:

    Code:
    if ($menurow = mysql_fetch_array($menu_left)) {
        do {
            if ($menurow['menu_show'] == 1) {
                $tpl->assign(array(
                     'menu_link' =>  $menurow['menu_link'],
                     'menu_name' =>  $menurow['menu_name']));
    
            } 
         }while ($menurow = mysql_fetch_array($menu_left));
    } else {
        echo "No records found!";
    }
    Otherwise the problem lies in whatever the assign() function does, as phpman has asked.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    ok that did work either, hobo. how can i just test to see if we can get the loop to work without the database. then put in the databsae and get that working.

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Muk108
    ok that did work either, hobo. how can i just test to see if we can get the loop to work without the database. then put in the databsae and get that working.
    What do you mean it doesn't work? Do you get an error? Do you get the "No records found!" message? What do you get? Nothing? Simply saying it doesn't work isn't going to help us find a solution.

    And You can't get the loop to work without the database. The loop runs based on whether or not you have stuff from the database.

    If the code I gave you gives no errors, then the problem must lie in whatever assign() is doing.

    Run this code. See if you get output:

    Code:
    $menu = "SELECT * FROM menu_left ORDER BY menu_order";
    
    if ($complete != 1) 
        $menu .= " LIMIT 20"; 
    
        $menu_left = mysql_query($menu) or die(mysql_error());
        while ($menurow = mysql_fetch_array($menu_left)) {
            if ($menurow['menu_show'] == 1) {
                /*$tpl->assign(array(
                     'menu_link' =>  $menurow['menu_link'],
                     'menu_name' =>  $menurow['menu_name']));*/
                
                echo "<b>Test</b>: " . $menurow['menu_name'] . "<br>\n";
    
            } 
         }
    If you do not get any output, then it is a problem with the database. If you do get output, then it is a problem with your assign() function.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    try this and see what it says.

    PHP Code:
    $menu "SELECT * FROM menu_left ORDER BY menu_order";

    if (
    $complete != 1) {
        
    $menu .= " LIMIT 20"
    }
    $menu_left mysql_query($menu); 
    if (
    mysql_num_rows($menu_left)){
        while (
    $menurow mysql_fetch_array($menu_left)) {
            if (
    $menurow["menu_show"] == "1") {
                
    $tpl->assign(array("menu_link" =>$menurow["menu_link"], "menu_name" =>$menurow["menu_name"]));
            } 
        }
    } else{
        echo 
    "nothing found in db";


  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    that only returned one resault, as did the last 2.

  13. #13
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Muk108
    that only returned one resault, as did the last 2.
    How many results should there be?

    You're messing something up, dude. We can't tell you the magical password to get your script to work. You need to step through it and figure out what's going on. We can't do that for you.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    could it be possbile that ther is only one row that has a $menurow["menu_show"] of 1 in the db???

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    It only displays the last one. There should be 8 results. do you know what im doing wrong that it only displayes the last item?

  16. #16
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    I believe that assigns() will only do 2.

    what is the assign() function? and what is the code you have now?

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    this is what it said on the site....
    The method assign() assigns values for variables. In order for a variable in a template to be interpolated it must be assigned. There are two forms which have some important differences. The simple form, is to accept an array and copy all the key/value pairs into an array in FastTemplate. There is only one array in FastTemplate, so assigning a value for the same key will overwrite that key.

    $tpl->assign(TITLE => "king kong");
    $tpl->assign(TITLE => "godzilla"); // overwrites "king kong"
    this is what i currently have as the second assign()...

    PHP Code:
        $tpl->assign(
            array(
                
    'title'                  =>  $title_text,
                
    'news_title_text'        =>  $news_title_text,
                 
    'news_message_text'      =>  $news_message_text,
                 
    'news_submitter_text'    =>  $news_submitter_text,
                 
    'news_date_text'         =>  $news_date_text,
                
    'message_area'           =>  $message_area,)
            ); 
    then i have what has been sudjested.

  18. #18
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    Originally posted by Muk108
    this is what it said on the site....

    The method assign() assigns values for variables. In order for a variable in a template to be interpolated it must be assigned. There are two forms which have some important differences. The simple form, is to accept an array and copy all the key/value pairs into an array in FastTemplate. There is only one array in FastTemplate, so assigning a value for the same key will overwrite that key.

    $tpl->assign(TITLE => "king kong");
    $tpl->assign(TITLE => "godzilla"); // overwrites "king kong"
    did you just not read what you just said???????

    There is only one array in FastTemplate, so assigning a value for the same key will overwrite that key.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    sorry, i miss read what you said then. i try and read things to fast. what template do you sedjest i can use then to do what im trying to do?

  20. #20
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    lol, hahahahaha
    My evil laugh has a squeak in it.

    kristopherwilson.com

  21. #21
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    Originally posted by Muk108
    sorry, i miss read what you said then. i try and read things to fast. what template do you sedjest i can use then to do what im trying to do?
    make your own, a good learning experience

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width