Help please!

Hi, is there an easier way to do this please?
I have a table of all the species discovered by each player.
I need to display the species name if it been discovered or undiscovered if not.
At present i am doing 1 query to display each but there may be hundreds of these to check.
Is there a way to do 1 query and loop through the result??

[php]
$query1 = $this->db->fetchRow(‘SELECT COUNT(*) AS count FROM discoveries WHERE username=? AND species=?’, array($username, ‘Species1’));
if ($query1->count > 0)
{ $species1 = ‘Species1’; }
else
{ $species1 = ‘Undiscovered’; }

$query2 = $this->db->fetchRow(‘SELECT COUNT(*) AS count FROM discoveries WHERE username=? AND species=?’, array($username, ‘Species2’));
if ($query2->count > 0)
{ $species2 = ‘Species2’; }
else
{ $species2 = ‘Undiscovered’; }

$query3 = $this->db->fetchRow(‘SELECT COUNT(*) AS count FROM discoveries WHERE username=? AND species=?’, array($username, ‘Species3’));
if ($query3->count > 0)
{ $species3 = ‘Species3’; }
else
{ $species3 = ‘Undiscovered’; }
[/php]

Yes, there is! I haven’t tested it but the below should work:

[php]for($i = 1; $i < 4; $i++) {
$query = $this->db->fetchRow(‘SELECT COUNT(*) AS count FROM discoveries WHERE username=? AND species=?’, array($username, ‘Species’ . $i));
if ($query->count > 0) {
${‘species’ . $i} = ‘Species1’;
} else {
${‘species’ . $i} = ‘Undiscovered’;
}
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service