473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A wildcard problem

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
3 1635
pbmods
5,821 Expert 4TB
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
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
5,821 Expert 4TB
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

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

Similar topics

1
by: Generic Usenet Account | last post by:
Here's the requirement that I am trying to satisfy: Handle wildcards in STL such that the wildcard entry is encountered before non-wildcard entries while traversing containers like sets and...
1
by: deko | last post by:
I have a form where users can enter a string with asterisks to perform a wildcard search. Currently, the string entered by the user looks like this: *somestring* The purpose is to match any...
2
by: Ken Yee | last post by:
First a little background: I've written an httphandler to handle wildcard extensions (i.e., I want to handle all URLs that come in rather than just URLs w/ a specific file extension so I can give...
3
by: george.lengel | last post by:
Hello experts, I have been struggling for days to solve this problem and every suggestion I find via Google does not work for me. There is probably a solution out there that will do what I want,...
78
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that...
2
by: Jan Kucera | last post by:
Hi, I have virtual MyFolder/* and MyFolder/MySubFolder/* mapped to an httphandler in the web.config file and all works perfectly on the asp.net development server. However on the IIS6/Win2003 I'm...
2
by: guoqi zheng | last post by:
Dear Sir, I would like all files on my application to pass asp.net, so I added aspnet_isapi.dll to the Wildcard application maps. After that, many things changed. I used to be able to visit my...
0
by: Thomas | last post by:
in .net 1.1 we successfully use a HttpModule to catch 404 / 403.1 html errors. after migrating to .net 2.0, this modules is broken in a very, very strange way. we have defined a wildcard...
6
by: Jan Kucera | last post by:
Hi, does anybody know about wildcard mapping ASP.NET 2 in IIS6? Any tutorial? Thanks, Jan
2
by: googlegroups.dsbl | last post by:
I'm really confused here, and am wondering if someone knows what could be the issue with my TableAdapter query. A few months ago, I created a really neat program that has th ability to search by...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.