Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    having problems echoing $Row = mysql_fetch_array($Result); with a for loop

    PHP Code:
    <?php
        $link 
    mysql_connect($Host$User$Password);  
        
    mysql_select_db($DBName$link);

        
    $Query "SELECT * FROM Categories";
        
    $Result mysql_query($Query);
        
    $num_rows mysql_num_rows($Result);
        
    $Row mysql_fetch_array($Result);
        
        if (
    $_GET['CatID'] == Company) {
            
    $col "Company_title";
            
    $nav_title "images/company-title.jpg";
        }
        if (
    $_GET['CatID'] == Hardware) {
            
    $col "Category";
            
    $nav_title "images/hardware-title.jpg";
        }
    ?>
    PHP Code:
    <?php
        
    echo '<ul>'
        for (
    $i 1$i <= $num_rows-1$i++) {
             echo 
    '<li>'.$Row[$col].'</li>';
        }
        
    mysql_close ($Link);
        echo 
    '</ul>';
    ?>
    Well, I'm having a hard time printing out the $Results using the for loop. I have no clue where to beginning. $col == Category which is the Column name. This works using a while loop... Any help would be greatly appreciated.

    Edit:
    It only prints out the first row which is Cases...

    -peace
    Last edited by DiarYofaMadmaN; 05-08-2004 at 10:01 PM.

  • #2
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahh nvm I figured it out, I had to put $Row = mysql_fetch_array($Result); inside the loop.

    -peace

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    However, there is a better way to do this:

    PHP Code:
    while($row mysql_fetch_assoc($Result)){
        echo 
    '<li>'.$row[$col].'</li>'

    This is the best way to do this, and it cuts out the need for mysql_num_rows(). Also note i am using mysql_fetch_assoc(), not fetch_array(), as assoc only returns the associative array (array with the names)

  • #4
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by missing-score
    However, there is a better way to do this:

    PHP Code:
    while($row mysql_fetch_assoc($Result)){
        echo 
    '<li>'.$row[$col].'</li>'

    This is the best way to do this, and it cuts out the need for mysql_num_rows(). Also note i am using mysql_fetch_assoc(), not fetch_array(), as assoc only returns the associative array (array with the names)

    Hey! Thanks for that tip, I really need to learn these different mysql_ functions better.

    One more question, is it possible to get the last value in mysql_fetch_assoc($Result)?

    -Rich

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how do you mean exactly? do you mean get values of the last row returned?

  • #6
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by missing-score
    how do you mean exactly? do you mean get values of the last row returned?
    Firstly, the reason why I used a for loop was because(get ready this may be kinda confusing for me to explain). Anyways, The reason why I used a for loop is because the way I have my visual design of my page. I would have used CSS but I don't know it to well so I used a table with three cells. I spliced up my image into three images. The first for the title, second for the back of the unordered list, and the third image for the bottom. For example of what I'm talking about go to: http://www.maxforcepc.com/itemlist.php?CatID=2 If you look closely to the last item in my list it says "Troubleshooting
    ".

    Troubleshooting should be echoed out into the last cell(In that big gap in the bottom) to look uniformed. So, the for loop loops in this case from 1 through 2 and prints out Networking and Upgrades. Then in the third cell I need to echo out the last result in the mysql array. With the while loop it's printing through the full list. I do relize I can edit my sql query but I can't do it that way because I'm changing the navigation look with the url.

    -Rich

  • #7
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    You could have used mysql_result as well, which is how I do it if I need to use a for loop..

    PHP Code:
    for( $i 0$i mysql_num_rows$result ); $i++ ){
      echo 
    mysql_result$result$i'field' );

    Omnis mico antequam dominus Spookster!

  • #8
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    take a look at this:

    this is causing errors on my site at http://www.moneybux.biz. can someone tell me a solution?:
    $row = mysql_fetch_array($result);


  •  

    Posting Permissions

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