Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

creating assoiated array in php

New Here ,
Oct 12, 2009 Oct 12, 2009

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 supermanbut 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/>

TOPICS
Server side applications
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Oct 13, 2009 Oct 13, 2009

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

...
Translate
Enthusiast ,
Oct 13, 2009 Oct 13, 2009

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 } ?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 18, 2009 Oct 18, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 18, 2009 Oct 18, 2009

<?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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 18, 2009 Oct 18, 2009

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'];?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 19, 2009 Oct 19, 2009
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines