Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Manipulating mysql output through PHP

    Hello All,

    I am trying to manipulate the way my mysql table data displays on my PHP page. Here is my setup:

    Mysql table called orders

    Fields:
    * custnum
    * fname
    * lname
    * item
    * qty
    * date

    When a customer places the order an entry is made to the table for each item purchased so in my example, Customer A made 3 purchases so this would be in the table.

    1001 First Name Last Name Shoes 1 2/17/2009
    1001 First Name Last Name Socks 2 2/17/2009
    1001 First Name Last Name Laces 2 2/17/2009

    I am using PHP to display this order information for order history purposes. Right now I have a while loop that just prints out the table exactly as it is above into a html table in a browser that looks like this

    <table>
    <tr>
    <td>1001</td><td>First Name</td><td>Last Name</td><td>Shoes</td><td>1</td><td>2/17/2009</td>
    </tr>
    <tr>
    <td>1001</td><td>First Name</td><td>Last Name</td><td>Socks</td><td>2</td><td>2/17/2009</td>
    </tr>
    <tr>
    <td>1001</td><td>First Name</td><td>Last Name</td><td>Laces</td><td>2</td><td>2/17/2009</td>
    </tr>
    </table>

    Now for my question, is there a way that I can print out my table into a php/html table where instead of creating three seperate lines for the same customer it will only print one line that looks like this (this is assuming each customer will ALWAYS have three items purchased at the same time, no more, no less):

    <table>
    <tr>
    <td>1001</td><td>First Name</td><td>Last Name</td><td>Shoes</td><td>1</td><td>Socks</td><td>2</td><td>Laces</td><td>2</td><td>2/17/2009</td>
    </tr>
    </table>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yes there is. But this is a PHP question, not a MySQL question.

    Anyways...

    First, isolate the portion of your HTML coding that repeats.

    once
    <table>
    <tr>

    once per customer
    <td>1001</td><td>First Name</td><td>Last Name</td>

    repeat up to three times per customer
    <td>Shoes</td><td>1</td>
    <td>Socks</td><td>2</td>
    <td>Laces</td><td>2</td>

    once per customer
    <td>2/17/2009</td>
    </tr>

    once
    </table>

    Now build your PHP code looping structure around this.

    HINT: As you fetch data from your tables, build an array instead of trying to create your HTML tags in your fetch loop.


  •  

    Posting Permissions

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