Page 1 of 1

Two(or more) records per row

Posted: Thu Dec 04, 2003 8:32 am
by toychief
Is there a way to get the result of my recordset to display like 2 or more on the same row, instead of the ordinary 1 row/1 recordset loop ?? Like may be 1 record in every column of my table (2 or 3 max..)

can anyone help me eith that??
thanks a lot!!

Posted: Thu Dec 04, 2003 9:30 am
by kendall
toychief,

i'd like to help you on this but you probably need to be more detailed...try using some examples to better explain your self or lets see some code

Kendall

Posted: Thu Dec 04, 2003 9:36 am
by toychief
Ok i'll try. (but as you can see, english is not my primary language..)

What i want to do, is to build a table of let say 2 column wide and X row of height.

My resultset contains 10 records. So i want the first record to be put in the cell A-1 (top-left) then the second one, in the cell A-2 (top-right) and then again, the third one in the cell B-1 (second row-left) etc.. etc...
Am i clearer now??

Thanks..

Posted: Thu Dec 04, 2003 9:44 am
by JAM
Everything, as you likely know:

Code: Select all

select * from table
Asume we have as you said in your example, a 10*X table:

Code: Select all

select name, password, email from table
...would then retrieve those 3 fields only from that table, 'looping' through all the X rows available.

Hope that is what you are looking for.

Posted: Thu Dec 04, 2003 9:46 am
by penguinboy

Code: Select all

<?php
$sql = your query
$x=0;
while($row = mysql_fetch_array($sql))
{
   if($x==0)
  {
     print '<tr><td>'.$row[0].'</td>';
     $x++;
  }
  else{
     if($x==1)
    {
       print '<td>'.$row[0].'</td></tr>';
       $x=0;
    }
  }
}
?>
Something like that??

Posted: Thu Dec 04, 2003 9:47 am
by toychief
yes!!! thanks a lot!!!

Posted: Thu Dec 04, 2003 9:59 am
by infolock
penguinboy wrote:

Code: Select all

<?php
$sql = your query
$x=0;
while($row = mysql_fetch_array($sql))
{
   if($x==0)
  {
     print '<tr><td>'.$row[0].'</td>';
     $x++;
  }
  else{
     if($x==1)
    {
       print '<td>'.$row[0].'</td></tr>';
       $x=0;
    }
  }
}
?>
Something like that??
i'm unsure if this code works like it should as you are just calling the same row twice, and the exact same value as well.

maybe a better solution would be

Code: Select all

<?php
$email=array();
$password=array();
$name=array();
$sql = "select name, password, email from table";
$result = mysql_query($sql);
$row=mysql_fetch_array();
while($row = mysql_fetch_array($result))
{
   $name[]=$row['name'];
   $password[]=$row['password'];
   $email[]=$row['email'];
}

/********************************************************

 depending on how many rows you want to show, you can change the mysql_num_rows($sql) to however many results you want ( ie : $x < 5; )

********************************************************/
echo '<table>';
   for($x=0; $x<mysql_num_rows($result); $x++)
   {
      echo '<tr><td>'.$name[$x].'</td>
              <td>'.$email[$x].'</td>
              <td>'.$password[$x].'</td></tr>';
  }
echo '</table>';
?>

Posted: Thu Dec 04, 2003 12:29 pm
by Chambrln
This code would work just fine. Doing it infolock's way would also work but requires php to do 2 loops through the data when only one is absolutely necessary.

Code: Select all

<?php
$sql = your query 
$x=0; 
while($row = mysql_fetch_array($sql)) 
{ 
   if($x==0) 
  { 
     print '<tr><td>'.$row[0].'</td>'; 
     $x++; 
  } 
  else{ 
     if($x==1) 
    { 
       print '<td>'.$row[0].'</td></tr>'; 
       $x=0; 
    } 
  } 
} 
?>