Looping mysql php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Looping mysql php

Post by aussie_clint »

Hi

I am wondering if there is some way I could list the CategoryID and the heading I wanted and could get it to loop trough the mysql query and html table, so I don’t have to do the query and html table evert time

Connect.php

Code: Select all

<?php 
$location = "localhost"; 
$username = "computer_test";
$password = "test1"; 
$database = "computer_price"; 

$connection = mysql_connect("$location","$username","$password"); 
if (!$connection) die ("Could not connect MySQL"); 
mysql_select_db($database,$connection) or die ("Could not open database"); 

$profit = "1.3";
?>

Networking.php

Code: Select all

<?PHP
include("Connect.php"); 

//Defining HTML table
$query="SELECT p.PartNumber, p.Description, round((p.IncTax*($profit)),1) AS Price FROM `price list` p WHERE p.CategoryID = '3800' order by PartNumber";
$data_resource=mysql_query($query,$connection);
//Defining HTML table

Echo '<p align="center"><font size="6" color="#4F55A6" face="Verdana">Networking (Wired)</font></p>';

$rowcount = 0; 
$table="<table align=center cellspacing=0 cellpadding=0 width=400>"; 
      while ($row = mysql_fetch_array($data_resource)) { 
$rowcount++; 
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['Description'].'</td>', $rowcolor); 
$table.=sprintf('<td ALIGN="RIGHT"> $'.$row['Price'].'0</td></tr>', $rowcolor); 
$rowcolor = $rowcount % 2 == 0 ? "#FFFFFF" : "#C4DAFA"; 
      }
//Closing HTML table
$table.="</table>";

echo $table;

$query="SELECT p.PartNumber, p.Description, round((p.IncTax*($profit)),1) AS Price FROM `price list` p WHERE p.CategoryID = '3700' order by PartNumber";
$data_resource=mysql_query($query,$connection);
Echo '<p align="center"><font size="6" color="#4F55A6" face="Verdana">Networking (Wireless)</font></p>';

$rowcount = 0; 
$table="<table align=center cellspacing=0 cellpadding=0 width=400>"; 
      while ($row = mysql_fetch_array($data_resource)) { 
$rowcount++; 
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['Description'].'</td>', $rowcolor); 
$table.=sprintf('<td align="right"> $'.$row['Price'].'0</td></tr>', $rowcolor); 
$rowcolor = $rowcount % 2 == 0 ? "#FFFFFF" : "#C4DAFA"; 
      }
//Closing HTML table
$table.="</table>";

echo $table;

mysql_close();
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

How about:

Code: Select all

$query="SELECT p.PartNumber, p.Description, round((p.IncTax*($profit)),1) AS Price FROM `price list` p WHERE p.CategoryID = '3700' OR p.CategoryID = '3800' ORDER BY CategoryID,PartNumber"
Then check when the CategoryID changes.
(#10850)
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Post by aussie_clint »

Sorry, I didn’t explain my self properly, I am wanting to list the CategoryID and the title I want the table to be listed

For example

3800 ‘Network (Wired)’
3700 ‘Networking (Wireless)’

And then get them to be looped trough the query and html table

Code: Select all

$query="SELECT p.PartNumber, p.Description, round((p.IncTax*($profit)),1) AS Price FROM `price list` p WHERE p.CategoryID = '3800' order by PartNumber"; 
$data_resource=mysql_query($query,$connection); 
//Defining HTML table 

Echo '<p align="center"><font size="6" color="#4F55A6" face="Verdana">Networking (Wired)</font></p>'; 

$rowcount = 0; 
$table="<table align=center cellspacing=0 cellpadding=0 width=400>"; 
      while ($row = mysql_fetch_array($data_resource)) { 
$rowcount++; 
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['Description'].'</td>', $rowcolor); 
$table.=sprintf('<td ALIGN="RIGHT"> $'.$row['Price'].'0</td></tr>', $rowcolor); 
$rowcolor = $rowcount % 2 == 0 ? "#FFFFFF" : "#C4DAFA"; 
      }
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Not sure if i understand how you want the layout to look!

Do you want heading - 3800 ‘Network (Wired)’ and then list of related values below etc... or do you simply want to display a list of cat id's and the heading?
Post Reply