Loop Qoustion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ihabgr
Forum Newbie
Posts: 13
Joined: Fri Jul 11, 2008 11:26 am

Loop Qoustion

Post by ihabgr »

Hello I did this script and my problem is I have 13 entries in the data base but it show me only 12

Code: Select all

<?php
include "contact.php";
 
 
$query  = "SELECT * FROM staff  ";
$result = mysql_query($query);
$tbl= "<table align = center ><tr>";
 
 
 
$AA=0;
 
while
 
(
 
    
$row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    $fname=$row['firstname'];
     $lname=$row['lastname'];
    $phone =$row['ext'];
    $email =$row['emil'];
    $photo =$row['pic'];
    $title=$row['title'];
 
    
 if ($AA<6) {
    $tbl.="<td><img src=$photo width=98 height=98 <br> <span class=style2> $fname $lname</span><br><span class=style5>$title<br> Ext.$phone $AA</td>";
  $AA++;
   
 }
 else{ 
 $tbl.="</tr><tr>";
 $AA=0;
 
 
}
         
} 
echo $tbl
?>
Last edited by Benjamin on Thu Apr 30, 2009 2:52 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Loop Qoustion

Post by requinix »

That if with the $AA is incorrect. If $AA is 6 then it won't add the current $row data to $tr.

You need to add onto $tr every time, but only add the </tr> if $AA is 6.
ihabgr
Forum Newbie
Posts: 13
Joined: Fri Jul 11, 2008 11:26 am

Re: Loop Qoustion

Post by ihabgr »

I'm sorry can you explain in code
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Loop Qoustion

Post by requinix »

No, because then you'll copy/paste it into your stuff and end up not having learned a thing.

Code: Select all

if ($AA<6) {
    $tbl.="<td><img src=$photo width=98 height=98 <br> <span class=style2> $fname $lname</span><br><span class=style5>$title<br> Ext.$phone $AA</td>";
  $AA++;
   
 }
 else{
 $tbl.="</tr><tr>";
 $AA=0;
 
 
}
What happens if $AA is less than six? You have that new <td> to add: when does it get added to $tbl and when does it not? When do you want it to get added?
ihabgr
Forum Newbie
Posts: 13
Joined: Fri Jul 11, 2008 11:26 am

Re: Loop Qoustion

Post by ihabgr »

I'm sorry I don't understand, give me example Please
jam57
Forum Newbie
Posts: 7
Joined: Fri May 01, 2009 12:58 pm

Re: Loop Qoustion

Post by jam57 »

the trouble is that once $AA == 6, the <td> etc is not added, but the </tr> is. so look at it like this:

AA == 0
first db result added
AA == 1
second db result added
etc until
AA == 6
6th db result NOT added
</tr><tr> added
AA == 0
7th db result added

so you see because the if statement doesn't add the row when it adds the </tr><tr> you effectively skip a row
Post Reply