22, August 2008

Code works for passing variables in URL, but maybe you have suggestions? - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Code works for passing variables in URL, but maybe you have suggestions?
« previous next »
Pages: [1] Print

Author Topic: Code works for passing variables in URL, but maybe you have suggestions?  (Read 962 times)
My Name is Enigo Montoya
*
Posts: 33
188 credits
Members referred : 0


« on: Dec 02, 2006, 08:36:14 PM »

Hi, 

Thank you for the nice scripts.  Atleast I am now registerd so I can ask a question in the future if I need to.  I worked a little bit  more with the following code I had, and believe I found a solution.  Someone may know a slightly different way to do it a little better.  I wanted something to be able to pass the variable in the URL like snippets.php?id=12  The example below would work for text updates, for instance where you have the snippets listed each id is text with links.  I wanted to see if there was a way to be able to include a file because changing the content in the below code could get messy with tables, graphics...etc.  in between the php tags I just did <?php include("mypage.php"); ?> so I can keep a possibly complicated structure in a seperate file wihout having to search through the following code to find out what page is which.  I also tested this without the HTML tags and included the php into a certain portion of a php page so only that portion changes.

Thank you,
Brad
------------------------------------

<html>
<head>
<title>Page Test</title>
</head>
<body>

<?php
if ($_GET['id'] == "") {
?>

<h1>Welcome</h1>
<p>id homepage</p>

<?php
} elseif ($_GET['id'] == "10") {
?>

<h1>ID 10</h1>
<p>THIS IS PAGE 10</p>

<?php
} elseif ($_GET['id'] == "15") {
?>

<h1>ID 15</h1>
<p>THIS IS PAGE 15</p>

<?php
} else {
?>

<h1>Sorry, no page here</h1>

<?php
}
?>

</body>
</html>
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8020
41077 credits
Members referred : 3



« Reply #1 on: Dec 02, 2006, 08:43:24 PM »

When you have lots of if statements is better to use a function like switch()

eg.

switch($_GET['id']){
 case 
1:
  echo 
'XXX';
  break;
 case 
2:
  echo 
'CCC';
  break;
 default :
  echo 
'No page found';
  break;
}

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6300
38626 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Dec 02, 2006, 08:45:11 PM »

check this php function:
http://www.php.net/switch Visit through proxy


Last blog : Create custom backups from your website using cURL
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6300
38626 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Dec 02, 2006, 08:45:50 PM »

shit nick you was to fast  Angry


Last blog : Create custom backups from your website using cURL
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8020
41077 credits
Members referred : 3



« Reply #4 on: Dec 02, 2006, 08:46:21 PM »

shit nick you was to fast  Angry

Hehe, I beat you Tongue

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6300
38626 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Dec 02, 2006, 08:47:10 PM »

yes but I moved the thread before you realized that this is not about a script of mine

hehe...


Last blog : Create custom backups from your website using cURL
My Name is Enigo Montoya
*
Posts: 33
188 credits
Members referred : 0


« Reply #6 on: Dec 03, 2006, 09:56:56 PM »

Thank you both for your reply!  I will look into the "switch" function.
My Name is Enigo Montoya
*
Posts: 33
188 credits
Members referred : 0


« Reply #7 on: Dec 06, 2006, 05:56:22 AM »

I was unfamiliar with the switch function, but there are some really good ways to make the code super clean.  This also makes since that it executes faster because it doesn't look down through each if/else statement.

Case "page": $inc = 'page.php';
Break;

include ($inc);

--------------
I also added parts like       
Case "":
echo "Empty string";
Break;


I do have one other question though.  With a default value set to like index.php how can I show a case that doesn't exist?

For instance, if someone types switch.php?id=abc and there is no case that defines "abc" how can the case be written to echo "page not found"?  I haven't applied this to anything yet, but as I have it now if someone would type in abc as the string it shows the default page and not that the string doesn't exist.

Thank you,
Brad
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6300
38626 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Dec 06, 2006, 09:34:38 AM »

You can put all possible values into an array and test the value against this array with in_array Visit through proxy


Last blog : Create custom backups from your website using cURL
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8020
41077 credits
Members referred : 3



« Reply #9 on: Dec 06, 2006, 11:18:22 AM »

Here is something that I commonly use :

Code:
<?php

$target 
= isset($_GET['screen'])?$_GET['screen']:'main';
if ( 
file_exists('./content/' $target '.php') )
   include 
'./content/' $target '.php';
else
   echo 
'404 error';

With this code, you are safe from XSS attacks, and you are supposed to have a directory with your content which will hold one php file for each page, while you have a way to know that someone asks for a page that does not exists.

Another benefit of this code is that it has the lower possible overhead, as with a switch() you could have problem if you had hundrends of pages.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
My Name is Enigo Montoya
*
Posts: 33
188 credits
Members referred : 0


« Reply #10 on: Dec 07, 2006, 06:06:46 AM »

Thank you Olaf.  That is one of the nice things about php, many times there is more than one way to do something.

Thank you Nikolas.  Excellent piece of code you provided.  Ironically the last few sites I have been working on I put the files to be included into a folder named "content" so I didn't have multiple pages in the root directory.  I know what you mean, it could get very messy with defining all the case statements while using the switch function.  The code you provided will work great with building a template type site where the majority of the content will remain the same.  It is shocking that a simple script does so much.
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=5013
Tags : php design html graphics snippets Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Code works for passing variables in URL, but maybe you have suggestions?
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Aug 22, 2008, 03:43:59 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.084
Total Topics: 7.439
Total Members: 3.807
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: marthawelch

28 Guests, 3 Users online :

16 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.