SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pulling and displaying Displaying data from mysql database

    My Mysql database table looks like this
    Product QTY
    Apple 2
    Orange 1
    Banana 3

    I need to pull out the data and present it like this.

    Products
    Apple
    Apple
    Orange
    Banana
    Banana
    Banana

    How do I do it?
    Last edited by bruin03; Nov 18, 2006 at 02:38. Reason: clarify

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems like more of an SQL question, but since you have posted it in PHP I would suggest that you select the whole table and then use the QTY column in a PHP loop (like 'for') and just loop QTY number of times.

    SELECT * FROM table

    Loop products
    Loop QTY
    echo $product;
    }
    }

    Obviously the above is not php code but you should get the picture
    Soren Beck Jensen

    http://www.automaticbacklinks.com - Free link exchange network
    http://www.notwebdesign.com - Joomla web design in Spain

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would be better to have 3 columns in the DB,

    id
    product
    qty

    Then each record would be inserted with the id auto incrementing and the other values would be added to from user input or something like that, then when you want to add one to the qty of apple just add one to the existing number. Now for displaying them this would work.

    PHP Code:
    //connect to the DB

    $result mysql_query("SELECT * FROM myTable") or die (mysql_error());

    while (
    $row mysql_fetch_array($result)) {

     
    $name $row['product'];
     
    $qty $row['qty'];

     for (
    $x=1$x <= $qty$x++) {

      echo 
    $name "<br>";
     
     }


    So if your DB was like this:

    id - 1
    product - Apple
    qty - 3

    It would produce

    Apple
    Apple
    Apple

    If it was like this:

    id - 1
    product - Apple
    qty - 3

    id - 2
    product - Pear
    qty - 2

    It would produce:

    Apple
    Apple
    Apple
    Pear
    Pear

    Hope this help (and works )

    p.s just reread your post and see you already have the 2 columns required, just ignore the 1st part of this post
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    webnoob, tt worked like a charm! Thank you!

    P.s. I do have an ID table, but ommitted it for clarify in my question

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bruin03
    webnoob, tt worked like a charm! Thank you!

    P.s. I do have an ID table, but ommitted it for clarify in my question
    Thanks for letting me know, never quite sure about for loops without testing first, lol
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  6. #6
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webnoob
    Thanks for letting me know, never quite sure about for loops without testing first, lol

    one More question,

    What if I need to pull the quantitiies from an array instead of msyql?

    For instance



    PHP Code:
    I have

    $products 
    =Array ( [0] => Array ( [id] => 150 [name] => Apple
                                       
    [quantity] => 
                      [
    1] => Array ( [id] => 130  [name] => Orange 
                          
    [quantity] => 
                       [
    2] => Array ( [id] => 155 [name] => Banana 
                         
    [quantity] => )
            ) 
    How do I put the $products array in the format above to get the same data? I appreciate your help.

  7. #7
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone?

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach ($products as $row) {
        
    // same thing here


  9. #9
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    PHP Code:
    foreach ($products as $row) {
        
    // same thing here

    What would $row be in the array?
    $id?

  10. #10
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $products =Array ( [0] => Array ( [id] => 150 [name] => Apple 
                                       
    [quantity] => 
                      [
    1] => Array ( [id] => 130  [name] => Orange 
                          
    [quantity] => 
                       [
    2] => Array ( [id] => 155 [name] => Banana 
                         
    [quantity] => 
            ) 
    foreach (
    $products as $row) {

    while (
    $row = array($products)) { 

    $name $row['name']; 
    $qty $row['quantity']; 

    for (
    $x=1$x <= $qty$x++) { 

      echo 
    $name "<br>"





    Would this be correct? Please help. I'm new to php.


Bookmarks

Posting Permissions

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