Jump to content
xisto Community
Sign in to follow this  
matak

More Dynamic ?id=browsing With Php (associative Array) Just create array and watch php code do the rest

Recommended Posts

The thing that has been bugging me for a while was that switch statement that we use to create ID browsing (some use If-Ifelse but results are the same for both). I wanted to figure out a way to use more dynamic switch statement so that i only need to update my links array in order to create links for template. With use of foreach, array_keys, and in_array functions finally i managed to do so. Also i'm planing on changing foreach with array_walk but i'll do that later. Now for the code..

 

First we create an associative array something like this

 

$glavni_linkovi = array(			"Index" => "glavna.html", 			"Link1" => "link1/index.html", 			"Link2" => "link2/index.html", 			"Link3" => "link3/index.html"		);

After we loop through it with foreach function to create our dynamic links :( (i just love the word dynamic)

 

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

Then we write down this code

 

$page = $_GET['p'];

I'll quote a fellow trapper on that code

 

<?php starts php code :(

$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 :()


and use array_keys to create a new string that holds keys of our main array ($glavni_linkovi). That trick is needed beacouse in_array function that i used later doesn't check for keys in associative array, but for values. And i used keys as my ID's for browsing.. :lol: Managed to confuse myself with this sentence

 

$linkovi = array_keys($glavni_linkovi);

And after all that a function that i'm proud off..

 

if (in_array($page, $linkovi)){	foreach ($glavni_linkovi as $gl_li => $gl_li_path) {		switch($page) {			case $gl_li:			include $gl_li_path;			break;		}	}}elseif ($page == ""){	include $glavni_linkovi['Index'];}else {	echo "	<h1>Started a ban function! :)</h1>			<p>As soon as i learn to create one.</p>			<p>Your IP address is: ".$REMOTE_ADDR."</p>";}?>

First part where in_array checks is ID value in array $linkovi, and switch statement that dynamicly changes ID based on it's value.. Wohoo.. I just love PHP..

Second part elseif checks what array is used for loading page when ID=="".. I would like to write better code for this so i can maybe change Index value and that it sets [0] value of array for ID=="", but i don't know how. Maybe some people here who now how to code can help.

And thanks to jlhaslip notice, the third ELSE part starts a ban function when "hacker" tries to enter other values in browser, in order to inject some malicious code.

 

All you need to do now is to change values of $glavni_linkovi array and rest is done with the code :lol:

 

Here is a working example... And here is a download zippy..

 

Hope you like the code, and again thanks to all coders that share :(

Matak

Share this post


Link to post
Share on other sites

Yeah, that code is faster than friiks, but an easyer code is the Include() function. I'm not exactly sure how it works, but you put the code you want on that page in a html, php, etc file. Say I want this-

<b>Hello</b>

I put that and name it bold.html. the i go back to the file that has the include() function and put this-

include("bold.html");

You keep doing that howevermany times you need to. The link after the sentance on when I asked a question and it got awnsered with the include() function which alot of people say is helpful. {link}

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.