Connecting Tech Pros Worldwide Forums | Help | Site Map

Echo array elements

sheel331@yahoo.com
Guest
 
Posts: n/a
#1: Mar 24 '08
Hello, I am new to coding in PHP, and had a question about a simple
thing:

I am trying to echo out the elements of an array for a sidenav in one
of my HTML pages, and I can't seem to figure out the syntax for
appending the element of the array into the path to the file. Here is
the code I have:

<?php
include 'c:\inetpub\webroot\sheel\template\navbar.html';

$fullPath = explode('/', $_SERVER['PHP_SELF']);
echo "The foldername $fullPath[0]";
echo "The filename $fullPath[1]";
echo 'c:\inetpub\webroot\sheel\template\'.$($fullPath[1]).".html";

$filename = 'c:\inetpub\webroot\sheel\template\' .
$fullPath[1].'.html';
$filename2 = 'c:\inetpub\webroot\sheel\template\' .
$fullPath[1].'.html';

if (file_exists($fileName)) {
echo "c:\inetpub\webroot\sheel\template\$fullPath[1].html";
include 'c:\inetpub\webroot\sheel\template\'.
$fullPath[1].'.html';}
else if (file_exists($fileName2)) {
echo "c:\inetpub\webroot\sheel\template\$fullPath[2].html";
include 'c:\inetpub\webroot\sheel\template\'.
$fullPath[2].'.html';}
else{
echo "<ul id=\"secondaryNav\" ></ul>";}
?>

Thanks for your help!
Sheel

sheldonlg
Guest
 
Posts: n/a
#2: Mar 24 '08

re: Echo array elements


sheel331@yahoo.com wrote:
Quote:
Hello, I am new to coding in PHP, and had a question about a simple
thing:
>
I am trying to echo out the elements of an array for a sidenav in one
of my HTML pages, and I can't seem to figure out the syntax for
appending the element of the array into the path to the file. Here is
the code I have:
>
<?php
include 'c:\inetpub\webroot\sheel\template\navbar.html';
>
$fullPath = explode('/', $_SERVER['PHP_SELF']);
echo "The foldername $fullPath[0]";
echo "The filename $fullPath[1]";
echo 'c:\inetpub\webroot\sheel\template\'.$($fullPath[1]).".html";
>
$filename = 'c:\inetpub\webroot\sheel\template\' .
$fullPath[1].'.html';
$filename2 = 'c:\inetpub\webroot\sheel\template\' .
$fullPath[1].'.html';
>
if (file_exists($fileName)) {
echo "c:\inetpub\webroot\sheel\template\$fullPath[1].html";
include 'c:\inetpub\webroot\sheel\template\'.
$fullPath[1].'.html';}
else if (file_exists($fileName2)) {
echo "c:\inetpub\webroot\sheel\template\$fullPath[2].html";
include 'c:\inetpub\webroot\sheel\template\'.
$fullPath[2].'.html';}
else{
echo "<ul id=\"secondaryNav\" ></ul>";}
?>
>
Thanks for your help!
Sheel

I have not followed your code, but I noticed one thing that I do
differently. I would always write this line:

echo "c:\inetpub\webroot\sheel\template\$fullPath[1].html";

as

echo "c:/inetpub/webroot/sheel/template/" . $fullPath[1] . ".html";
or

echo 'c:/inetpub/webroot/sheel/template/' . $fullPath[1] . '.html';

That is, I would separate the string from the variable explicitly and I
would use unix notation. Otherwise (I'm not 100% certain), you may well
need

echo "c:\\inetpub\\webroot\\sheel\\template\\" . $fullPath[1] . ".html";

since the backslash is a quoting character.

Shelly
Closed Thread