CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Not displaying the first item in an array (http://www.codingforums.com/showthread.php?t=273001)

cast_no_shadow 09-13-2012 08:02 AM

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!

Len Whistler 09-13-2012 05:31 PM

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.





---

Fou-Lu 09-13-2012 05:57 PM

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.


All times are GMT +1. The time now is 11:19 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.