Two(or more) records per row
Moderator: General Moderators
Two(or more) records per row
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!!
can anyone help me eith that??
thanks a lot!!
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..
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..
Everything, as you likely know:
Asume we have as you said in your example, a 10*X 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.
Code: Select all
select * from tableCode: Select all
select name, password, email from tableHope that is what you are looking for.
-
penguinboy
- Forum Contributor
- Posts: 171
- Joined: Thu Nov 07, 2002 11:25 am
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;
}
}
}
?>
Last edited by penguinboy on Thu Dec 04, 2003 9:47 am, edited 1 time in total.
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.penguinboy wrote:Something like that??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; } } } ?>
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>';
?>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; } } } ?>