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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP newbie: Loading external variables not working

    I'm learning PHP at the moment and have created a few pages on my hosting account to test out things.

    I am trying to load a value from a randomly generated postion in an array in an external PHP file and echo it to the screen inside a generated div.

    The text doesn't appear on the page and is missing in the source of the final page. I have managed to do exactly the same thing in a different file but not inside a div and these files are in the same directory with the code being identical.

    The online example is here:

    http://www.olliesilviotti.co.cc/php_stuff

    (the second submit button (next to the combo box) executes the script I'm talking about)

    Here's the relevant source's:

    index.php:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <?php include("txt.php"); ?>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>PHP Stuff</title>
    </head>
    
    <body>
    <form method="get" action="phpscript.php">
    Name:&nbsp;<input type="text" name="nameBox" />
    <input type="submit" name="btn1" value="Submit"/>
    </form>
    <br /><br />
    <hr>
    <form method="post" action="phpscript2.php">
    <select name="combo_no">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>
    <input type="submit" name="submitbtn" value="Submit" />
    </form>
    <hr>
    <?php 
    $randval = rand(0, 2);
    $imglist = array("red", "blue", "green");
    echo "<img src='images/".$imglist[$randval].".jpg' />";
    ?>
    <hr>
    <?php $randmessval = rand(0,2); echo $mess[$randmessval]; ?>
    
    </body>
    </html>
    phpscript2.php:
    Code:
    <?php
    
    $no_divs = $_POST['combo_no'];
    
    echo "<html>"."\n"."<head>"."\n"."<link rel='text/css' type='stylesheet' src='script2style.css'/>"."\n"."<title>PHP Stuff - Script 2 - DIVs</title>"."\n"."<?php include(\"txt.php\"); ?>"."\n"."</head>"."\n"."<body>"."\n";
    for($i = 0; $i < $no_divs; $i++)
    {
    echo "<div id='div".$i."'".">"."\n";
    echo "<?php echo $mess[0]; ?>";
    echo "\n"."</div>"."\n";
    }
    echo "</body>"."\n"."</html>";
    
    ?>
    txt.php:
    Code:
    <?php
    
    $mess = array("This is Div 1. I have filled it with content to check that this all works and I'm not an idiot. Have a nice day. Div 1 over and out.", "This is Div 2. I have filled it with content to check that this all works and I'm not an idiot. Have a nice day. Div 2 over and out.", "This is Div 3. I have filled it with content to check that this all works and I'm not an idiot. Have a nice day. Div 3 over and out.");
    
    ?>

    I'm sure the answer is stupidly simple but I can't figure it out

    Cheers

    ollie
    Last edited by ollie101; 03-10-2009 at 09:18 PM.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    echo "$mess[$i-1]; "
    To get the number from the array the same as the div number you should use -1 because the array pointer starts at 0 not 1. Also the $i is the dynamic variable on which the div number is.
    Also you are already echo'ing in PHP there is no reason to echo the <?php ?> tags.
    Thanks
    Rafiki
    Last edited by rafiki; 03-10-2009 at 02:46 PM.

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the reply

    I 've tried changing it but it's not working still. What I've done is replaced

    Code:
    echo "<?php echo $mess[0]; ?>";
    in phpscript2.php with:

    Code:
    echo "$mess[0]; ";
    and all that gets echo'd is the semi-colon. this suggests that the link between the external file isn't quite right but I can't see how, I've checked it against the file in which it works and can't see any differences

  • #4
    Regular Coder FWDrew's Avatar
    Join Date
    Apr 2008
    Location
    Missouri
    Posts
    380
    Thanks
    38
    Thanked 45 Times in 43 Posts
    Code:
    echo "$mess[0]; ";
    and all that gets echo'd is the semi-colon. this suggests that the link between the external file isn't quite right but I can't see how, I've checked it against the file in which it works and can't see any differences
    Did you mean to do this:

    PHP Code:
    echo $mess[0]; 
    Best Regards,

    Drew

    EDIT-If you wanted the string to actually have quotes around it when printed, one way to do that would be:
    PHP Code:
    echo '"'.$mess[0].'"'
    Last edited by FWDrew; 03-10-2009 at 08:13 PM.
    Dev-Tips.com || Employee at the Envato Network || My Portfolio

  • #5
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've tried that as well and get no output still

    Code:
    <?php
    
    $no_divs = $_POST['combo_no'];
    
    echo "<html>"."\n"."<head>"."\n"."<link rel='text/css' type='stylesheet' src='script2style.css'/>"."\n"."<title>PHP Stuff - Script 2 - DIVs</title>"."\n"."<?php include(\"txt.php\"); ?>"."\n"."</head>"."\n"."<body>"."\n";
    for($i = 0; $i < $no_divs; $i++)
    {
    echo "<div id='div".$i."'".">"."\n";
    echo $mess[0];   
    echo "\n"."</div>"."\n";
    }
    echo "</body>"."\n"."</html>";
    
    ?>
    http://olliesilviotti.co.cc/php_stuff/
    (second submit button down)

  • #6
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    try this to make sure there are items in your array
    PHP Code:
    <?php

    print_r
    ($mess);   

    ?>

  • #7
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    There are defiantely items in the array as the index.php is showing them (at the bottom of the page, the text that says about filling divs with content)

  • #8
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    You're not including txt.php on the page that prints the output.
    you really need this
    PHP Code:
    <?php
    include('txt.php');
    $no_divs $_POST['combo_no'];

    echo 
    "<html>"."\n"."<head>"."\n"."<link rel='text/css' type='stylesheet' src='script2style.css'/>"."\n"."<title>PHP Stuff - Script 2 - DIVs</title>"."\n"."<?php include(\"txt.php\"); ?>"."\n"."</head>"."\n"."<body>"."\n";
    for(
    $i 0$i $no_divs$i++)
    {
    echo 
    "<div id='div".$i."'".">"."\n";
    echo 
    $mess[0];   
    echo 
    "\n"."</div>"."\n";
    }
    echo 
    "</body>"."\n"."</html>";

    ?>

  • Users who have thanked ninnypants for this post:

    ollie101 (03-10-2009)

  • #9
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That was it! Thanks a lot, was echoing it so it was included in the html, didn't think that it needed to be included when it was processed by the server. Knew it'd be something stupid like.

    Thanks a lot for your time


  •  

    Posting Permissions

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