Using a set of Mysql results as a comparison in PHP -
this question quite simple i'm not sure of terms involved answer myself. have mysql database containing of product information. make image appear on product pages products have attribute. have created sql query outputs list of product_ids products image show on. however, not know how use set of results in page itself.
i have:
$cupwinnerids = mysql_query("select product_id `jos_vm_product_type_1` jos_vm_product_type_1.cup_winner ='cup winners';"); while($row = mysql_fetch_array($cupwinnerids)) { echo $row['product_id'] . ","; }
this outputs correct ids comma in between them. wrap whole thing $listofids = (...) , can use in product page php file: if $product_id in $listofids ... if not ... having problem understanding how use selection of ids. if try output list directly "array". appreciated.
the problem may trying display array using echo, while should use print_r.
in case this:
$listofids = array(); $cupwinnerids = mysql_query("select product_id `jos_vm_product_type_1` 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 want manage values of array should use foreach loop, iterates through array, this
foreach ($listofids $id) { echo $id . ", "; } // edit: use echo implode($listofids,", ") same
if want compare if $product_id in $listofids in_array, returns true if value looking in array, , false if isn't:
if (in_array($product_id, $listofids)) { echo "product id in list of product id's"; } else { echo "product id isn't in list of product id's"; }
Comments
Post a Comment