Jump to content

switch bad-coding I guess


soltek

Recommended Posts

$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 =)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)  :shrug:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.