tags:

views:

139

answers:

1

Hi,

I cannot work out how to display an image depending on the vaule of a product($idd):

    foreach ($response['products'] as $product) 
{ 
 if($filter != "all" && $bb[$product['product-id']['value']] && $bb[$product['product-id']['value']][0] != $filter) continue; 

 $r = array(); $idd = 0; $ca = 0; 

  foreach($product as $key=>$value) 
 { 
  if($key == "product-id") 
  { 
   $idd = (int)$value['value']; 
   $r[0] = $idd?"<input type='radio' name='product_id' value='" . $idd . "'/>":"0&nbsp;"; 
   //price// 
    $r[4] = $bb[$idd]?$bb[$idd][3]:"ID"; 
   //info// 
    $r[5] = "<a href='checker/info.php?id=" .$idd. "'; rel=\"shadowbox;width=400;height=200\" class=\"option\"'><img src=\"http://www.flipconnect.co.uk/images/more-info.gif\" style=\"border:none; padding-top:24px;\" alt=\"more information about this pack\" /></a>"; 
   //buy 
    $r[6] = "<a href='checker/next.php?id=" . $idd . "&amp;postcode=".$postcode."&amp;speed=".$phone."'><img src=\"http://www.flipconnect.co.uk/images/buy-now.gif\" style=\"border:none\" alt=\"buy this package\" /></a>"; 
  }

I'm thinking it should tak the form of:

$r[0] = if ($idd == 1021) echo "IMAGE URL": echo "";

But doesn't work, by the way all the $idd are stored in an array with a load of othewr info.

+1  A: 

Your question is not quite clear to me but Yours this line of code

`$r[0] = if ($idd == 1021) echo "IMAGE URL": echo "";`

can be rewritten as

echo $r[0] = ($idd == 1021) ? "IMAGE URL" : "";

Try it.

EDIT

Regarding Bifter's comment "This work, how would adapt to display a different image if the $idd value is different. i.e - $idd == 1021 - Image 1 is displayed $idd == 1022 - Image 2 is displayed $idd == 1023 - Image 3 is displayed"

if your data array is something like ..

$arr = array('1021' => 'img 1', '1022' => 'Img 2')

then you could try something like this

echo $r[0] = array_key_exists($idd, $data) ? $data[$id] : "";

assuming that you are looping through ID values ($id).

Wbdvlpr
changing an if-statement to a ternary one is never good for clearity nor readability.
BeowulfOF
This work, how would adapt to display a different image if the $idd value is different. i.e - $idd == 1021 - Image 1 is displayed $idd == 1022 - Image 2 is displayed$idd == 1023 - Image 3 is displayed
@beowulfof .. not very sure if you are right.. But here ternary is much more clear than Bifter's line.
Wbdvlpr
if() does not return anything, hence you can't use it to assign anything. Also, "echo" outputs, it doesn't return the value for assignment. The first line is not even valid syntax.
deceze
Returns - Parse error: syntax error, unexpected T_FOREACH on line 367line 367 is foreach($product as $key=>$value)Thanksfor your help.
+1 for ternary in this instance. If the if statement can be reduced to one line, then ternary all the way! It's less readable IMO to keep switching between multi-and one-line if statements. Consistant code == readable code.
MatW