Jump to content


Photo

Lots of IF and ELSE statements, help


  • Please log in to reply
8 replies to this topic

#1 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 September 2006 - 10:47 AM

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

<?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
}
}


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.
~ Mutley.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 10:53 AM

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

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

Legend has it that reading the manual never killed anyone.
My site

#3 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 September 2006 - 11:12 AM

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.  ;)
~ Mutley.

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 11:21 AM

Well, let's start with working code by correcting my error:

<?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!
?>

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.
Legend has it that reading the manual never killed anyone.
My site

#5 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 01 September 2006 - 11:41 AM

I think AndyBs solution is good, and you should use it, but this so that you can learn more:
switch($team_id){
case 1: echo '1st Team'; break;
case 2: echo '2nd Team'; break;
case 3: echo '3rd Team'; break;
etc
}

Right way to use if would be:
if($team_id == 1){ echo '1st Team'; }
elseif($team_id == 2){ echo '2st Team'; }
elseif($team_id == 3){ echo '3rd Team'; }
etc
I'm just a guy who likes to code for fun...

#6 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 September 2006 - 12:08 PM

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.
~ Mutley.

#7 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 12:32 PM

If you're retrieving $team_id from each database row, just make sure the $teamnames array is declared before database retrieval, and then echo $teamnames[$team_id] should output the team name for each row retrieved.
Legend has it that reading the manual never killed anyone.
My site

#8 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 September 2006 - 01:59 PM

I can't get it to work, I've just tried doing that, it appears blank, here is the whole 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;
}
}
}
?>

Thanks.
~ Mutley.

#9 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 03:02 PM

while(list($team_id, $dstamp, $home, $away) = mysql_fetch_row($result)) {
	echo $teamnames[$team_id]; // should work
	      ?>

The $_GET syntax was only an example of how you might have been getting the team_id, i.e. passed through a link to display a specific record.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users