Log in

View Full Version : Multiple pages in one PHP doc.



boxxertrumps
10-08-2006, 05:30 PM
i was looking earlier, and failing to properly imprement Tweys mysql login code.
(http://www.twey.co.uk/?q=loginscript)
and i was wondering how exactly the different pages were shown while keeping the php document the same?
i know that the
<?php
}

function foot() {
?>
peice echos the other pages and that
<?php
foot();
}
/*rest of code*/
defines the start of a page, but i have no clue as to how the code is placed. this has been bugging me for a while... oh, and i would like to beable to put my entire site like that, just without the login funtions. would make my time on my website alot better spent.

Twey
10-08-2006, 07:38 PM
Just echo different code depending on circumstances. I took this to extremes with the chat app I wrote on these forums a while back, which uses PHP and XHR, and which I shoved into one file, images and all.

djr33
10-09-2006, 12:26 AM
<?php
if ($_GET['page'] == "1") {
echo "<html>...this is page one...";
}
elseif ($_GET['page'] == "2") {
echo "....this is page two...";
}
else {
echo "...this is the default index page if there either is no value of ?page= or if it's value isn't already dealt with in the script, so they don't get a page not found error.";
}
?>


Also, you can use a switch statement to send it to functions... basically like above, but it calls a function instead of doing the action right there. This has it's uses too.

boxxertrumps
10-09-2006, 05:07 PM
awsome.
its allright to replace the double quotes around the echoed code with single ones right?
mostly because my website uses double quotes often and i read that
echo "<a href="url">"
would get you <a href= and then an error. (because the first quote interpreted as start and the second is interpreted with end)

Twey
10-09-2006, 06:03 PM
If you're echoing large blocks of HTML, you can drop out of PHP parsing mode altogether:
<?php if(isset($_GET['page']) && $_GET['page'] === '1') { ?>

<html>
<!-- Page 1 -->
</html>

<?php } else if(isset($_GET['page']) && $_GET['page'] === '2') { ?>

<html>
<!-- Page 2 -->
</html>

<?php } else { ?>

<html>
<!-- Default -->
</html>

<?php } ?>djr33: elseif is for lazy people. :)

boxxertrumps
10-09-2006, 07:48 PM
djr33's code and got this
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in C:\UTILITIES\Webserv\Abyss Web Server\htdocs\web.php on line 30

and this is line 30

<li><a href="Installers/RobotArena2.exe">Robot Arena</a>

so now im trying yours...and it works. thank you very much , Twey, you have helped me once again.

djr33
10-09-2006, 07:54 PM
There's no error in my code, but adding double quotes inside the quotes exits the quotes.

To use double quotes, yes, use single quotes around them. However, sometimes, you may need both in a string, so you can use either, then escape those quotes that match the outside ones.
For example: echo "<img src=\"image.jpg\">";
The backslashes escape them so they aren't used in the php but rather output to html. (The backslashes are just a php command... they aren't output to the html.)

And, yes, Twey's method works, but only if you're looking for lots of text. If you just want a couple lines of text, might want to use the other syntax. either is fine.... just that knowing both will help in the end.


Twey, why is elseif lazy?
It continues the same set of ifs. That means that ONLY if the first fails will the elseif execute. If the first is true, then the second won't execute (like if there was some weird way of setting the variable to two values, or if they weren't mutually exclusive for some reason), and that has been very helpful for me.
It's just like else { if (...) {.....}}, but less to type.
What's your alternative?

Twey
10-09-2006, 08:05 PM
elseif is a separate keyword that performs exactly the same function as a combination of "else" and "if." The braces are, as always, optional;
if(1) {

} else if(2) {

} is perfectly valid. There's really no need for elseif (heck, it saves a single character), and it's a bad habit into which to get, since most C-like languages don't bother implementing something so pointless.

djr33
10-09-2006, 08:07 PM
Ah, didn't realize you can just put a space there.

But if it's there, why not use it? ;)

But.... yeah, I see your point.

Twey
10-09-2006, 08:11 PM
Since the whole of the second if block counts as a single statement, there's no need to enclose it in braces.
But if it's there, why not use it?
it's a bad habit into which to get, since most C-like languages don't bother implementing something so pointless.:)