Page 1 of 1

How many returned results have the same manufacturers

Posted: Sat Apr 10, 2010 1:04 pm
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);

Re: How many returned results have the same manufacturers

Posted: Sat Apr 10, 2010 1:40 pm
by minorDemocritus
Let's have an example of the database contents, and what you want the code to output.

Re: How many returned results have the same manufacturers

Posted: Sat Apr 10, 2010 4:31 pm
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';

Re: How many returned results have the same manufacturers

Posted: Sun Apr 11, 2010 12:39 pm
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);
                        }




Re: How many returned results have the same manufacturers

Posted: Sun Apr 11, 2010 10:41 pm
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';

Re: How many returned results have the same manufacturers

Posted: Mon Apr 12, 2010 7:04 am
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.

Re: How many returned results have the same manufacturers

Posted: Tue Apr 13, 2010 10:16 am
by Vestax159
Worked wonderfully thanks a bunch!