Pages: [1]   Go Down
Author Topic: Dynamic Inclusion with Arrays  (Read 38 times)
mikeruption
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 2


WWW
« on: July 25, 2010, 01:36:29 PM »

Hi!

I'm new to PHP and I'm having trouble using an array within my script to replace the string from a variable obtained via the URL with the actual name of a directory.  The code is as follows:
Code:
<?php
$url '';
if (!empty($_GET['category'])) {
$url .= $_GET['category'] . '/';
}
if (!empty($_GET['product'])) {
$prod = array('stationery pads'=>'01 stationery pads'
'greeting cards'=>'02 greeting cards'
'magnetic pads'=>'03 magnetic pads',
'journals with locks'=>'04 journals with locks',
'spiral notebooks'=>'05 spiral journals',
'sets'=>'90 sets',
'featured'=>'99 new - featured');
$url .= $prod[$_GET['product']] . '/';
}
if (!empty($_GET['page'])) {
$url .= $_GET['page'] . '.php';
}
echo $url;
?>


Again, I'm new to PHP and I'm sure it's a simple remedy to fix my coding.  Any help you can provide would be greatly appreciated.  Thanks!   Smiley

- Mike
Report to moderator   Logged

mikeruption
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 2


WWW
« Reply #1 on: July 26, 2010, 08:49:37 PM »

OMG!  The above code actually works.  Unfortunately, I didn't realize it until it tried a different approach.  Here's the alternate code:
Code:
<?php
$url '';
if (!empty($_GET['category'])) {
$url .= $_GET['category'] . '/';
}
if (!empty($_GET['product'])) {
$prod  $_GET['product'];
$prodlist = array("stationery pads""greeting cards""magnetic pads""journals with locks""spiral notebooks""sets""featured");
$actprod   = array("01 stationery pads""02 greeting cards""03 magnetic pads""04 journals with locks""05 spiral journals""90 sets""99 new - featured");
$actproddir str_replace($prodlist$actprod$prod);
$url .= $actproddir '/';
}
if (!empty($_GET['page'])) {
$url .= $_GET['page'] . '.php';
}
echo $url;
?>


My mistake was that the variable I was trying to pass through wasn't plural.   Embarrassed

I've learned something from this mistake and I hope others can too.  Always double check your spelling.  Smiley

- Mike
Report to moderator   Logged

Pages: [1]   Go Up
Print
 
Jump to:  

Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC | Theme Kani By Fussilet

Page created in 0.04 seconds with 21 queries.