Jump to content

How to display just ONE image using a php array in html


mpsn

Recommended Posts

Hi, i'm getting an error when I load my php code in a browser. Here's my code snippet:

<?php

  mysql_connect("localhost","root");

  mysql_select_db("something");

  $bellProductsArray=array(1=>"Apple_iPhone3GS.jpg",2=>"Apple_iPhone4.jpg");

 

  $result=mysql_query("SELECT Name, Manufacturer FROM bellProducts WHERE ID=1");

  $row=mysql_fetch_assoc($result);

  print "Name: {$row['Name']} Manufacturer: {$row['Manufacturer'] }";

 

?>

 

<img src=<?php array_values($bellProductsArray); ?>  alt="Apple iPhone 3GS" title="Apple iPhone 3GS" />

 

**I can't get it to display the first image (eg: Apple_iPhone3GS.jpg), I made sure that I have the image in same directory. Plz help!1!

You'll need to target a specific key of the array. In your case, you have keys: 1 and 2. If you leave keys empty, they'll be generated starting by zero (0).

 

<?php
$bellProductsArray=array(1=>"Apple_iPhone3GS.jpg", 2=>"Apple_iPhone4.jpg");

echo $bellProductsArray[1]; //will print "Apple_iPhone3GS.jpg
echo $bellProductsArray[2]; //will print "Apple_iPhone4.jpg
?>

 

Or otherwise you can leave the keys empty:

<?php
$bellProductsArray=array("Apple_iPhone3GS.jpg", "Apple_iPhone4.jpg");

echo $bellProductsArray[0]; //will print "Apple_iPhone3GS.jpg
echo $bellProductsArray[1]; //will print "Apple_iPhone4.jpg
?>

 

By the way, arra_values() will just return an array holding the values of the original array and the keys will be numerical, starting from zero. There's no use of that function in this case.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.