Mutley Posted September 1, 2006 Share Posted September 1, 2006 I'm trying to make it find an ID and if the database = that ID it prints words.[code]<?phpif($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. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 1, 2006 Share Posted September 1, 2006 [code]<?php$teamnames = array('','1st team','2nd team', ........,'ul9Dev');// get $team_id from somewhere and thenecho $teamnames($team_id);?>[/code] Quote Link to comment Share on other sites More sharing options...
Mutley Posted September 1, 2006 Author Share Posted September 1, 2006 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. ;) Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 1, 2006 Share Posted September 1, 2006 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 teamecho $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. Quote Link to comment Share on other sites More sharing options...
Ifa Posted September 1, 2006 Share Posted September 1, 2006 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 Quote Link to comment Share on other sites More sharing options...
Mutley Posted September 1, 2006 Author Share Posted September 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 1, 2006 Share Posted September 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
Mutley Posted September 1, 2006 Author Share Posted September 1, 2006 I can't get it to work, I've just tried doing that, it appears blank, here is the whole code:[code]<?phprequire_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 thenecho $teamnames[$team_id];?>Against:<?phpif($home == York) {echo $away;}else {echo $home;}}}?>[/code]Thanks. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 1, 2006 Share Posted September 1, 2006 [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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.