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.

Edited by PHPBear
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.