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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.