#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Better way to do this?


    Hi,

    I am starting out with PHP and I'm working on a small project to take an id and map it against other id's. Is there a better way to code this? The mapping of page_id's will expand. This is inside a javascript as you can see the echo is for a var which the js then reads.

    <?
    $raw_page_id = htmlspecialchars($_REQUEST['pageid'], ENT_QUOTES);

    if ( $raw_page_id == "83517543-blackberry") { echo "var pr_page_id = '83517543';"; }
    elseif ( $raw_page_id == "83517543-capuccino") { echo "var pr_page_id = '83517543';"; }
    elseif ( $raw_page_id == "83517543-blue") { echo "var pr_page_id = '83517543';"; }


    else{ echo "var pr_page_id = '$raw_page_id';";} ?>
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,004
    Rep Power
    9398
    Is it always the first number part of the page ID? There might be an easier way to fix this: are you using URL rewriting? What do you have in place for that?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    No. Basically it is a manual mapping of various product id's that we want to manually map to other product id's. This is code within a javascript and I need to change the var to the manually mapped product id.

    Originally Posted by requinix
    Is it always the first number part of the page ID? There might be an easier way to fix this: are you using URL rewriting? What do you have in place for that?
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,004
    Rep Power
    9398
    Well that sucks.

    You can stuff everything into a database and look it up there, or build a gigantic array of all the pairs.
    PHP Code:
    $lookup = array(
        
    "83517543-blackberry" => "83517543",
        
    "83517543-capuccino" => "83517543",
        
    // etc
    );

    echo 
    "var pr_page_id = '" . (isset($lookup[$raw_page_id]) ? $lookup[$raw_page_id] : $raw_page_id) . "';"

IMN logo majestic logo threadwatch logo seochat tools logo