Question:
PHP random repeat rows?
Kelly H
2010-12-02 10:19:02 UTC
Hey everyone i need some help,

I want to make this code for mat with 4 products on first row and 4 products on the second and 4 on the third and so on.
as of right now using the code below it shows one on each row i want it to show 4 on row 1 and 4 on row two so on and so on.

How do i do this?


mysql_selectdb("*********",$link);
// Select records from the DB
$query = "SELECT * FROM Products WHERE home='1' ORDER BY Rand() ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo"";
echo "
";
echo"";
}
Three answers:
just "JR"
2010-12-02 10:52:16 UTC
You can't!

Each "while" will extract ONE row, and one record only.

To display your results as a table of 4 colums, use:



$col = 0;

while (...)

{

if ($col == 0)

echo ("");

echo ("... the images and details of THAT entry...);

$col++;

if ($col == 4)

{

$col = 0;

echo ("");

}

}

mysql_free_result(...);

if ($col != 0)

echo ("");

echo ("");
anonymous
2010-12-02 18:50:51 UTC
Try something like



echo"";

mysql_selectdb("*********",$link);

// Select records from the DB

$query = "SELECT * FROM Products WHERE home='1' ORDER BY Rand() ";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

echo"";

echo "";

}

echo"

";
Greg
2010-12-03 14:36:20 UTC
You will want to take a look at what just JR said. What he is explaining is a reporting structure, and learning how to engineer reporting structures is very important.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...