mpsn Posted May 21, 2011 Share Posted May 21, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/237072-how-to-display-just-one-image-using-a-php-array-in-html/ Share on other sites More sharing options...
Fadion Posted May 22, 2011 Share Posted May 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237072-how-to-display-just-one-image-using-a-php-array-in-html/#findComment-1218536 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.