Jump to content

Beginner Array Question (Grouping Values)


PHPBear

Recommended Posts

I've just started learning about arrays and am still taking baby steps. I'd like to ask how to place values from a database table in various groups.

 

From the beginning, I have a database-driven website with pages for thousands of animal species (table gz_life). I have a second table that lists species that are somehow unique, including species that are extinct. In fact, there are three categories of extinction, as illustrated below...

 

Field: TaxonG | Wild_Card

Dinosaurs | Prehistoric

Trilobites | Prehistoric

Mammoth | Ice_Age

Mastodon | Ice_Age

Passenger_Pigeon | Extinct

Dodo | Extinct

 

I would like to somehow classify the above animals into groups, so I can more easily manipulate information relating to them. For example, imagine if I wanted to link pages of living animals to files in one directory and pages representing extinct creatures to a different directory. I might create a switch similar to this:

 

switch($Group)
{
case 'Extinct':
case 'Ice_Age':
case 'Prehistoric':
(Link to Folders/Extinct/MyPage.php
break;
default:
(Link to Folders/Living/MyPage.php
break;
)

 

So how can I put all these URL's into groups or arrays so I can manage them? Below is the basic script I'm working with...

 


<?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");
}

while ($row = mysql_fetch_array($Children, MYSQL_ASSOC))
{
$TaxonG = $row['TaxonG'];
$Wild_Card = $row['Wild_Card'];
?>

 

If I add WHERE Wild_Card = 'Ice_Age' to the query, then I can create the following array:

 

$Icy = array($TaxonG);

 

It seems to work, but I can't do much with it, because I don't know how to manipulate it or associate it with URL's.

 

I also discovered the foreach function. If I replace $v with $MyURL, it works - but only on the page MySite/Life/Mammoth. I don't know how to replace $v with an array value that will work for every page matching the array's contents.

 


foreach ($Icy as $k => $v) {
if ($v == "Mammoth") {
echo "Hello World";
}
}

 

I've also played with the in_array function, with a little success, but it still isn't working correctly. I've tried combining foreach and in_array and have tried my experiments both inside and outside the loop.

 

* * * * *

 

Anyway, getting back to the first query I posted (without a where clause), how can I put these values into separate groups and manipulate them?

 

I thought I could create a switch similar to the one below, but I don't know exactly how to construct it.

 

switch($Wild_Card)

 

{

case 'Parataxon':

$Group = 'Parataxon';

// OR...

$Parataxon = 'array($Wild_Card)';

break;

case 'EZ':

$Group = 'EZ':

// OR...

$EZ = 'array($Wild_Card)';

break;

case 'Extinct':

$Extinct = 'array($Wild_Card)';

break;

case 'Ice_Age':

$Ice_Age = 'array($Wild_Card)';

break;

case 'Prehistoric':

$Prehistoric = 'array($Wild_Card)';

break;

case 'Ghost':

$Ghost = 'array($Wild_Card)';

break;

default:

$Joker = '';

break;

}

[/code]

 

Anyway, I think that pretty well explains what I'm trying to do. Any tips?

 

Thanks!

 

P.S. I've looked at the PHP Manual - http://php.net/manua...res.foreach.php - but it doesn't make a lot of sense to me.

Link to comment
https://forums.phpfreaks.com/topic/271117-beginner-array-question-grouping-values/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.