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 2 of 2
  1. #1
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts

    mysql_fetch_assoc() && $i<10 (for loop)

    PHP Code:
    for($i=0$utopicArray=mysql_fetch_assoc($utopicQuery) && $i<10$i++){
        echo 
    '<li><a href="/?utopic='.$utopicArray['id'].'>'.$utopicArray['topic'].'</a></li>\n';

    I want to echo the content of the array but only for 10 rows. The problem is that the $utopic[] variables don't output anything with && $i<10 in the statement. Why is this and how might I go about fixing it?
    Last edited by djh101; 07-17-2011 at 08:28 PM.
    "Yeah science!"
    Online Science Tools

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    for($i=0; $utopicArray=mysql_fetch_assoc($utopicQuery) and $i<10; $i++){
        echo '<li><a href="/?utopic='.$utopicArray['id'].'>'.$utopicArray['topic'].'</a></li>\n';
    }
    should work.

    In your case, the row returned by the function mysql_fetch_assoc() will get ANDed with the result of the immediate comparison operation. At the end, $utopicArray will get a true/false. Check the order of precedence at http://php.net/manual/en/language.op...precedence.php

    PS: That's why we always use or die() and not || die()
    Last edited by abduraooft; 07-18-2011 at 07:09 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    djh101 (07-17-2011)


  •  

    Posting Permissions

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