...

View Full Version : echoing html code- Help. please



Keyser520
02-14-2006, 03:31 PM
Ok, so I have the following code


<?php
$left="http://www.runeescape.net/theaddiction/left.gif";
$right="http://www.runeescape.net/theaddiction/right.gif";
$link="/theaddiction/";
echo "<img src=".$link.$z.".jpg>";
echo "<a href=index.php?page=".$prev."><img src=".$left." align=left></a>"."<a href=index.php?page=".$next."><img src=".$right." align=right></a>";
?>


Which, for the time being, let's assume that $next=2 and $prev=0

So the portion

echo "<a href=index.php?page=".$prev."><img src=".$left." align=left></a>"."<a href=index.php?page=".$next."><img src=".$right." align=right></a>";


outputs
<a href=index.php?page=0><img src=http://www.runeescape.net/theaddiction/left.gif align=left></a><a href=index.php?page=2><img src=http://www.runeescape.net/theaddiction/right.gif align=right></a>

however, I want it to say

<a href="index.php?page=0"><img src="http://www.runeescape.net/theaddiction/left.gif" align=left></a><a href="index.php?page=2"><img src="http://www.runeescape.net/theaddiction/right.gif align=right"></a>

So, obviously, the problem is that it won't be valid html code without the "" surrounding the url's. How can I enter the " in since they indicate a closing portion of the string?

SpirtOfGrandeur
02-14-2006, 03:37 PM
\"
Should work

degsy
02-14-2006, 03:42 PM
You have 3 main options for mixing HTML and PHP

Escape slashes


$str = "<a href=\"page.php?id=$myvar\">Page</a>";


Use single quotes and break of the string


$str = '<a href="page.php?id=' . $myvar . '">Page</a>';


Break in and out of HTML


<a href="page.php?id=<?php echo $myvar; ?>">page</a>

If you have a lot of HTML then this is a good option.

Keyser520
02-14-2006, 04:09 PM
You have 3 main options for mixing HTML and PHP

Escape slashes


$str = "<a href=\"page.php?id=$myvar\">Page</a>";


Use single quotes and break of the string


$str = '<a href="page.php?id=' . $myvar . '">Page</a>';


Break in and out of HTML


<a href="page.php?id=<?php echo $myvar; ?>">page</a>

If you have a lot of HTML then this is a good option.

A cool. I didn't know you could just pop php in the html code like that. I like option 3. THanks.

Vin0rz
02-14-2006, 04:59 PM
You can also use the heredoc syntax, meaning that you say
echo <<<SOMENAME
//Things you want to echo
SOMENAME; // No whitespace before this

Just as with double quotes, variables are converted. However, you do not need to escape the double quotes. Though the third way degsy describes is faster, you probably want to know this as you might come across this in code examples or something ;)

Keyser520
02-14-2006, 05:11 PM
You can also use the heredoc syntax, meaning that you say
echo <<<SOMENAME
//Things you want to echo
SOMENAME; // No whitespace before this

Just as with double quotes, variables are converted. However, you do not need to escape the double quotes. Though the third way degsy describes is faster, you probably want to know this as you might come across this in code examples or something ;)

not sure I understand what you are saying, but I have a new question now.

If I want to do the code

while (condition)
{
do this stuff
}

but I want it to be while ((NOT(condition))

What do I type in?

the condition in question is file_exists so it will return a boolean

Vin0rz
02-14-2006, 05:18 PM
More info about heredoc (http://nl3.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

You just place an exclamation mark (!) in front of it, so if(!file_exists(...))

Keyser520
02-14-2006, 05:31 PM
More info about heredoc (http://nl3.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

You just place an exclamation mark (!) in front of it, so if(!file_exists(...))


I'm having problems getting file exist to work.. What's wrong with this code?

assume that $test does, in fact = 0
$prevchap = 4
$chap = 5
$sec =1

I know the above as I echoed them to the screen. What I can't figure it is why it's not running the while loop...


if ($test==0)
{
$prevchap=$chap-1;
$prevsec=0;
do
{
$prevsec=$prevsec+1;
echo "prevchap: ".$prevchap." - prevsec: ".$prevsec."<br>";
echo "http://www.runeescape.net/theaddiction/chapters/".$prevchap."-".$prevsec.".txt<br>";
}
while (file_exists("http://www.runeescape.net/theaddiction/chapters/".$prevchap."-".$prevsec.".txt"));
$prevsec=$prevsec+1;
}
else
{
$prevsec=$sec-1;
$prevchap=$chap;
}

This is the output on the screen

prevchap: 4 - prevsec: 1
http://www.runeescape.net/theaddiction/chapters/4-1.txt
next file=-2.txt

Keyser520
02-14-2006, 05:41 PM
ok, i've simplified it and still can't figure it out.


$filename="http://www.runeescape.net/theaddiction/chapters/".$prevchap."-".$prevsec.".txt";
echo $filename."<br>";
if (file_exists($filename))
{
echo "i'm here<br>";
}
else
{
echo "not there<br>";
}

and it's telling me that it's "not there"

despite the fact that "http://www.runeescape.net/theaddiction/chapters/4-1.txt" is most definetly there (type it into your browser)

Vin0rz
02-14-2006, 05:49 PM
ok, i've simplified it and still can't figure it out.


$filename="http://www.runeescape.net/theaddiction/chapters/".$prevchap."-".$prevsec.".txt";
echo $filename."<br>";
if (file_exists($filename))
{
echo "i'm here<br>";
}
else
{
echo "not there<br>";
}

and it's telling me that it's "not there"

despite the fact that "http://www.runeescape.net/theaddiction/chapters/4-1.txt" is most definetly there (type it into your browser)

Well, with double-quotes you don't need to escape the string, so either you do

$filename="http://www.runeescape.net/theaddiction/chapters/{$prevchap}-{$prevsec}.txt";
echo "$filename <br>";
if (file_exists($filename))
{
echo "i'm here<br>";
}
else
{
echo "not there<br>";
}

Or the faster way:

$filename='http://www.runeescape.net/theaddiction/chapters' . $prevchap . '-' . $prevsec . '.txt';
echo $filename . '<br>';
if (file_exists($filename))
{
echo 'i\'m here<br>';
}
else
{
echo 'not there<br>';
}

Don't know if the problem lies there, but try this first.

Keyser520
02-14-2006, 06:39 PM
ok, why is this not working?


if (file_exists("http://www.runeescape.net/theaddiction/chapters/4-1.txt"))
{
echo "i'm here<br>";
}
else
{
echo "not there<br>";
}

It's returning "not there" but the dang file exists :-(

Vin0rz
02-15-2006, 08:22 AM
I have no idea, actually... Sorry that I couldn't help but I wouldn't want you waiting for an answer not to come ;)

degsy
02-15-2006, 03:46 PM
file_exists may be local only.

Try this


<?php
$url = "http://www.runeescape.net/theaddiction/chapters/4-1.txt";

if (@fopen($url, "r")) {

echo "File Exists";

} else {

echo "Can't Connect to File";

}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum