How many returned results have the same manufacturers

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
Vestax159
Forum Newbie
Posts: 12
Joined: Thu Feb 18, 2010 3:24 pm

How many returned results have the same manufacturers

Post by Vestax159 »

So here is the issue. I am running a query to return every item that is searched for using LIKE I then use a while statement to echo each row of the database one at a time. The variable $manufacturer returns well the manufacturer of the product. What I need is to display each individual manufacturer returned and how many items for each manufacturer.

So say HP (15), Epson (5), etc. This is how I'm currently thinking of going about doing this but there is probably a much more effective way to do this then how I'm going about it below. Even in my example I'm not sure how to do an incrementing variable variable. Still fairly new to php so any help is greatly appreciated! Also there is a lot of manufacturers so testing if it's one of each wouldn't work either. :banghead:

Let me know if I'm being unclear about my problem.

Code: Select all

// Check if current manufacturer has already been found in previous item
If ($manufacturer = $existing_manufacturer1){

$existing_manufacturer1_count += 1

} elseif ($manufacturer = $existing_manufacturer2){

$existing_manufacturer2_count += 1

} else {
//if not found in a previous item then assign it to an incrementing variable
$manufacturer = $existing_manufacturer3

}

echo $existing_manufacturer1 . $existing_manufacturer1_count (etc etc);
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: How many returned results have the same manufacturers

Post by minorDemocritus »

Let's have an example of the database contents, and what you want the code to output.
lunarnet76
Forum Commoner
Posts: 67
Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh

Re: How many returned results have the same manufacturers

Post by lunarnet76 »

you can simply use SQL to have the results :
SELECT count(i.*) thecount, m.name FROM manufactures m JOIN items i ON i.manufacturers=m.id
or something like that^^

Otherwise if you want to do it in PHP use array!
for example :

Code: Select all

while($infos=mysql_fetch_assoc($query){
             If(!isset($manufacturers[$infos['name']]))
                            $manufacturers[$infos['name']]=0;
             $manufacturers[$infos['name']]++;
}

foreach($manufacturers as $name=>$count)
echo $name.' has '.$count.' items';
Vestax159
Forum Newbie
Posts: 12
Joined: Thu Feb 18, 2010 3:24 pm

Re: How many returned results have the same manufacturers

Post by Vestax159 »

In the example below if the user searched for inkjet it should return HP (4) and Epson (2). Using oscommerce if your wondering about the tep_db_query / tep_db_fetch_array.

Code: Select all

products_name     manufacturer
Inkjet                       HP
Inkjet                       HP
Inkjet                       Epson
Inkjet                       HP
Inkjet                       Epson
Inkjet                       HP


$getmanufacturer = tep_db_query(
"SELECT * FROM  products 
WHERE products_name LIKE '%" . $search . "%' ") 
					or die ("Couldn’t execute query.");

while ($row = tep_db_fetch_array($getmanufacturer))
			{
				extract($row);
                        }



Vestax159
Forum Newbie
Posts: 12
Joined: Thu Feb 18, 2010 3:24 pm

Re: How many returned results have the same manufacturers

Post by Vestax159 »

So how would I apply the above answer into that? Sorry still fairly new to php.

Code: Select all

while($infos=mysql_fetch_assoc($query){
             If(!isset($manufacturers[$infos['name']]))
                            $manufacturers[$infos['name']]=0;
             $manufacturers[$infos['name']]++;
}

foreach($manufacturers as $name=>$count)
echo $name.' has '.$count.' items';
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: How many returned results have the same manufacturers

Post by roders »

This is how i would go about to do it

Code: Select all

$query="select manufacturer,count(manufacturer) as `mcount` from tablename group by manufacturer";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
	echo $row['manufacturer'] ."(".$row['mcount'].")<br>";
}
change it to match the tablename and fieldname in your database.
Vestax159
Forum Newbie
Posts: 12
Joined: Thu Feb 18, 2010 3:24 pm

Re: How many returned results have the same manufacturers

Post by Vestax159 »

Worked wonderfully thanks a bunch!
Post Reply