Jump to content
xisto Community
Sign in to follow this  
friiks

Making The Popular Id Browsing For Your Site.

Recommended Posts

Was just sitting and being bored but then I realized I could show how to create more or less popular ?id=page browsing.

It's actually really easy. I know two ways how to do it. First one I learned was checking the variable and if it's true including the text/file/anything needed and so on. It was ok, but sometimes I just couldn't make it work so I switched to switch() function and that's what I'm going to show you guys right now.

 

So, I made a test page which contains the code needed and here is its source.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Untitled</title></head><body><a href="?p=idx">Home</a> - <a href="?p=pagetwo">Page Two</a> - <a href="?p=pagethree">Page Three</a><br><?php$page=$_GET['p'];switch($page){default:echo 'This is the default page';break;case 'pagetwo':echo 'This is page two';break;case 'pagethree':echo 'This is page three<br>Cool, huh?';}?></body></html>

Okies, I'll split up the code and explain it to you.

I'll start with the links

<a href="?p=idx">Home</a> - <a href="?p=pagetwo">Page Two</a> - <a href="?p=pagethree">Page Three</a><br>
Those are normal html links only their href leads to links like ?p= ...well replace p with letter or a word you want - mind that you can't put spaces there..well, maybe you can but..don't do that.

 

Now for the PHP.

<?php$page=$_GET['p'];
<?php starts php code :P

$page=$_GET['p']; sets the $page variable to the page requested. Um, you have to change p in $_GET['p'] to word/letter you use for your links. $_GET gets the info from the url (in this tutorial :()

 

switch($page){default:echo 'This is the default page';break;case 'pagetwo':echo 'This is page two';break;case 'pagethree':echo 'This is page three<br>Cool, huh?';}
switch($page){ Um, this part of code uses switch function to switch parts of code by using the $page variable.

default: sets the default page that will be shown when user inputs an invalid url and the content that will be shown in index page.

You see the echo() function in next line. You can input anything there...echo text/include file...use your imagination. Just put what you want to see in the default page.

break; ends execution of the current switch structure. Can't really explain it but that.

 

Ok, now for the part you want to see so bad.

case 'pagetwo': ...see anything in common with the previous code I've shown? If you guessed <a href='?p=pagetwo'> then you are correct. If the $page variable is set to pagetwo switch function will switch the content to the content you will put after the colon. Same thing works for the rest of the code. You add a case'': checking for a variable, put the code to show it is true, use break to break the code and add next case. When you have added all the pages you want you just put a curved bracket (Like here I have done all my pages and want to end it.

case 'pagethree':echo 'This is page three<br>Cool, huh?';}
) and voila, your links are working.

 

 

Hope that helps you at least a little. Ask if you have any questions, corrections or suggestions.

 

Oh, and here's the link to a preview.

 

Cheers, Matt.

Edited by friiks (see edit history)

Share this post


Link to post
Share on other sites

Cool dude, very useful.
When I used to go to 64Digits I always wondered how they used that system.
Now, that you've used it on my site I was able to see the code and learned greatly from that. :P

