Results 1 to 8 of 8
  1. #1

    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++;
    		}
    I assume a simple while statement would fix that but I couldn't get it to work. Please help and thanks

  2. #2
    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++;
         }

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  3. #3
    Haha thanks I tried that last night but I must have did it wrong so I will try your version thanks

  4. #4
    Hello, it didn't seem to work it only displayed numbers:

    4
    4
    4
    4
    4
    4
    4
    4
    Last edited by lexington; 12-30-2007 at 01:42 PM.

  5. #5
    I'm not sure if this is what you want or not. It sounds like you would want to put
    Code:
    LIMIT 1
    on the end of your query.

    Hope that helps
    Daniel Thompson
    http://thehonesthost.com

  6. #6
    Hmm I am not sure how adding a limit would change the numbers into displaying the names

  7. #7
    Join Date
    Mar 2005
    Posts
    31
    Quote Originally Posted by lexington View Post
    Hello, it didn't seem to work it only displayed numbers:
    That shouldn't be happening, can you post all of your code?

  8. #8
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,345
    Add this before the loops:

    PHP Code:
    print_r($row); 
    What is the output?

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •