relative documents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Lookie23
Forum Newbie
Posts: 7
Joined: Fri Jan 10, 2003 5:43 am

relative documents

Post by Lookie23 »

Hello!

I'm a PHP newbie and i have a question regarding relative documents.
The thing is..i have a bunch of pics sorted in array. What i would like to do is shuffle them and with for loop put them into table. SO far so good, but i'm stuck with relative file naming... so...my script is called random.php, and its absolute path is c:\inetpub\wwwroot\phplearning\random.php.
I have all my pics in Pics folder, which is in c:\inetpub\wwwroot\phplearning\pics. So how could i form the src attribute for <img> tag, that i could access pics from my script?...and...if anyone is willing to help me...is it possible to explain that a bit? :wink:

oh..and just one more thing... i've tried putting pictures in phplearning folder and typed <img src = \"" and it worked..but i don't quiet get it (i've typed it from a book).
thanks in advance for any help or suggestions!

Luka
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Not sure I've entirely understood the question (apologies if I've got it all wrong) but if you're just trying to work out how to access an image in the pics folder from random.php you'd need an IMG tag something like:

Code: Select all

<img src="pics/myrandomimage.gif" width="x" height="y" alt="something" />
Mac
Lookie23
Forum Newbie
Posts: 7
Joined: Fri Jan 10, 2003 5:43 am

Post by Lookie23 »

hi!

here is my code..tested :)

<?
$pictures = array ("back.gif", "cat.gif", "cramps.gif", "dave.gif", "info.gif",
"jim.gif", "note.gif", "oxford.gif", "ring.gif", "strike.gif", "warning.gif");

shuffle ($pictures);
?>

<html>
<head><title>Random selection</title></head>
<body>
<center>
<h1>Random pics</h1>
<table width = 100%>
<tr>

<?
for ($i = 0; $i < 3; $i ++)
{
echo "<td align = center><img src = \"";
echo $pictures[$i];
echo "\" width = 100 height = 100></td>";
}
?>
</body>
</html>

What i don't understand... is \"" ..i mean...i know what it does...but i don't know how to explain it.
now..this works if the pictures are in the same folder as the script. What i would like to know...how to form src attribute...if pictures were in pics folder? I hope i was more clear now :roll:

Luka
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Okie dokie, right I think the thing that is probably causing you the most confusion is down to the way that the book you are using has set out the code if we change:

Code: Select all

echo "<td align = center><img src = ""; 
echo $pictures[$i]; 
echo "" width = 100 height = 100></td>";
to

Code: Select all

echo "<td align = center><img src = "".$pictures[$i]."" width = 100 height = 100></td>";
Then it's a bit easier to see what's going on - basically all the backslash (\) does is escape the double quote. You need to escape the double quotes because otherwise PHP will think that you've ended the echo statement and will get confused about everthing that follows it and will throw a wobbly. The ". and ." bits indicate to PHP that we are ending the string, joining it with something else and then finishing the string.

To avoid having to escape the double quotes you could put the string into single quotes like so:

Code: Select all

echo '<td align="center"><img src="'.$pictures[$i].'" width="100" height="100"></td>';
Then we can have as many double quotes as we like without having to worry about escaping them. Note that where before we had ". and ." we've now got '. and .'

Now to answer the actual question, to be able to access the images from within a pic's folder you would have to change the above to:

Code: Select all

echo '<td align="center"><img src="pics/'.$pictures[$i].'" width="100" height="100"></td>';
Some references: Mac
Lookie23
Forum Newbie
Posts: 7
Joined: Fri Jan 10, 2003 5:43 am

Post by Lookie23 »

Now i get it :D ... i really appreciate all your help. thanks!

Luka
Post Reply