Daney11 Posted November 6, 2007 Share Posted November 6, 2007 Hey guys, I am stuck on how to write this function. Basically i have a table 'ladder' and 'teams' In 'ladder' i have ladderid ladderteamid laddernumber in 'teams' i have teamid teamname I want a function to grab the name of the team 'teamname' from 'teams' however im selecting the team name like this.. $teamnameQuery = "SELECT * FROM `teams` WHERE `teamid` = $ladderteamid"; All the information on ladder.php is in ladder but the teamname is in a seperate table. Any other information you need ill post. Thanks in advanced Dane Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/ Share on other sites More sharing options...
MadTechie Posted November 6, 2007 Share Posted November 6, 2007 may want to look into JOIN http://dev.mysql.com/doc/refman/5.0/en/join.html Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-385797 Share on other sites More sharing options...
Daney11 Posted November 6, 2007 Author Share Posted November 6, 2007 so something like function getteamname($team_tid) { $namequery=mysql_query("select * from teams LEFT JOIN ladder where `teams.team_tid` = 'ladder.ladder_lteamid'"); $nameinfo=mysql_fetch_array($namequery); $nameinfo['ladder_lteamid']=stripslashes(trim(htmlspecialchars($nameinfo['team_tname']))); $nameinfo['team_tname']=stripslashes(trim(htmlspecialchars($nameinfo['team_tname']))); return $nameinfo['team_tname']; } but that isnt working ^^ never used that join command lol Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-385806 Share on other sites More sharing options...
MadTechie Posted November 6, 2007 Share Posted November 6, 2007 something like this SELECT teams.* FROM teams LEFT JOIN ladder ON teams.team_tid = ladder.ladder_lteamid; Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-385813 Share on other sites More sharing options...
Daney11 Posted November 6, 2007 Author Share Posted November 6, 2007 Yeah i got to that aswell and it is only echoing out the teamid 1. i have 6 teams in. 1 2 3 4 5 6 and its only pulling out teamid 1. so im close. Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-385816 Share on other sites More sharing options...
MadTechie Posted November 6, 2007 Share Posted November 6, 2007 do you have entries on the ladder for the other teams ? Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-385822 Share on other sites More sharing options...
Daney11 Posted November 6, 2007 Author Share Posted November 6, 2007 yes. all teams are in the ladder. with points etc. it is echoing out 6 entries but all 6 are named team1 Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-385825 Share on other sites More sharing options...
MadTechie Posted November 6, 2007 Share Posted November 6, 2007 the key is teams.team_tid = ladder.ladder_lteamid; they must match IE teams - team_tid teamA - 1 teamB - 2 teamC - 3 teamD - 4 ladder - ladder_lteamid Atest1 - 1 Atest2 - 1 Atest3 - 1 Atest4 - 1 Btest1 - 2 Btest2 - 2 Btest3 - 2 Btest4 - 3 will should return teamA - Atest1 teamA - Atest2 teamA - Atest3 teamA - Atest4 teamB - Atest1 teamB - Atest2 teamB - Atest3 teamB - Atest4 Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-385834 Share on other sites More sharing options...
Daney11 Posted November 6, 2007 Author Share Posted November 6, 2007 it works if i dont add in it a function. but i want most bits in functions. check the pm i sent u dude, thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-385851 Share on other sites More sharing options...
Daney11 Posted November 6, 2007 Author Share Posted November 6, 2007 any other suggestions anyone can think of? thanks dane Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-385951 Share on other sites More sharing options...
MadTechie Posted November 6, 2007 Share Posted November 6, 2007 try this <?php // Start String Function function escape_ladder_data($data) { global $connect; if (ini_get('magic_quotes_gpc')) { #$date = stripslashes($data); //TYPEO $data = stripslashes($data); //TYPEO FIXED } return mysql_real_escape_string(trim($data), $connect); } // End String Function // Start Get Team Name Function function getteamname($team_tid) { $namequery=mysql_query("SELECT teams.* FROM teams LEFT JOIN ladder ON teams.team_tid = ladder.ladder_lteamid WHERE teams.team_tid=$team_tid;"); //UPDATED $nameinfo=mysql_fetch_array($namequery); $nameinfo['ladder_lteamid']=stripslashes(trim(htmlspecialchars($nameinfo['ladder_lteamid']))); $nameinfo['team_tname']=stripslashes(trim(htmlspecialchars($nameinfo['team_tname']))); return $nameinfo['team_tname']; } // End Get Team Name Function ?> Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-386068 Share on other sites More sharing options...
Daney11 Posted November 7, 2007 Author Share Posted November 7, 2007 Im getting a warning Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\ladder_functions.php on line 22 Line 22 being $nameinfo=mysql_fetch_array($namequery); :S Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-386238 Share on other sites More sharing options...
MadTechie Posted November 7, 2007 Share Posted November 7, 2007 change $namequery=mysql_query("SELECT teams.* FROM teams LEFT JOIN ladder ON teams.team_tid = ladder.ladder_lteamid WHERE teams.team_tid=$team_tid;"); //UPDATED to $namequery=mysql_query("SELECT teams.* FROM teams LEFT JOIN ladder ON teams.team_tid = ladder.ladder_lteamid WHERE teams.team_tid=$team_tid;") or die(mysql_error()); //UPDATED Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-386247 Share on other sites More sharing options...
Daukan Posted November 7, 2007 Share Posted November 7, 2007 Try this <?php $namequery=mysql_query("SELECT teams.* FROM teams LEFT JOIN ladder ON teams.team_tid = ladder.ladder_lteamid WHERE teams.team_tid='$team_tid'"); ?> I took out the semi colon and put single quotes around $team_tid Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-386249 Share on other sites More sharing options...
Daney11 Posted November 7, 2007 Author Share Posted November 7, 2007 Parse error: parse error, unexpected T_LOGICAL_OR in D:\ladder_functions.php on line 20 with line 20 being $namequery=mysql_query("SELECT teams.* FROM teams LEFT JOIN ladder ON teams.team_tid = ladder.ladder_lteamid WHERE teams.team_tid=$team_tid"); or die(mysql_error(); thanks for how much you're helping me btw. Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-386252 Share on other sites More sharing options...
MadTechie Posted November 7, 2007 Share Posted November 7, 2007 20 should be $namequery=mysql_query("SELECT teams.* FROM teams LEFT JOIN ladder ON teams.team_tid = ladder.ladder_lteamid WHERE teams.team_tid=$team_tid") or die(mysql_error()); note the extra ) and a ; removed Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-386257 Share on other sites More sharing options...
Daney11 Posted November 7, 2007 Author Share Posted November 7, 2007 I should have really found that mistake. Its late. 12:43am here lol. Thanks a lot MadTechie for the help, it works Quote Link to comment https://forums.phpfreaks.com/topic/76229-solved-stuck-on-a-function/#findComment-386260 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.