Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    why does this image array not work?

    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 !!!
    i wanna do this because i need to load 100 images on the page and i think its easier with a for loop.


    PHP Code:
    <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>
    Last edited by Inigoesdr; 01-11-2012 at 03:42 AM.

  • #2
    New Coder
    Join Date
    Jan 2012
    Location
    Columbus, Ohio, U.S.A
    Posts
    41
    Thanks
    0
    Thanked 8 Times in 8 Posts
    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?

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    PHP Code:
    <!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>
    Last edited by _Aerospace_Eng_; 01-11-2012 at 05:54 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •