Natastna2
Natastna2

Reputation: 39

Using a set of Mysql results as a comparison in PHP

This question is quite simple but I'm not sure of the terms involved to look up the answer myself. What I have is a MYSQL database containing all of my product information. I would like to make an image appear on the product pages for products that have a certain attribute. I have created this SQL query which outputs the list of product_ids for the products that I would like the image to show up on. However, I do not know how to use this set of results in the page itself.

I currently have:

$cupwinnerids = mysql_query("SELECT product_id FROM `jos_vm_product_type_1` WHERE jos_vm_product_type_1.Cup_Winner ='Cup Winners';");
while($row = mysql_fetch_array($cupwinnerids))
  {
  echo $row['product_id'] . ",";
  }

This outputs the correct ids with a comma in between them. What I would like to do is wrap the whole thing with something like $listofids = (...) and then I can use in the product page PHP file: if $product_id is in $listofids then ... if not then ... I am just having a problem understanding how to use this selection of ids. If I try to output the list directly I just get "array". Any help would be greatly appreciated.

Upvotes: 0

Views: 82

Answers (3)

davidaam
davidaam

Reputation: 449

The problem may be that you are trying to display the array using echo, while you should use print_r.

In your case you could do something like this:

$listofids = array();
$cupwinnerids = mysql_query("SELECT product_id FROM `jos_vm_product_type_1` WHERE jos_vm_product_type_1.Cup_Winner ='Cup Winners';");
while($row = mysql_fetch_array($cupwinnerids))
{
array_push($listofids, $row['product_id']);
}
print_r($listofids);

As Ryan commented, if you want to manage the values of the array you should then use a foreach loop, that iterates through the array, something like this

foreach ($listofids as $id) {
echo $id . ", ";
}
// EDIT: you could also use echo implode($listofids,", ") which will do basically the same

If what you want is to compare if $product_id is in $listofids us in_array, that returns true if the value you are looking for is in the array, and false if it isn't:

if (in_array($product_id, $listofids)) {
echo "Product ID is in the List of Product ID's";
}
else {
echo "Product ID isn't in the List of Product ID's";
}

Upvotes: 1

user1191247
user1191247

Reputation: 12973

It sounds like you would be better off modifying your query for returning the products so that it joins to the jos_vm_product_type_1 table -

SELECT `jos_vm_product`.*, IF(`jos_vm_product_type_1`.`product_id` IS NULL, 0, 1) AS `winner`
FROM `jos_vm_product`
LEFT JOIN `jos_vm_product_type_1`
    ON `jos_vm_product`.`product_id` = `jos_vm_product_type_1`.`product_id`
    AND `jos_vm_product_type_1`.`Cup_Winner` ='Cup Winners'

Upvotes: 0

Ryan Kempt
Ryan Kempt

Reputation: 4209

Why not return $cupwinnerids to your products page, and then do something like...

while($row = mysql_fetch_array($cupwinnerids)) {
  if ($row['product_id'] == $product_id)
    // display your image
}

Upvotes: 0

Related Questions