...

View Full Version : Resolved PHP newbie: Loading external variables not working



ollie101
03-10-2009, 02:01 PM
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:


<!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:


<?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:


<?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

rafiki
03-10-2009, 02:44 PM
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

ollie101
03-10-2009, 07:44 PM
Thanks for the reply

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


echo "<?php echo $mess[0]; ?>";

in phpscript2.php with:


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

FWDrew
03-10-2009, 08:09 PM
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:


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:

echo '"'.$mess[0].'"';

ollie101
03-10-2009, 08:48 PM
I've tried that as well and get no output still


<?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)

ninnypants
03-10-2009, 08:54 PM
try this to make sure there are items in your array


<?php

print_r($mess);

?>

ollie101
03-10-2009, 09:01 PM
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)

ninnypants
03-10-2009, 09:13 PM
You're not including txt.php on the page that prints the output.
you really need this


<?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>";

?>

ollie101
03-10-2009, 09:17 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum