Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-08-2007, 11:03 PM   PM User | #1
bunny1
New Coder

 
Join Date: Feb 2007
Posts: 92
Thanks: 1
Thanked 0 Times in 0 Posts
bunny1 is on a distinguished road
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
bunny1 is offline   Reply With Quote
Old 03-08-2007, 11:34 PM   PM User | #2
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
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.
timgolding is offline   Reply With Quote
Old 03-09-2007, 12:26 AM   PM User | #3
bunny1
New Coder

 
Join Date: Feb 2007
Posts: 92
Thanks: 1
Thanked 0 Times in 0 Posts
bunny1 is on a distinguished road
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
bunny1 is offline   Reply With Quote
Old 03-09-2007, 12:30 AM   PM User | #4
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,602
Thanks: 2
Thanked 398 Times in 391 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Removed. See below.

Last edited by Inigoesdr; 03-09-2007 at 01:14 AM..
Inigoesdr is offline   Reply With Quote
Old 03-09-2007, 12:32 AM   PM User | #5
bunny1
New Coder

 
Join Date: Feb 2007
Posts: 92
Thanks: 1
Thanked 0 Times in 0 Posts
bunny1 is on a distinguished road
ok that makes sense thanks!
bunny1 is offline   Reply With Quote
Old 03-09-2007, 12:56 AM   PM User | #6
phoenixshade
Regular Coder

 
Join Date: Feb 2007
Location: near Washington, DC
Posts: 135
Thanks: 0
Thanked 0 Times in 0 Posts
phoenixshade is on a distinguished road
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>".
__________________
— 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

Last edited by phoenixshade; 03-09-2007 at 01:08 AM..
phoenixshade is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:45 AM.


Advertisement
Log in to turn off these ads.