October 16th, 2012, 04:20 PM
-
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';";} ?>
October 16th, 2012, 04:34 PM
-
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?
October 16th, 2012, 04:37 PM
-
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?
October 16th, 2012, 05:28 PM
-
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) . "';";