PDA

View Full Version : how do you display three records then start a new row?


DrewM
04-03-2006, 06:01 PM
I'm doin a script and I want 3 resaults and then start a new row can some one tell me what code to use?

Sean S
04-03-2006, 08:47 PM
do you want all the first three results in one row? so like

---------------------------
| result_1 result_2 result_3 |
---------------------------
| result_4 result_5 result_6 |
---------------------------

DrewM
04-03-2006, 09:07 PM
yes that is.

Sean S
04-03-2006, 09:44 PM
Ok I'm no php professional, but this is what I did once for a client of mine and it seemed to work. Basically it displays the results upto 3 columns per row or 3 results per row, once the 3 results has been displayed, it goes to the next row.

$number, this variable controls the total amount of data you want to display.

also you have to change this part
SELECT id, image, link, category FROM $table to whatever your table is setup to.

and last, change this the same way,
$row[link]


<?php
$username="database_username";
$password="database_password";
$database="database_name";
$table="table_name"; //the sql table name that you want to pull out the data from
$number="6"; //the total amount of results that you want to display

$i = 1;
$e = 2;

//number of columns of the table generated
$column = 3;

//connecting to mysql
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$getresults = mysql_query("
SELECT id, image, link, category FROM $table
ORDER BY id ASC
LIMIT $number");

echo "<table><tr>";
while($row = mysql_fetch_array($getresults))
{
echo "<td>";

echo "<p>$row[link]</p>";

echo "</td>";

if ($i == $column) {

echo "</tr><tr>";

$column = 3*$e;

$e++;

}

$i++;

}

echo "</table>";

?>
<br />


hope this helps ;)

DrewM
04-03-2006, 10:26 PM
Ok I'm no php professional, but this is what I did once for a client of mine and it seemed to work. Basically it displays the results upto 3 columns per row or 3 results per row, once the 3 results has been displayed, it goes to the next row.

$number, this variable controls the total amount of data you want to display.

also you have to change this part
SELECT id, image, link, category FROM $table to whatever your table is setup to.

and last, change this the same way,
$row[link]


<?php
$username="database_username";
$password="database_password";
$database="database_name";
$table="table_name"; //the sql table name that you want to pull out the data from
$number="6"; //the total amount of results that you want to display

$i = 1;
$e = 2;

//number of columns of the table generated
$column = 3;

//connecting to mysql
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$getresults = mysql_query("
SELECT id, image, link, category FROM $table
ORDER BY id ASC
LIMIT $number");

echo "<table><tr>";
while($row = mysql_fetch_array($getresults))
{
echo "<td>";

echo "<p>$row[link]</p>";

echo "</td>";

if ($i == $column) {

echo "</tr><tr>";

$column = 3*$e;

$e++;

}

$i++;

}

echo "</table>";

?>
<br />


hope this helps ;)
how can this be done with vb?

Sean S
04-03-2006, 10:50 PM
I personally did it by putting the forum's db info and connecting to forum's database.

DrewM
04-04-2006, 08:56 AM
oh well I'll just use what you gave me.

Code Monkey
04-04-2006, 01:26 PM
Just go to a place like Sitepoint.com and do a search for the term "Modulus" and you will find loads of info on easy solutions. I would answer direct but I'm headed off to work. :D

DrewM
04-04-2006, 08:58 PM
Can you there wasn't any thing at sitepoint

Code Monkey
04-05-2006, 02:34 AM
Really (http://www.sitepoint.com/forums/search.php?searchid=2125751)?
;)

DrewM
04-05-2006, 09:34 AM
tthat search does not work.

Edit I found this code that will and won't work it works with out the templeat but with it it doesn't start a new row heres my code:
$counter=3;
$rows = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "table WHERE active='1'");
while ($row = $db->fetch_array($rows)) {
if($counter % 3 == 0){
echo "<tr>";
}
?>
<td>
<?php echo $row['name']; ?>
</td>
<?php
if($counter % 3 == 0){
echo "</tr>";
$counter=1;
}
$counter++;
}

Aesma Deva
04-08-2006, 10:13 AM
Try this:

$rows = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "table WHERE active='1'");
$counter=0;
echo "<table><tr>";
while ($row = $db->fetch_array($rows)) {
if ($counter==3) {
echo "</tr><tr>";
$counter=0;
}
echo "<td>" . $row['name'] . "</td>";
$counter++;
}
echo "</tr></table>";

DrewM
04-08-2006, 10:34 AM
thanks!

Code Monkey
04-08-2006, 04:33 PM
tthat search does not work.

Edit I found this code that will and won't work it works with out the templeat but with it it doesn't start a new row heres my code:
$counter=3;
$rows = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "table WHERE active='1'");
while ($row = $db->fetch_array($rows)) {
if($counter % 3 == 0){
echo "<tr>";
}
?>
<td>
<?php echo $row['name']; ?>
</td>
<?php
if($counter % 3 == 0){
echo "</tr>";
$counter=1;
}
$counter++;
}

The modulus operator is the proper way to do this. My guess is you were not appending the next row to the output variable with .= instead of =. But you haven't posted the templated version.