soltek Posted July 18, 2010 Share Posted July 18, 2010 $z = mysql_query("SELECT id, name FROM `teams` WHERE id = $row[team] "); ///////////THIS PART GRABS DATA FROM TEAMS TABLE///////////////// $z = @mysql_fetch_row($z); switch ($z[0]) { case 5: // $team = "<font color=#2274ca><b>$row[id]</b></font>"; break; case 4: // $team = "<font color=yellow></font>"; break; case 3: // $team = "<font color=#f63de7></font>"; break; case 2: // $team = "<font color=purple></font>"; break; case 1: // $team = "<font color=gray></font>"; break; default: // $team = "<font color=white></font>"; } if ($row["team"] != "") /////////////This part grabs the data from USER table///////////////// print ("$team"); Hey there, guys, with this code im trying to print the ID of a row at TEAMS table, but Im getting the ID of the USER table. Can you give the noob a hand, please? Thank you =) Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/ Share on other sites More sharing options...
wildteen88 Posted July 18, 2010 Share Posted July 18, 2010 How do you know its grabbing the id from the users table? Your query is grabbing the id and name from the teams table mysql_query("SELECT id, name FROM `teams` WHERE id = $row[team] "); MySQL will only grab the data from the table you tell it to. Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087803 Share on other sites More sharing options...
TapeGun007 Posted July 18, 2010 Share Posted July 18, 2010 I was going to try this and tell me what you get: $result = mysql_query("SELECT * FROM teams'"); while($row = @mysql_fetch_array($result)) { echo $row['id']. $row['name']."<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087804 Share on other sites More sharing options...
soltek Posted July 18, 2010 Author Share Posted July 18, 2010 How do you know its grabbing the id from the users table? Your query is grabbing the id and name from the teams table mysql_query("SELECT id, name FROM `teams` WHERE id = $row[team] "); MySQL will only grab the data from the table you tell it to. I thought the same, mate, but the ID that is being 'print' at the browser is user ID, not the team ID. A couple of lines above I have something similar, but it works. Maybe is interfering? $a = mysql_query("SELECT class, username FROM `users` WHERE id = $row[owner] "); $a = @mysql_fetch_row($a); switch ($a[0]) { case 5: // $user = "<font color=#2274ca>$row[username]</font>"; break; (...) } I was going to try this and tell me what you get: $result = mysql_query("SELECT * FROM teams'"); while($row = @mysql_fetch_array($result)) { echo $row['id']. $row['name']."<br>"; } Where exactly do I type that? Im still learning, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087808 Share on other sites More sharing options...
wildteen88 Posted July 18, 2010 Share Posted July 18, 2010 I thought the same, mate, but the ID that is being 'print' at the browser is user ID, not the team ID. Again how do you know this? Can we see some sample data from both your users and teams table and also your table structure. Can you explain what you're trying to do with the switch/case statement? I am not understanding your code either. Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087815 Share on other sites More sharing options...
soltek Posted July 18, 2010 Author Share Posted July 18, 2010 I thought the same, mate, but the ID that is being 'print' at the browser is user ID, not the team ID. Again how do you know this? Can we see some sample data from both your users and teams table and also your table structure. Can you explain what you're trying to do with the switch/case statement? I am not understanding your code either. The user ID is 205, the team ID is 5 and it's printing 205, the user ID. Now, I have a html table where each row have several columns, one of them is called «responsible». At that column, it's printing the username of the responsible, but I wanted to print the team he belongs to, using the team ID. Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087822 Share on other sites More sharing options...
jcbones Posted July 18, 2010 Share Posted July 18, 2010 From looking at your code, the Team id would be. Either: $row['team']; OR, $z[0]; Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087867 Share on other sites More sharing options...
.josh Posted July 18, 2010 Share Posted July 18, 2010 in your OP your query for the teams table is being put in $z but you are echoing out $row Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087872 Share on other sites More sharing options...
soltek Posted July 19, 2010 Author Share Posted July 19, 2010 From looking at your code, the Team id would be. Either: $row['team']; OR, $z[0]; And what would make it be $z[0];? Now I found out that something's wrong with the first sting (the Z one) Take a look at this, please: $z = mysql_query("SELECT id, name FROM `teams` WHERE id = $row[equipa] "); $z = @mysql_fetch_row($z); switch ($z[0]) { case 5: // $team = "<font color=#2274ca><b>$row[id]</b></font>"; break; case 4: // $team = "<font color=yellow>$row[id]</font>"; break; case 3: // $team = "<font color=#f63de7>$row[id]</font>"; break; case 2: // $team = "<font color=purple>$row[id]</font>"; break; case 1: // $team = "<font color=gray>$row[id]</font>"; break; default: // $team = "<font color=white>TEST</font>"; } if ($row[equipa] != "") echo "$team"; In the USERS table, equipa=5. Theres a row at the TEAMS table, where the ID=5 Though, at this example, Im getting the «TEST» when it should be $row[id]. Please dont give up on me xD Ps: If I echo «$row[equipa]» instead of «$team», I get the correct output: 5 (grabbed from USERS table) Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087944 Share on other sites More sharing options...
jcbones Posted July 19, 2010 Share Posted July 19, 2010 And what would make it be $z[0];? This line: $z = @mysql_fetch_row($z); Would make the array; $z[0] = 5; //the id. $z[1] = 'Team'; //team name. Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087953 Share on other sites More sharing options...
soltek Posted July 19, 2010 Author Share Posted July 19, 2010 And what would make it be $z[0];? This line: $z = @mysql_fetch_row($z); Would make the array; $z[0] = 5; //the id. $z[1] = 'Team'; //team name. What can I do to set this right, jcbones? Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087958 Share on other sites More sharing options...
jcbones Posted July 19, 2010 Share Posted July 19, 2010 I would be more than happy to tell you. But, you have confused me so much on what you want, I don't know what "right" is. Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087965 Share on other sites More sharing options...
soltek Posted July 19, 2010 Author Share Posted July 19, 2010 I would be more than happy to tell you. But, you have confused me so much on what you want, I don't know what "right" is. I'll try to make it simple. Lets consider only two tables, USERS and TEAMS(the fields are ID, OWNER, NAME, LOGO). Now i code a html table, like this: mysql_query("SELECT name, id, teamnumber, time FROM users where ID=$ID"); <table> <tr> <td>$row[id]</td> <td>$row[time]</td> if ( $row[teamnumber] == '') ////With this last lines I wanted to print the name of the user, unless he belongs to a team <td>$row[name]</td> else <td>THE.NAME.OF.THE.CORRESPONDING.TEAM's.ID</td> </tr> <table> And this is why I was trying the switch thingy. It's the only way I know how to do it. Or thought I knew lol. Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1087971 Share on other sites More sharing options...
wildteen88 Posted July 19, 2010 Share Posted July 19, 2010 Rather than trying to merge the results of two queries together. You'll want to look into SQL Joins, which allows you to query more than one table a time. With SQL Joins your query may look like this SELECT u.id, u.name, u.teamnumber, u.time, t.name AS teamname, t.owner AS teamowner, t.logo AS teamlogo, t.id AS teamid FROM users u LEFT JOIN teams t ON u.teamnumber = t.id WHERE u.id = $ID Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1088226 Share on other sites More sharing options...
soltek Posted July 19, 2010 Author Share Posted July 19, 2010 Ok, now I have this: [/code] SELECT post.equipa, post.id, post.name, post.owner, categories.name AS cat_name, users.username, teams.id AS teams_id, teams.name AS teams_name, IF(post.numratings < 2, NULL, ROUND(post.ratingsum / post.numratings, 1)) AS rating FROM torrents LEFT JOIN teams ON post.equipa = teams.id LEFT JOIN categories ON category = categories.id LEFT JOIN users ON post.owner = users.id WHERE visible = 'yes' AND banned = 'no' ORDER BY post.id DESC LIMIT 45"; [/code] But now: echo "$row[teams_id]"; Gives me no data at all, just a blank space. Any suggestion? Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1088271 Share on other sites More sharing options...
wildteen88 Posted July 19, 2010 Share Posted July 19, 2010 Have you checked that your query has returned any results? Also it will be helpful if you could post the code you're using now. Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1088284 Share on other sites More sharing options...
soltek Posted July 19, 2010 Author Share Posted July 19, 2010 Oh I got now! Your last suggestion, wildteen88, was perfect. The error was mine because at the database, that entry got the team number 1, at the post table. Though, at the TEAMS table, there wasnt any entry with the ID 1. Now it's working perfectly. Thank you for letting me learn something new, i appreciate it, mates. Have a nice week. Quote Link to comment https://forums.phpfreaks.com/topic/208098-switch-bad-coding-i-guess/#findComment-1088295 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.