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 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Not displaying the first item in an array

    Hello,

    I wrote this code that displays a table with matching words. When I run the code all the items included in the database appear but the first one. I don't why. How can I display all the items including the first one?

    PHP Code:
    <?php 
    include("config.php");

                
    // this grab the words of the lesson
                
    $data2 mysql_query ("SET NAMES 'utf8'");    
                
    $data2 mysql_query("SELECT * FROM words WHERE lesson_number='$lesson'") ;  
                
    $info2 mysql_fetch_array($data2);
                
                if (empty(
    $info2['french'])) {}  // if no words don't print anything
                
    else {
                 echo 
    " <div class='vocabulary' ><h1>Vocabulary</h1>  
                             
                             <table><tr><th>french</th> <th>English</th></tr>"
    ; }
            
                
    // this displays all the words of the lesson   
                
    while($info2 mysql_fetch_array($data2)){
                
                echo 
    "<tr> <td> " $info2['french'] . "</td> <td> ".$info2['english']."</td></tr>"
                }  
                
                echo 
    "</table> </div>";
                        
    ?>
    Thanks a lot!
    Last edited by cast_no_shadow; 09-13-2012 at 10:11 AM.

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    This block of code appears to do nothing, I think you can delete it along with the last curly brace that goes with it.

    PHP Code:
    if (empty($info2['french'])) {}  // if no words don't print anything
                
    else { 
    I don't see why the while loop would skip the 1st row, it looks OK to me.





    ---
    Last edited by Len Whistler; 09-13-2012 at 05:36 PM.
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    cast_no_shadow (09-21-2012)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    PHP Code:
                $info2 mysql_fetch_array($data2);
    // . . .
                
    while($info2 mysql_fetch_array($data2)){ 
    You've already fetched the first row into $info2. Calling it in the while now starts at the second record.
    If you need to keep this if branch check (invert it with a ! and remove the empty braces instead of using an else), then change the while loop into a do/while loop and it will function properly. Alternatively use mysql_data_seek to change your record. Personally I'd alter the query since you don't need to use that if/else branch there at all; simply add a check on that field for either not an empty string (if its an empty string), or not is null check if its null.

  • Users who have thanked Fou-Lu for this post:

    cast_no_shadow (09-21-2012)


  •  

    Posting Permissions

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