Copy link to clipboard
Copied
I have the following code that creates the category and associate link for each category. I like to know how can I have the category name different than the keywords for each category. For example I like to search for ‘superman’ but like to call the link ‘superman movie’
Any help will be greatly appreciated
thanks
<? $categories
= array(
'Superman'
,
'Batman'
,
'Iron Man'
,
'Incredible Hulk'
);
?>
<
h2
>
Categories
:</
h2
>
<? foreach (
$categories
as
$category
) {
?>
<a href="/category.php?psps_search=
<?= urlencode
(
$category
)
?>
">
<?= $category ?>
</a><br/>
1 Correct answer
Assuming it's not as simple as adding the string " Movie" after each search term, you could make a nested associative array that stores both bits of information under the appropriate key (without getting a database or XML file involved). Something like:
<?php
$categories = array(
array('search'=>'Superman', 'link'=>'Superman Movie'),
array('search'=>'Batman', 'link'=>'Batman Movie'),
array('search'=>'Iron Man', 'link'=>'Iron Man Movie'),
array('search'=>'Incredible Hulk', 'link'=>'Incredible Hulk Movi
...Copy link to clipboard
Copied
Assuming it's not as simple as adding the string " Movie" after each search term, you could make a nested associative array that stores both bits of information under the appropriate key (without getting a database or XML file involved). Something like:
<?php
$categories = array(
array('search'=>'Superman', 'link'=>'Superman Movie'),
array('search'=>'Batman', 'link'=>'Batman Movie'),
array('search'=>'Iron Man', 'link'=>'Iron Man Movie'),
array('search'=>'Incredible Hulk', 'link'=>'Incredible Hulk Movie')
);
?>
<?php foreach ($categories as $category) { ?>
<a href="/category.php?psps_search=<?= urlencode($category['search']) ?>">
<?= $category['link'] ?>
</a><br/>
<?php } ?>
Copy link to clipboard
Copied
Dear Mr Baldwin
Thank you for this very precise info. I have created two groups follwing your suggestion: one for category and one for brandI just wanted to know if i like to have a heading for the appropriate link( ie if it is a brand the heading would be the given brand or if it is category than the heading will be given category). i tried the following . and i am sure there is something i am missing :
<?php if ($psps_search=urlencode($category['search'])) echo $category['link']; elseif ($psps_search=urlencode($brand['search'])) echo $brand['link'];?>
psps_search is the search parameter
thanks

Copy link to clipboard
Copied
<?php
$categories = array(
array('search'=>'Superman', 'link'=>'Superman Movie'),
array('search'=>'Batman', 'link'=>'Batman Movie'),
array('search'=>'Iron Man', 'link'=>'Iron Man Movie'),
array('search'=>'Incredible Hulk', 'link'=>'Incredible Hulk Movie')
);
foreach($categories as $category){
$psps_search = urlencode($category['search']);
echo $category['link'];
}
Didn't you forget the foreach statement?
Copy link to clipboard
Copied
Assuming it is inside the foreach statement, looks like you might have the wrong operator in there - the single = (assignment operator) should be a double equal sign (comparison operator). So, if I'm reading that right, change this:
<?php if ($psps_search=urlencode($category['search'])) echo $category['link']; elseif ($psps_search=urlencode($brand['search'])) echo $brand['link'];?>
to this:
<?php if ($psps_search==urlencode($category['search'])) echo $category['link']; elseif ($psps_search==urlencode($brand['search'])) echo $brand['link'];?>
Copy link to clipboard
Copied
Thank you so much for your response.
<?php if ($psps_search=urlencode($category['search'])) echo $category['link']; elseif ($psps_search=urlencode($brand['search'])) echo $brand['link'];?>
With this I was getting the last category(no matter what i entered in psps_search) . I actually did not have
foreach statement. I have added foreach as follow and now get all the
categories in the page(instead of one).I did the code as cody rob suggested :
foreach($categories as $category){
$psps_search = urlencode($category['search']);
echo $category['link'];
}
Also i can just add if statement before each foreach statement so that category and brand will get called as appropriate.
thanks for your help

