0

I've been wrestling with a really cool script someone gave me, trying to adapt it to my site. I'm getting closer, but I'm still getting two errors that have me puzzled.

First: Warning: Invalid argument supplied for foreach()...

This is the foreach statement:

foreach ($Topic as $Topics)

It follows a function:

function generate_menu_items($PopTax, $Topics, $Current_Topic)

I THINK the problem relates to the middle value in the function - $Topics. I don't understand how it's derived. My guess is it's supposed to be an array of all the possible topics (represented by $MyTopic in my database). But I'm not that familiar with functions, and I don't understand why he put the function and foreach BEFORE the database queries. (However, there is a more general DB query that establishes some of these values higher up the food chain.)

Here's the second problem: Fatal error: Call to undefined function render_menu()...

Can anyone tell me how and where I should define this function?


Let me briefly explain what this script is all about. First imagine these URL's:

MySite/topics/animal MySite/topics/animal-homes MySite/topics/animal-ecology MySite/topics/mammal-ecology MySite/topics/bird-ecology

Two key values are associated with each URL - $PopTax (popular name) and $MyTopic. For the first three URL's, $PopTax = Animal, while the other two are Mammal and Bird. $MyTopic = Ecology for the last three rows. For the first two, $MyTopics = Introduction and Homes.

The ID's for both values (Tax_ID and Topic_ID) are simply the first three letters of the name (e.g. Mam = Mammal, Eco = Ecology). Also, Life is the Parent of Animal, which is the Parent of Vertebrate, which is the Parent of Mammal.

Now I'm just trying to pull it all together to create a little index in the sidebar. So if you visit MySite/topics/animal-ecology, you'd see a list of ALL the animal topics in the sidebar...

<a href="/topic/animal" title="Animals">Animals</a>
<a href="/topic/animal-classification" title="Animal Classification">Animal Classification</a>
<a href="/topic/animal-homes" title="Animal Homes">Animal Homes</a>
<a href="/topic/animal-ecology" title="Animal Ecology">Animal Ecology</a>

As you can see there are some case and plural differences (animal vs Animals), though I don't think that really relates to the problems I'm having right no.

But I'm not sure if my code just needs to be tweaked or if there's something grotesquely wrong with it. Something doesn't look right to me. Thanks for any tips.


$Tax_ID = 'Mam'; // Mam represents Mammal
$Current_Topic = 'Homes';

function generate_menu_items($PopTax, $Topics, $Current_Topic)
{ 
    $menu_items = array(); 

    foreach ($Topic as $Topics) 
    { 
     $url   = "/topics/$PopTax[PopTax]-$Topic[MyTopic]"; 
     $title = "$PopTax[PopTax] $Topic[MyTopic]"; 
     $text  = $Topic['MyTopic']; 

    if ($Topic === 'People') { 
        $url   = "$PopTax[PopTax]-and-$Topic[MyTopic]"; 
        $title = "$PopTax[PopTax] and $Topic[MyTopic]"; 
        $text  = "$PopTax[PopTax] &amp; $Topic[MyTopic]"; 
    } 

    if ($Topic === 'Movement' && $PopTax['Parent'] == 'Ver' && $PopTax['PopTax'] != 'Human') { 
        $url   = "$PopTax[PopTax]-locomotion"; 
        $title = "$PopTax[PopTax] Locomotion"; 
        $text  = "Locomotion"; 
    } 

    $menu_items[] = array( 
        'url'    => strtolower($url), 
        'title'  => ucwords($title), 
        'text'   => ucfirst($text), 
        'active' => ($Topic['MyTopic'] === $Current_Topic) 
    ); 
  } 

    return $menu_items; 
}

function generate_menu_html($menu_items)
{
 $list_items = array();

 foreach ($menu_items as $item)
 {
    if ($item['active']) {
        $list_items[] = "<li><span class=\"active\">$item[text]</b></span></li>";
    } else {
        $list_items[] = "<li><a href=\"$item[url]\" title=\"$item[title]\">$item[text]</a></li>";
    }

 }

 return '<ol>' . implode("\n", $list_items) . '</ol>';
}

$stm = $pdo->prepare("SELECT T.Topic_ID, T.MyTopic
FROM gz_topics T
JOIN gz_topics_poptax TP ON TP.Topic_ID = T.Topic_ID
WHERE TP.Tax_ID = :Tax_ID");

$stm->execute(array('Tax_ID' => $Tax_ID));
// Fetch all rows (topics) as an associative array
$Topics = $stm->fetchAll(PDO::FETCH_ASSOC);

// Get the DB row for the taxon we're dealing with
$stm = $pdo->prepare("SELECT Tax.ID, Tax.PopTax, Tax.Parent
FROM gz_poptax Tax
WHERE Tax.ID = :Tax_ID");

$stm->execute(array('Tax_ID' => $Tax_ID));
// Fetch a single row, as the query should only return one row anyway
$PopTax = $stm->fetch(PDO::FETCH_ASSOC);

// Call our custom functions to generate the menu items, and render them as a HTML list
$menu_items = generate_menu_items($PopTax, $Topics, $Current_Topic);
$menu_html = render_menu($menu_items);

// Output the list to screen
echo $menu_html;
1
  • You sure render_menu isn't meant to be generate_menu_html? Commented Apr 3, 2014 at 22:54

1 Answer 1

1

You want foreach ($Topics as $Topic). You are looping over each $Topic in $Topics is another way to think of it.

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

1 Comment

Alright, that fixed the first error; thanks. I'll wait until I get the second problem fixed before I mark your answer as correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.