Two(or more) records per row

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
toychief
Forum Newbie
Posts: 3
Joined: Thu Dec 04, 2003 8:32 am

Two(or more) records per row

Post 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!!
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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
toychief
Forum Newbie
Posts: 3
Joined: Thu Dec 04, 2003 8:32 am

Post 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..
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post 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??
Last edited by penguinboy on Thu Dec 04, 2003 9:47 am, edited 1 time in total.
toychief
Forum Newbie
Posts: 3
Joined: Thu Dec 04, 2003 8:32 am

Post by toychief »

yes!!! thanks a lot!!!
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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>';
?>
Chambrln
Forum Commoner
Posts: 43
Joined: Tue Dec 02, 2003 10:45 am
Location: Oregon

Post 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; 
    } 
  } 
} 
?>
Post Reply