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 6 of 6

Thread: Loop

  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Loop

    Hi

    I want to create a loop to print items from the database.
    I want to check for product type and assign that to a variable and then list all items with that product type.
    Then move to the next product type, enter that into the variable and print all items of that product type and so forth.
    Any ideas how i should do this?

    Thanks

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    So first things first have you got a database?
    If so is it an SQL database?
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ya i'm connected to a database and i'm using this code to display the results in a table
    PHP Code:
    <?php echo "<table border='1'>
    <tr>
    <th>Product Code</th>
    <th>Name</th>
    <th>Product Type</th>
    <th>Picture</th>
    <th>Description</th>
    <th>Stock</th>
    <th>Price</th>


    </tr>"
    ;

    while(
    $row = @mysql_fetch_array($result))
    {

    if (
    $row['Department'] == $var)
    {
    echo 
    "<tr>";
    //echo "<td>" . $row['Department'] . "</td>";
    echo "<td>" $row['Prod_code'] . "</td>";
    echo 
    "<td>" $row['Name'] . "</td>";
    echo 
    "<td>" $row['Prod_Type'] . "</td>";
    echo 
    "<td><img height = 50 width = 50 src = " $row['Picture'] . "></td>";
    echo 
    "<td>" $row['Description'] . "</td>";
    echo 
    "<td>" $row['Stock'] . "</td>";
    echo 
    "<td>" $row['Price'] . "</td>";
    echo 
    "</tr>";
    }
    }
    echo 
    "</table>";mysql_close($con);
    ?>
    However this lists all the data from the database in a row.
    I want to have a heading with each product type in the database in bold and then all the products of that type underneath and then the next type etc.
    Like this structure :

    Product Type(first prod type in the table)

    Name Code Description Stock Picture Price
    Name Code Description Stock Picture Price (all items of that prod type)
    Name Code Description Stock Picture Price
    Name Code Description Stock Picture Price

    Product Type (next prod type in table)

    Name Code Description Stock Picture Price
    Name Code Description Stock Picture Price
    Name Code Description Stock Picture Price
    Name Code Description Stock Picture Price

    I was thinking if i could add all distinct prod types to an array.Then select * from the first element of the array and display, then the same with the next element and so forth. I just cant get my head around coding the loop i need!

    thanks

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Removed. See below.
    Last edited by Inigoesdr; 03-09-2007 at 01:14 AM.

  • #5
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok that makes sense thanks!

  • #6
    Regular Coder
    Join Date
    Feb 2007
    Location
    near Washington, DC
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, I think it's ORDER BY that would be more appropriate in this case. GROUP BY collapses multiple rows into one, and generates errors if you're not very careful.

    The SQL query would be like this:
    Code:
    SELECT Prod_Code, Name, Prod_Type, Picture, Description, Stock, Price
    FROM tbl_Products
    ORDER BY Prod_Type
    ... and the PHP would be modified to this:
    Code:
    <?php echo "<table border='1'> 
    <tr> 
        <th>Product Code</th> 
        <th>Name</th> 
    // delete this line    <th>Product Type</th>
        <th>Picture</th> 
        <th>Description</th> 
        <th>Stock</th> 
        <th>Price</th>
    </tr>";
    $lastType = 'none';        // initialize $lastType to any value not in the table
    while($row = @mysql_fetch_array($result)) { 
        if ($row['Prod_Type'] != $lastType) {       // Prod_Type same as last row?
            echo '<tr><td colspan="6">Product Type: ' . $row['Prod_Type'] . '</td></tr>';     // put a product type heading in the table
            $lastType = $row['Prod_Type'];      // set $lastType to the new Prod_Type value
        }
        if ($row['Department'] == $var) { 
            echo "<tr>"; 
            //echo "<td>" . $row['Department'] . "</td>"; 
            echo "<td>" . $row['Prod_code'] . "</td>"; 
            echo "<td>" . $row['Name'] . "</td>"; 
    // delete this        echo "<td>" . $row['Prod_Type'] . "</td>";
            echo "<td><img height = 50 width = 50 src = " . $row['Picture'] . "></td>"; 
            echo "<td>" . $row['Description'] . "</td>"; 
            echo "<td>" . $row['Stock'] . "</td>"; 
            echo "<td>" . $row['Price'] . "</td>"; 
            echo "</tr>"; 
        } 
    } 
    echo "</table>";mysql_close($con); 
    ?>
    Now you'll get the output you mentioned. At the top of each product type, you'll get a cell the full width of the table with a heading for that type, with all the products in that type listed below. If you wanted, you could even add a class to the <td colspan="6"> and style that class with a contrasting background to further separate the table into sections by product type.

    Just as an aside, since you are echoing double-quoted strings, there's no need to concatenate with the . operator. Simply do this: echo "<td>$row['column']</td>".
    Last edited by phoenixshade; 03-09-2007 at 01:08 AM.
    — Wilford Nusser
    Validate Your Code: (X)HTML CSS
    An HTML Email is NOT a Web Page: HTML Email Guide (1.2Mb pdf) Webmail CSS Support
    REGEX: Brought to you by Psychotic Crack-Smoking Monkeys


  •  

    Posting Permissions

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