Results 1 to 8 of 8
-
12-30-2007, 11:31 AM #1Disabled
- Join Date
- Dec 2003
- Posts
- 1,941
How can I duplicate the results of a mysql query?
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:
Code:$count = $row['quantity']; $x = 0; while($x < $count) { echo $row['name'] . ' <br>'; $x++; }
-
12-30-2007, 12:42 PM #2Web Hosting Master
- Join Date
- Mar 2004
- Location
- USA
- Posts
- 4,345
Just add the first loop:
PHP Code:
foreach($row as $thisrow){
$count = $thisrow['quantity'];
$x=0;
while($x < $count) {
echo $thisrow['name'] . ' <br>';
$x++;
}
}
Testing 1.. Testing 1..2.. Testing 1..2..3...
-
12-30-2007, 12:45 PM #3Disabled
- Join Date
- Dec 2003
- Posts
- 1,941
Haha thanks I tried that last night but I must have did it wrong so I will try your version thanks
-
12-30-2007, 01:30 PM #4Disabled
- Join Date
- Dec 2003
- Posts
- 1,941
Hello, it didn't seem to work it only displayed numbers:
4
4
4
4
4
4
4
4Last edited by lexington; 12-30-2007 at 01:42 PM.
-
12-30-2007, 03:35 PM #5Newbie
- Join Date
- Dec 2007
- Posts
- 11
I'm not sure if this is what you want or not. It sounds like you would want to put
Code:LIMIT 1
Hope that helpsDaniel Thompson
http://thehonesthost.com
-
12-30-2007, 06:51 PM #6Disabled
- Join Date
- Dec 2003
- Posts
- 1,941
Hmm I am not sure how adding a limit would change the numbers into displaying the names
-
12-30-2007, 07:13 PM #7Junior Guru Wannabe
- Join Date
- Mar 2005
- Posts
- 31
-
12-30-2007, 09:55 PM #8Web Hosting Master
- Join Date
- Mar 2004
- Location
- USA
- Posts
- 4,345
Add this before the loops:
PHP Code:print_r($row);
Peace,Testing 1.. Testing 1..2.. Testing 1..2..3...