-2

My first attempt with PHP, and I have a shopping cart.

I want each item to have a link back to its own product page. So I'm using this

$output[] = '<a href="'.$brand.'/index.php#'.$id.'" target="_blank">';

Unfortunately the $brand doesn't have the same folder name. I need help to write some code that replaces 'this is the first brand' to 'brand-one' for example. Also I have just a few brands, so I can use a line for each operation, no need to think of something that replaces ' ' to '-' for example!

I think this could be something but I don't want the script to rename the values for everything, as they still need to be outputted like 'this is the first brand', with the processed value outputted only on the link.

4
  • 1
    it would be better if you give as a little more of code. It's not clear where $barnd and $id variables are assigned with values Commented Nov 3, 2012 at 19:30
  • It need to replace this is the first brand with brand-one, but that's rather specific. How about the second brand? Commented Nov 3, 2012 at 19:30
  • It isn't clear what you are trying to do. Are you trying to generate slugs based on a brand and product name? Commented Nov 3, 2012 at 19:33
  • well table elements in the 'brand' cloumn are named (for example): - Native Instruments Maschine - Rob Papen Albino 2 - Ableton Live 8 Suite and I'd want to rename the to something like: - Native-Instruments - Rob-Papen - Ableton this is just an example, I can't find a code that creates a new value copying and renaming from an existing value. I hope I've been more clear Commented Nov 4, 2012 at 15:03

1 Answer 1

1

According to what you say you require (that you can do a line by line replacement), you can use the same function in the link ie str_replace.

$array1 = {'this is the first brand','this is the second brand'};
$array2 = {'brand-one','brand-two'};
$brand2 = str_replace($array1,$array2,$brand);

This would work for a small number of elements, but for larger numbers it would be easier if you have a pattern of replacement as jasper requested.

Sign up to request clarification or add additional context in comments.

3 Comments

Also please dont use tags like shopping-cart
i would need something like this, but still i can't make it work.
say the $brand outputs 'Wrath of the Titans', i need to create another value for $brand2 which outputs 'Wrath-Titans', or 'Feel like flying high' to 'Flying-High'. as above there's a few elements, I can write a line for each one of them! thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.