Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-13-2012, 08:02 AM   PM User | #1
cast_no_shadow
New Coder

 
Join Date: Aug 2012
Posts: 44
Thanks: 26
Thanked 0 Times in 0 Posts
cast_no_shadow is an unknown quantity at this point
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..
cast_no_shadow is offline   Reply With Quote
Old 09-13-2012, 05:31 PM   PM User | #2
Len Whistler
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
Len Whistler is on a distinguished road
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.





---
__________________
Leonard Whistler

Last edited by Len Whistler; 09-13-2012 at 05:36 PM..
Len Whistler is offline   Reply With Quote
Users who have thanked Len Whistler for this post:
cast_no_shadow (09-21-2012)
Old 09-13-2012, 05:57 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
cast_no_shadow (09-21-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:49 PM.


Advertisement
Log in to turn off these ads.