Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> More Dynamic ?id=browsing With Php (associative Array), Just create array and watch php code do the rest
matak
post Mar 24 2007, 03:43 PM
Post #1


Super Member
*********

Group: Members
Posts: 413
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



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

CODE
$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 tongue.gif (i just love the word dynamic)

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


Then we write down this code

CODE
$page = $_GET['p'];


I'll quote a fellow trapper on that code

QUOTE(friiks @ Feb 28 2007, 09:43 PM) *
<?php starts php code tongue.gif
$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 tongue.gif)


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.. blink.gif Managed to confuse myself with this sentence

CODE
$linkovi = array_keys($glavni_linkovi);


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

CODE
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 wink.gif

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

Hope you like the code, and again thanks to all coders that share cool.gif
Matak
Go to the top of the page
 
+Quote Post
Imtay22
post Apr 9 2007, 10:24 PM
Post #2


Super Member
*********

Group: [HOSTED]
Posts: 299
Joined: 27-January 07
From: Winter is cold here.
Member No.: 37,984
Spam Patrol



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-

CODE
<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-

CODE
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}
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How do you test your php code(86)
  2. Php Clock(7)
  3. Get Filename Of Referring Url(8)
  4. Change Permission With Php Code(3)
  5. Adapting Html Code Embed To Work On Phpnuke(7)
  6. Dynamic Image / Signature Generator(12)
  7. Wap Source Code Viewer(4)
  8. Dynamic Php Pages(5)
  9. Can I Make Dynamic Menu In Php(7)
  10. Trap17 Board Status Dynamic Image(13)
  11. Using Multiple Selection Array In Table To Order Data(1)
  12. Whats Wrong>?(9)
  13. Getting An Array Value Of A Dynamic Variable(9)
  14. Awesome Source Code Viewer Script(7)
  15. Html Code Tester. Online Script(15)
  1. I Need Some Proof Reading For My Code Please! [resolved](7)
  2. Will This Code Work(5)
  3. Use Rss In Php Code(3)
  4. Remove A Value From A Php Array Based On Its Value(5)
  5. Php Code(15)
  6. Php Code Needed(5)
  7. Php Code?(12)
  8. Php Code Needed Iii(10)
  9. Php And Mysql Programming(2)
  10. Malicious Code Injection(3)
  11. Php Source Code Unveiled In Browser?(7)
  12. Create Table - Mysql Code - Help(1)
  13. Php Code For Login Form With Validation In Php(6)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 02:43 AM