...

View Full Version : why does this image array not work?



AndreasE
01-11-2012, 02:48 AM
Hi, i really need this to work and i thought it should be. Please help me. sorry the text is in spanish (its my first language) thanks for ur help :D!!!
i wanna do this because i need to load 100 images on the page and i think its easier with a for loop.



<html>

<head>
<title>Prueba 1</title>
</head>

<body>
<p>Este es el sitio de imagenes de prueba</p>


<?
$Img[1] = 'hola/Foto1.jpg';
$Img[2] = 'hola/Foto2.jpg';
$Img[3] = 'hola/Foto3.jpg';
$Img[4] = 'hola/Foto4.jpg';
$Img[5] = 'hola/Foto5.jpg';

echo $Img[5];


?>
<img src= "<?$Img[4];?>" width ="80" height = "50" alt = "Primera foto">


</body>
</html>

adaminaudio
01-11-2012, 03:24 AM
I'm not the 'expert' at php but do you not need a 'php' tag?

after the <?

so: <?php

....

?>

so that the browser sees it.
I could be wrong, does your echo output anything to you?

_Aerospace_Eng_
01-11-2012, 05:50 AM
There are several things wrong with your code including your HTML. First off you need a doctype. Without a doctype the display of your page may look different across. Next using <? may not work on all servers and it is likely to not work at all with PHP 6. The use of short open tags is discouraged for a few reasons. On to the next problem. What is $Img? You don't tell PHP to create a new array so it doesn't know what type $Img should be. Finally in your image tag you need to echo out the image you want even thought I think PHP will still treat it as an array it is good practice to initialize variables.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<p>Este es el sitio de imagenes de prueba</p>
<?php
$Img = array();
$Img[1] = 'hola/Foto1.jpg';
$Img[2] = 'hola/Foto2.jpg';
$Img[3] = 'hola/Foto3.jpg';
$Img[4] = 'hola/Foto4.jpg';
$Img[5] = 'hola/Foto5.jpg';
echo $Img[5];
?>
<img src= "<?php echo $Img[4];?>" width="80" height="50" alt="Primera foto">
</body>
</html>

Satoius
01-11-2012, 03:03 PM
Ok I'm relatively new to PHP but I have enough experience to help a bit. But first a few questions. First what are you trying to do? And have you experimented with PHP at all? I'm going to assume you're trying to disply 100 images on your page, considering you said you need to load 100 photos. Ok so..

<html>

<head> <title>Prueba 1</title> </head>

<body> <p>Este es el sitio de imagenes de prueba</p>

<?
$Img[1] = 'hola/Foto1.jpg';
$Img[2] = 'hola/Foto2.jpg';
$Img[3] = 'hola/Foto3.jpg';
$Img[4] = 'hola/Foto4.jpg';
$Img[5] = 'hola/Foto5.jpg';

echo $Img[5];

?>

<img src= "<?$Img[4];?>" width ="80" height = "50" alt = "Primera foto">

</body> </html>

You mentioned you think a for loop would work. Yes it would so use one... just making a variable equal to a value several times defeats the purpose.

Also as said above, you need to declare, or let the server know, $img is a variable array.
and you can do that by stating $img = array(), however if you want things to be in the array you either have to add it in the declared statement or use the array_push function.

In PHP, 'echo' will display whatever is after it. 'echo $img[5];' will just display whatever was put in that variable, in this case will display 'hola/Foto5'.

There are a lot of issue with your code and what I've mentioned was just the start. The project you're on maybe too much for you right now. Practice first making a variable array work without making them images. Make them just plain text and try to display several different words. And then learn... well what a for loop is because you're not using one.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum