Results 1 to 19 of 19

Thread: Loop through database...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448

    Loop through database...

    Ok im back again and i am still trying to loop through my database in a template. is this how i would loop through it? i need to loop through the ID of the item first then while its still doing that print out the data?

    so

    loop {

    $dbid

    loop {

    $dbtext

    }

    }

    or something like that? do you think you could help me put this in php?

  2. #2
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    loop through the id first?? huh you can't do that.

    just loop through the whole table do this

    PHP Code:
    $query=("select * from table")

    while (
    $rowmusql_fetch_array($query){
    echo 
    $row["id"];
    echo 
    $row["field1"];

    that will loop through the whole db and display every ID and FIELD1

    so what are you having trouble with? you said a template?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    well im trying to use a template engine like fasttemplate so i can have someone do the html and stuff and i just work on the php part. but whenever i do it, it only displayes one item from the database.

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Muk108
    well im trying to use a template engine like fasttemplate so i can have someone do the html and stuff and i just work on the php part. but whenever i do it, it only displayes one item from the database.
    You'd probably have to post your code before we can find the bug...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    PHP Code:
    <?
    include("db.php");
    include("class.FastTemplate.php3");
    if ($complete != 1) 
        $news = mysql_query($query); 
        while ($row = mysql_fetch_array($news)) {
          $title_text = $row['main_title'];
        };
        //
        
              $menu = "SELECT * FROM menu_left ORDER BY menu_order";

              if ($complete != 1) 
              $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 "";
               };
              }
              
              //
            $newsquery = "SELECT * FROM news";

        if ($complete != 1) 
            $news = mysql_query($newsquery); 
            while ($row = mysql_fetch_array($news)) {
             $news_title_text = $row['news_title'];
             $news_message_text = $row['news_message'];
             $news_submitter_text = $row['news_submitter'];
             $news_date_text = $row['news_date'];
            };
            //

    $tpl = new FastTemplate("./templates");
      $tpl->define(array(
            'main'                   => "index.tpl",
          )
      );
      //
      
        $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,
                'menu_link'              =>  $menu_link,
                'menu_name'              =>  $menu_name,
                'message_area'           =>  $message_area,)
            );
              $tpl->parse('MAIN', array("main"));

    $tpl->FastPrint();
    //$tpl->showDebugInfo();
    exit;
    ?>
    thats what i have, i have been working with this for a long time, hope you can help me.

  6. #6
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    is this it?
    PHP Code:
    while ($row mysql_fetch_array($news)) {
             
    $news_title_text $row['news_title'];
             
    $news_message_text $row['news_message'];
             
    $news_submitter_text $row['news_submitter'];
             
    $news_date_text $row['news_date'];
            }; 
    that will only get the last info from teh db, so the last id will only be sent. I thought we fixed this a while ago.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    no, i dont think we did, or we did and when i tryed putting it in it didnt work. could you help me with this one?

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This is a problem right here:

    Code:
    while ($menurow == mysql_fetch_array($menu_left)) {
    Should only be one = sign.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    that doesnt seem to matter, i still only get one resault printed out on the page.

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Muk108
    that doesnt seem to matter, i still only get one resault printed out on the page.
    It may not fix your problem, but yes, it does matter, as that's not how you do it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    ok well, thank you. i have tryed a for loop at
    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,
                
    'menu_link'              =>  $menu_link,
                
    'menu_name'              =>  $menu_name,
                
    'message_area'           =>  $message_area,)
            ); 
    this point. but still no luck.

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I think phpman hit the nail on the head. You need to do something like this:

    Code:
    <?php
        include("db.php");
        include("class.FastTemplate.php3");
    
        if ($complete != 1) 
            $news = mysql_query($query); 
        while ($row = mysql_fetch_array($news)) {
            $title_text[] = $row['main_title'];
        }
        //
            
        $menu = "SELECT * FROM menu_left ORDER BY menu_order";
    
        if ($complete != 1) 
           $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 "";
            }
        }
                  
        $newsquery = "SELECT * FROM news";
    
        if ($complete != 1) 
            $news = mysql_query($newsquery); 
                
        while ($row = mysql_fetch_array($news)) {
            $news_title_text[] = $row['news_title'];
            $news_message_text[] = $row['news_message'];
            $news_submitter_text[] = $row['news_submitter'];
            $news_date_text[] = $row['news_date'];
        }
    
        $tpl = new FastTemplate("./templates");
        $tpl->define(array('main' => "index.tpl",)); 
        // ^-- what's that comma doing in there?
    
            
        for ($i = 0; $i < count($title_text); $i++) {
            $tpl->assign(
                array(
                'title'                  =>  $title_text[$i],
                'news_title_text'        =>  $news_title_text[$i],
                'news_message_text'      =>  $news_message_text[$i],
                'news_submitter_text'    =>  $news_submitter_text[$i],
                'news_date_text'         =>  $news_date_text[$i],
                'menu_link'              =>  $menu_link[$i],
                'menu_name'              =>  $menu_name[$i],
                'message_area'           =>  $message_area[$i],)
                //^--what's up with that last comma?
            );
    
            $tpl->parse('MAIN', array("main"));
    
            $tpl->FastPrint();
            //$tpl->showDebugInfo();
        }
    
        exit;
    ?>
    Give it a try. I believe it will work
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    exactly, Hobo.

    see that is what you needed to do, Muk108. jsu tlike this thread where you just left it hanging

    http://www.vbforums.com/showthread.p...hreadid=228334

    you never said it didn't work and we told you what the problem was. and again you ask the same question and got the same answer.

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    see that is what you needed to do, Muk108. jsu tlike this thread where you just left it hanging

    http://www.vbforums.com/showthread.p...hreadid=228334

    you never said it didn't work and we told you what the problem was. and again you ask the same question and got the same answer.
    I guess when you don't like the answer, you can ask again later and see if you get a different one.

    Sadly, for him, this is the answer.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    im sorry i havent replyed, i was buisy the last two days. and when i execute that i go this error....

    Code:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\HTTP\www\intra\index.php on line 71
    
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\HTTP\www\intra\index.php on line 81
    and it just loops through that locking up my server.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    ok i got the page to load, but now for ME, it doesnt want to loop through and print it out again...

  17. #17
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Muk108
    ok i got the page to load, but now for ME, it doesnt want to loop through and print it out again...
    I don't know what to tell you anymore. Try finding some basic tutorials first. Or maybe programming concepts.

    Not to overuse a cliche, but we can only lead you to water...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    i understand i have been looking at tutorials and havnt found anything. ill keep looking and when i find it ill post it. thanks for all your guys help.

  19. #19
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    my guess is that you messed up the query when you got the mysql_fetch_arrary errors.

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