Very nice example, and I know everybody on this forum will agree when they get on to see it. :(

Edited by JDameron91 (see edit history)

Share this post


Link to post
Share on other sites

Show me the code you're using ,please!

Share this post


Link to post
Share on other sites

this code works for me...

<a href="?p=idx">Home</a> - <a href="?p=pagetwo">Page Two</a> - <a href="?p=pagethree">Page Three</a><br><?php$page=$_GET['p'];switch($page){default:echo 'This is the default page';break;case 'pagetwo':echo 'This is page two';break;case 'pagethree':echo 'This is page three<br>Cool, huh?';}?>

Share this post


Link to post
Share on other sites

this code works for me...

<a href="?p=idx">Home</a> - <a href="?p=pagetwo">Page Two</a> - <a href="?p=pagethree">Page Three</a><br><?php$page=$_GET['p'];switch($page){default:echo 'This is the default page';break;case 'pagetwo':echo 'This is page two';break;case 'pagethree':echo 'This is page three<br>Cool, huh?';}?>

It displays this-

Home - Page Two - Page ThreeCool, huh?'; } ?>

On every page
Edited by Imtay22 (see edit history)

Share this post


Link to post
Share on other sites

I don't know what the problem is but default: should always be the last case statement:

 

<a href="?p=idx">Home</a> - <a href="?p=pagetwo">Page Two</a> - <a href="?p=pagethree">Page Three</a><br><?php$page=$_GET['p'];switch($page){case 'pagetwo':echo 'This is page two';break;case 'pagethree':echo 'This is page three<br>Cool, huh?';break;default:echo 'This is the default page';}?>

Maybe that's the problem.

Share this post


Link to post
Share on other sites

Wow, I never knew that..But anyways I've been always using it this way AND it has always worked..

Share this post


Link to post
Share on other sites

Woah, I had a typo in my code :(Corrected it, thanks to matak for pointing it out :P

Share this post


Link to post
Share on other sites

wow thanks for putting this tutorial up i have been to many sites where it just shows the page name in the href i always thought it was the way they had name there pages but it is all just a simple php code. I am going to start using this.

Share this post


Link to post
Share on other sites

Hello Friiks, again I have a question about this syntax. I have multiple lines of code i would like to put in the

echo 'This is page two';
part of the code. When i try to have line breaks, it comes up with-

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/imtay22/public_html/runescape/news.php on line 16

Help?!

Share this post


Link to post
Share on other sites
echo 'This<br>is<br>a<br>linebreaked<br>text<br>w00t';[code]oryou can use echo like this...[code]echo <<<ENDYour long linebreaked etc. text hereEND;
Im too tired and sleepy to explain so just google echo php and look for php.net ..

Share this post


Link to post
Share on other sites

Anyone reading this Thread might also be interested in this reply to a question about implementing Friik's code by another member.
Actually it is the same question as Imtay posted...

Share this post


Link to post
Share on other sites

After a while struggling with PHP arrays in order to build even more dynamic, more like administrator friendly dynamic links i figured out it could be done this way. First we need to build ourselves an associative array like this

$glavni_linkovi = array(			"Index" => "This is the index file!", 			"Link1" => "This is path to link1!!", 			"Link2" => "This is path to link2!!", 			"Link3" => "This is path to link3!! :)"		);

After we made our array with foreach statement we can make our links. I made it something like this

foreach ($glavni_linkovi as $gl_li => $gl_li_path) {	echo '<a href="?p='.$gl_li_path.'">'.$gl_li.'</a> ';}

Now we go to the part where we use $_GET like posted above to use that associative array in our page. Code looks a bit messy but it's couse i didn't know any other way to use Default switch with foreach statement.. Here's the code

$page = $_GET['p'];if ($page == "") {	echo $glavni_linkovi['Index'];}else {foreach ($glavni_linkovi as $gl_li => $gl_li_path) {switch($page) {	case $gl_li_path:	echo $gl_li_path;	break;}}}

This can be quite handy beacouse we don't need to write switch statement every time when we create link. Also we don't need to write links manualy, beacouse they are created automaticly when added to associative array..
Hope u Like this example. Here's how it works. Link. And here's the download zippy if you don't understand how to implement it in webpage..Zippy :lol:

Thanks to all Xisto coders!
matak

Share this post


Link to post
Share on other sites

Nice code, but use the following query string to see what happens.

p=test
You need to find a way to always end up at the index.php page, otherwise the client gets a header with no "page" content.
Also, move the array outside the page code to a text file so the entire page doesn't need to be editted, only the text file. Makes it a bit more user friendly. ( and safer to update the link array without damaging the page ) :lol:
Keep trying.

http://forums.xisto.com/topic/35271-templating-system-using-php-includes-building-a-dynamic-site-using-includes-and-flat-files/
(I'll send the zip if you want. I took it off the net due to a small (potential) security problem)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.