By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,985 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,985 IT Pros & Developers. It's quick & easy.

A wildcard problem

P: 2
Happy new years folks,

I have a problem. I'm trying to set up a webpage using php to handle much of my link interactions. Now, I have several categories, and each will have its own set of links.

So, let us say that I want to be able to pick different links for each color, and each color below will have several possible subcategories or objects. I would far rather use wildcards to specify each list than have to put an incredibly long if/elseif statement.


Expand|Select|Wrap|Line Numbers
  1. <?php  
  2.         $pass = array('redtruck','redhouse','redhat','redshirt','redhair','redbicycle','redwagon','greenwagon','greentruck','greenshirt','greenlawn','greenhouse','greenhat','greenairplane','bluesky','bluejeans','bluebell','blueeyes','bluesguitar',);
  3.      if (in_array($_GET['id'], $pass)) {
  4.     if ($_GET['id'] == 'red' . '*'') {
  5.     include ($_SERVER['DOCUMENT_ROOT'].'/includes/redlinks.php'); }
  6.     elseif ($_GET['id'] == 'green' . '^') {
  7.     include ($_SERVER['DOCUMENT_ROOT'].'/includes/greenlinks.php'); }
  8.     elseif ($_GET['id'] == 'blue' , '*') {
  9.     include ($_SERVER['DOCUMENT_ROOT'].'/includes/bluelinks.php'); }
  10.     else {
  11.         header("HTTP/1.0 404 Not Found");
  12.         include ($_SERVER['DOCUMENT_ROOT'].'/includes/error.php');
  13. }
  14. ?>
  15.  
Is there a way to do this, without having to write an incredibly long if/elseif statement to check for every single possible 'red*' 'green*' and 'blue*' posibilities?
Jan 1 '08 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Valkrist. Welcome to TSDN!

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. if( ! empty($_GET['id']) )
  2. {
  3.     foreach
  4.     (
  5.         array
  6.         (
  7.             'red',
  8.             'green',
  9.             'blue'
  10.         )
  11.             as $__color
  12.     )
  13.     {
  14.         if( strpos($_GET['id'], $__color) === 0 )
  15.         {
  16.             include "{$_SERVER['DOCUMENT_ROOT']}/includes/{$__color}links.php";
  17.             $__found = true;
  18.             break;
  19.         }
  20.     }
  21. }
  22.  
  23. if( empty($__found) )
  24. {
  25.     header('HTTP/1.0 404 Not Found');
  26.     include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.php'
  27. }
  28.  
If $_GET['id'] exists and begins with one of those colors, then the matching colorlinks page will be loaded.

If $_GET['id'] does not exist or is invalid, the error page will be loaded.
Jan 2 '08 #2

P: 2
Thank you for your help. I've almost got it.

So, now what I have is something like this:

Expand|Select|Wrap|Line Numbers
  1. if(! empty($_GET['id']) )
  2.   {
  3.     foreach
  4.       (
  5.     array
  6.       (
  7.         'red',
  8.         'grn',
  9.         'blu',
  10.         'ora',
  11.         'top'
  12.       )
  13.     as $__passcolor1
  14.       )
  15.       {
  16.     if( strpos($_GET['id'], $__passcolor1) === 0 )
  17.         {
  18.             if(! empty($_GET['id']) )
  19.                 {
  20.                     foreach
  21.                         (
  22.                             array
  23.                                 (
  24.                                 'bicycle',
  25.                                 'car',
  26.                                 'house',
  27.                                 'pants',
  28.                                 'shirt',
  29.                                 'sweater',
  30.                                 'telephone',
  31.                                                                 )
  32.                             as $__passname1
  33.                         )
  34.                         {
  35.     if( strpos($_GET['id'], $__passname1) === 3 )
  36.                                 {
  37.                                     include ($_SERVER['DOCUMENT_ROOT'].'/includes/'.$_GET['id'].'.php');
  38.                                     $__found = true;
  39.                                     break;
  40.                                 }
  41.                             else
  42.                                 {
  43.                                 include ($_SERVER['DOCUMENT_ROOT'].'/includes/error.php');
  44.                                     $__found = true;
  45.                                     break;
  46.                                 }
  47.                         }
  48.                 }
  49.             }
  50.         }
  51.     }
  52. if(empty($__found) )
  53.     {
  54.         include "{$_SERVER['DOCUMENT_ROOT']}/includes/top_main.php";
  55.     }
  56.  
However, this only gives me 'redbicycle' as a valid option.. any other option I try gives me the error message.

I tried taking out the 'ELSE' and suddenly 'redcar' will pass correctly, but if I were to type in, say, 'reddentist', instead of an error message or the top_main.php page, I just get a blank field with nothing in it.

So, to sum up:

If I remove the 'ELSE' statement, then I can get the ids of 'redbicycle' and 'redcar' to work correctly, but I have no error for someone manually putting in 'redmybuttlolzz'.

If I leave the ELSE statement in, it ONLY allows the very first possible combinatino "red + bicycle" to pass, and any other combination of variables gets me an error message (even ones that passed correctly when there was no ELSE statement).

What am I doing wrong?
Jan 3 '08 #3

pbmods
Expert 5K+
P: 5,821
Heya, Valkrist.

substr() will be your friend here:

Expand|Select|Wrap|Line Numbers
  1. if( ! empty($_GET['id']) )
  2. {
  3.     foreach
  4.     (
  5.         array
  6.         (
  7.             'red'   => 3,
  8.             'green' => 5,
  9.             .
  10.             .
  11.             .
  12.         )
  13.         as $__color => $__len
  14.     )
  15.     {
  16.         if( strpos($_GET['id'], $__color) === 0 )
  17.         {
  18.             $__part2 = substr($_GET['id'], $__len);
  19.  
  20.             echo $__part2;
  21.             exit;
  22.         }
  23.     }
  24. }
  25.  
Jan 5 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.