Jump to content

Archived

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

Mutley

Lots of IF and ELSE statements, help

Recommended Posts

I'm trying to make it find an ID and if the database = that ID it prints words.

[code]<?php
if($team_id == 1) { ?>
1st Team
<?php
}
else {
if($team_id == 2) { ?>
2nd Team
<?php
}
}
else {
if($team_id == 3) { ?>
3rd Team
<?php
}
}
else {
if($team_id == 4) { ?>
4th Team
<?php
}
}
else {
if($team_id == 5) { ?>
Under 7s
<?php
}
}
else {
if($team_id == 6) { ?>
Under 8s
<?php
}
}
else {
if($team_id == 7) { ?>
Under 9s
<?php
}
}
else {
if($team_id == 8) { ?>
Under 10s
<?php
}
}
else {
if($team_id == 9) { ?>
Under 11s
<?php
}
}
else {
if($team_id == 10) { ?>
Under 12s
<?php
}
}
else {
if($team_id == 11) { ?>
Under 13s
<?php
}
}
else {
if($team_id == 12) { ?>
Under 14s
<?php
}
}
else {
if($team_id == 13) { ?>
Under 15s
<?php
}
}
else {
if($team_id == 14) { ?>
u17 Dev
<?php
}
}
else {
if($team_id == 15) { ?>
u19 Dev
<?php
}
}
[/code]


As you can see, it won't work.  :D Simply because of the ELSE after ELSE after ELSE plus it is too long, so was wondering how I could shorten it down.

Share this post


Link to post
Share on other sites
[code]<?php
$teamnames = array('','1st team','2nd team', ........,'ul9Dev');

// get $team_id from somewhere and then
echo $teamnames($team_id);?>[/code]

Share this post


Link to post
Share on other sites
I get:

Fatal error: Call to undefined function: array()

Could you explain how your code works, what it does? I like to learn the techniques, not just do them.  ;)

Share this post


Link to post
Share on other sites
Well, let's start with working code by correcting my error:

[code]<?php
$teamnames = array('','1st team','2nd team', ........,'ul9Dev');

// get $team_id from somewhere and then
$team_id = 2; // example = 2nd team
echo $teamnames[$team_id]; // edited so it works!
?>[/code]

All it does is define a whole list of names, each enclosed with single ' and separated by commas to create an array of teamnames.

Then it echoes out any specific name.

Just edit the $teamnames = array( ....) line so it contains all of your team names.

Share this post


Link to post
Share on other sites
I think AndyBs solution is good, and you should use it, but this so that you can learn more:
[code=php:0]switch($team_id){
case 1: echo '1st Team'; break;
case 2: echo '2nd Team'; break;
case 3: echo '3rd Team'; break;
etc
}[/code]

Right way to use if would be:
[code=php:0]if($team_id == 1){ echo '1st Team'; }
elseif($team_id == 2){ echo '2st Team'; }
elseif($team_id == 3){ echo '3rd Team'; }[/code]
etc

Share this post


Link to post
Share on other sites
Thanks alot that's really good material.

I got it to work by doing:

$team_id = 2;

But I want it to come out of the database, now I use a while(list
while(list($team_id, $dstamp, $home, $away) = mysql_fetch_row($result)) {

So how do I get it out of the database? I tried $team_id = $_GET['team_id']; but no luck? And having no $team_id = etc doesn't do anything either.

Share this post


Link to post
Share on other sites
If you're retrieving $team_id from each database row, just make sure the $teamnames array is declared [i]before[/i] database retrieval, and then echo $teamnames[$team_id] should output the team name for each row retrieved.

Share this post


Link to post
Share on other sites
I can't get it to work, I've just tried doing that, it appears blank, here is the whole code:

[code]<?php


require_once("connection.php");
$teamnames = array('1st Team','2nd Team','3rd Team','4th Team','u7s Team','u8s team','u9s team','u10s team','u11s team','u12s team','u13s team','u14s team','u15s team','u17s Dev','u19s Dev');

$query = "SELECT team_id, DATE_FORMAT(date,'%d-%m-%Y') AS dstamp, home, away FROM scores ORDER BY date LIMIT 1";
$result = mysql_query($query);

if(mysql_num_rows($result)!=0) {

while(list($team_id, $dstamp, $home, $away) = mysql_fetch_row($result)) {
$team_id = $_GET['team_id'];
      ?>
Next match on:
<?=$dstamp?>
For:
<?php
// get $team_id from somewhere and then
echo $teamnames[$team_id];?>


Against:
<?php
if($home == York) {
echo $away;
}
else {
echo $home;
}
}
}
?>[/code]

Thanks.

Share this post


Link to post
Share on other sites
[code] while(list($team_id, $dstamp, $home, $away) = mysql_fetch_row($result)) {
echo $teamnames[$team_id]; // should work
      ?>[/code]

The $_GET syntax was only an example of how you [i]might[/i] have been getting the team_id, i.e. passed through a link to display a specific record.

Share this post


Link to post
Share on other sites

×

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.