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. -
<?php
-
$pass = array('redtruck','redhouse','redhat','redshirt','redhair','redbicycle','redwagon','greenwagon','greentruck','greenshirt','greenlawn','greenhouse','greenhat','greenairplane','bluesky','bluejeans','bluebell','blueeyes','bluesguitar',);
-
if (in_array($_GET['id'], $pass)) {
-
if ($_GET['id'] == 'red' . '*'') {
-
include ($_SERVER['DOCUMENT_ROOT'].'/includes/redlinks.php'); }
-
elseif ($_GET['id'] == 'green' . '^') {
-
include ($_SERVER['DOCUMENT_ROOT'].'/includes/greenlinks.php'); }
-
elseif ($_GET['id'] == 'blue' , '*') {
-
include ($_SERVER['DOCUMENT_ROOT'].'/includes/bluelinks.php'); }
-
else {
-
header("HTTP/1.0 404 Not Found");
-
include ($_SERVER['DOCUMENT_ROOT'].'/includes/error.php');
-
}
-
?>
-
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?
3 1635
Heya, Valkrist. Welcome to TSDN!
Try something like this: -
if( ! empty($_GET['id']) )
-
{
-
foreach
-
(
-
array
-
(
-
'red',
-
'green',
-
'blue'
-
)
-
as $__color
-
)
-
{
-
if( strpos($_GET['id'], $__color) === 0 )
-
{
-
include "{$_SERVER['DOCUMENT_ROOT']}/includes/{$__color}links.php";
-
$__found = true;
-
break;
-
}
-
}
-
}
-
-
if( empty($__found) )
-
{
-
header('HTTP/1.0 404 Not Found');
-
include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.php'
-
}
-
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.
Thank you for your help. I've almost got it.
So, now what I have is something like this: -
if(! empty($_GET['id']) )
-
{
-
foreach
-
(
-
array
-
(
-
'red',
-
'grn',
-
'blu',
-
'ora',
-
'top'
-
)
-
as $__passcolor1
-
)
-
{
-
if( strpos($_GET['id'], $__passcolor1) === 0 )
-
{
-
if(! empty($_GET['id']) )
-
{
-
foreach
-
(
-
array
-
(
-
'bicycle',
-
'car',
-
'house',
-
'pants',
-
'shirt',
-
'sweater',
-
'telephone',
-
)
-
as $__passname1
-
)
-
{
-
if( strpos($_GET['id'], $__passname1) === 3 )
-
{
-
include ($_SERVER['DOCUMENT_ROOT'].'/includes/'.$_GET['id'].'.php');
-
$__found = true;
-
break;
-
}
-
else
-
{
-
include ($_SERVER['DOCUMENT_ROOT'].'/includes/error.php');
-
$__found = true;
-
break;
-
}
-
}
-
}
-
}
-
}
-
}
-
if(empty($__found) )
-
{
-
include "{$_SERVER['DOCUMENT_ROOT']}/includes/top_main.php";
-
}
-
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?
Heya, Valkrist. substr() will be your friend here: -
if( ! empty($_GET['id']) )
-
{
-
foreach
-
(
-
array
-
(
-
'red' => 3,
-
'green' => 5,
-
.
-
.
-
.
-
)
-
as $__color => $__len
-
)
-
{
-
if( strpos($_GET['id'], $__color) === 0 )
-
{
-
$__part2 = substr($_GET['id'], $__len);
-
-
echo $__part2;
-
exit;
-
}
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
by: Jan Kucera |
last post by:
Hi,
does anybody know about wildcard mapping ASP.NET 2 in IIS6?
Any tutorial?
Thanks,
Jan
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |