Web Hosting Talk







View Full Version : How can I duplicate the results of a mysql query?


lexington
12-30-2007, 11:31 AM
Hello, I hope this is easy to do since I cannot figure it out. Basically the mysql DB stores rows of data for example:

name - quantity

bob - 5
jake - 4
sarah - 3
tom - 6

How can I create a php code that selects this info from the DB but duplicates the name by the amount of the quantity? Since you see bob has 5, could it displays bob's name 5 times and then jake 4 times right under it:

bob
bob
bob
bob
bob
jake
jake
jake
jake

and so on. I was able to create something that kinda works but it doesn't continue for every listing only the first one:

$count = $row['quantity'];

$x = 0;
while($x < $count)
{
echo $row['name'] . ' <br>';
$x++;
}

I assume a simple while statement would fix that but I couldn't get it to work. Please help and thanks :)

azizny
12-30-2007, 12:42 PM
Just add the first loop:



foreach($row as $thisrow){
$count = $thisrow['quantity'];
$x=0;
while($x < $count) {
echo $thisrow['name'] . ' <br>';
$x++;
}
}


Peace,

lexington
12-30-2007, 12:45 PM
Haha thanks I tried that last night but I must have did it wrong so I will try your version thanks :)

lexington
12-30-2007, 01:30 PM
Hello, it didn't seem to work it only displayed numbers:


4
4
4
4
4
4
4
4

DanielWTJ
12-30-2007, 03:35 PM
I'm not sure if this is what you want or not. It sounds like you would want to put LIMIT 1 on the end of your query.

Hope that helps :)

lexington
12-30-2007, 06:51 PM
Hmm I am not sure how adding a limit would change the numbers into displaying the names :)

dmspilot
12-30-2007, 07:13 PM
Hello, it didn't seem to work it only displayed numbers: That shouldn't be happening, can you post all of your code?

azizny
12-30-2007, 09:55 PM
Add this before the loops:


print_r($row);


What is the output?

Peace,