A wildcard problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Valkrist
    New Member
    • Jan 2008
    • 2

    #1

    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.


    [CODE=php]
    <?php
    $pass = array('redtruck ','redhouse','r edhat','redshir t','redhair','r edbicycle','red wagon','greenwa gon','greentruc k','greenshirt' ,'greenlawn','g reenhouse','gre enhat','greenai rplane','bluesk y','bluejeans', 'bluebell','blu eeyes','bluesgu itar',);
    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');
    }
    ?>
    [/CODE]

    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?
    Last edited by pbmods; Jan 2 '08, 03:04 AM. Reason: Fixed a code typo that is not relevant to the problem.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Valkrist. Welcome to TSDN!

    Try something like this:
    [code=php]
    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'
    }
    [/code]

    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.

    Comment

    • Valkrist
      New Member
      • Jan 2008
      • 2

      #3
      Thank you for your help. I've almost got it.

      So, now what I have is something like this:

      [CODE=php]
      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($__fou nd) )
      {
      include "{$_SERVER['DOCUMENT_ROOT']}/includes/top_main.php";
      }
      [/CODE]

      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?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Valkrist.

        substr() will be your friend here:

        [code=php]
        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;
        }
        }
        }
        [/code]

        Comment

        Working...