I’m just learning to work with arrays, and someone introduced me to the foreach function on another thread. My experiments are kind of working, but something isn’t clicking. I’ve looked at the PHP Manual at http://php.net/manual/en/control-structures.foreach.php but I don’t really get it.
From the beginning, I have a database table (gz_life) that lists the names of thousands of animal species. A second table (gz_ghosts) lists animals that are somehow different from the others, including species that are extinct. The field TaxonG lists animal names and matches the URL’s of some pages. For example, the mastodon page at MySite/Life/Mammutidae matches the TaxonG value “Mammutidae.”
The field Wild_Card groups pages according to what makes them unique - e.g. Extinct (recently), Ice Age (extinct over 10,000 years ago), etc.
So here’s my query:
<?php
$result = mysql_query('select count(*) from gz_ghosts');
if (($result) && (mysql_result ($result , 0) > 0)) {
} else {
die('Invalid query: ' . mysql_error());
}
{
$Children = mysql_query ("SELECT N, TaxonG, Wild_Card
FROM gz_ghosts
WHERE Wild_Card = 'Ice_Age'");
}
// Note that I'm selecting only rows that represent Ice Age creatures in the above query.
while ($row = mysql_fetch_array($Children, MYSQL_ASSOC))
{
$TaxonG = $row['TaxonG'];
$Wild_Card = $row['Wild_Card'];
// Next, I put the names of all the animals that lived during the Ice Age in an array named $Icy...
$Icy = array($TaxonG);
?>
Now I’m trying to figure out how to manipulate the data in that array. For example, suppose I want to display something only on pages representing extinct animals, like these…
MySite/Life/Mammoth
MySite/Life/Sabertooth_Cat
I do NOT want to display it on pages representing living animals…
MySite/Life/Moose
Assuming I’ve correctly captured the values “Mammoth” and “Sabertooth_Cat” in my array, how can I do that?
I discovered the in_array function, which kind of helps, but I can only use it in very limited ways. I tried a basic foreach function someone gave me:
foreach ($Icy as $k => $v) {
if ($v == "Mammutidae") {
echo "Mammoth";
} // Or any other control structure.
}
If I replace $v with $MyURL, then it will work on the page with that specific URL…
foreach ($Icy as $k => $v) {
if ($MyURL == "Mammutidae") {
echo "Mammoth";
} // Or any other control structure.
}
But I’d like to replace “Mammutidae” with a value representing EVERYTHING in the array, so it will work with Mammutidae or with Mastodon, Sabertooth-Cat, etc.
Here’s one of my experiments with the in_array function:
if (in_array($TaxonG, $Icy)) {
echo "Hello World";
}
However, it displays values for EVERY row representing the Ice Age…
If I replace $TaxonG with $MyURL, then it displays nothing, because I don’t know how to associate the page’s URL with the contents of the array.
I’ve also tried experiments like this:
switch($MyURL)
{
// case array($Icy):
case (in_array($TaxonG, $Icy)):
echo 'Whatever';
break;
default:
echo 'DEFAULT';
break;
}
}
Anyway, can someone tell me how to manipulate the array $Icy so I can associate it with URL’s matching its contents? Also, is there a way to display all the values in that array, so I can verify that it’s working the way I want it to?
Thanks.
P.S. I should mention that I’ve tried my experiments both inside and outside the while loop.