Jump to content

Archived

This topic is now archived and is closed to further replies.

esiason14

php if/else, mysql, etc ?

Recommended Posts

I have a list of basball players. For each player listed, the user has the option to select that player so that they can track them in their profile (I got this part working).
The player id in the baseball player table is player_id. In the users table it is myplayer_id.
Next to each player there is a submit button that does the insert into the db if selected. What I want to do is...if that player already exists in the users profile..then show a different image...not the submit button for that player. Make sense?
Basically (I think) I have to build a query that compares the myplayer_id (in the users table) to the player_id (in the players table. Make sense? IF they match, and the user is logged in ($logged_in == 1) then show this :

[code] echo "\t\t<td$row_color align=\"center\"><img src=\"images/ismyp.gif\" border=\"0\"></td>\n"; [/code]

if there is no match, then show this:

[code]       echo "\t\t<td$row_color align=\"center\"><input type=\"image\" src=\"images/addmyp.gif\" value=\"$player_id\"
name=\"myplayers\"></td>\n";[/code]

Can someone please help me figure this out. I've been trying for a couple of hours now and I getting pretty pissed :)

Share this post


Link to post
Share on other sites
Do you mean like this?

[code]
if ($logged_in == '1')
{
  if ($myplayer_id == $player_id)
    {
     echo "do this stuff";
    }
  
  else{
         echo "do this instead";
        }
  }

else{
       echo "you need to log in";
       }
[/code]

Share this post


Link to post
Share on other sites
Thats probably pretty close. I tried it out and it only shows the else....

I tried this and it only showed the if :)

[code]  if  (($logged_in == 1)  && ($myp = $player_id))
{
     echo "\t\t<td$row_color align=\"center\"><img src=\"images/ismyp.gif\" border=\"0\"></td>\n"; }

else {
         echo "\t\t<td$row_color align=\"center\"><input type=\"image\" src=\"images/addmyp.gif\" value=\"$player_id\" name=\"myplayers\"></td>\n";
} [/code]

Maybe if provide more info, it will help.

Heres my query that selects all the player info for the players, playerstats and teams table

[code]$sql = "SELECT ps.*, p.*, t.*
        FROM playerstats ps
        INNER JOIN
        (
            players p INNER JOIN mlbteams t
            ON t.mlbteam_id = p.mlbteam_id
        )
        ON ps.player_id = p.player_id
        WHERE p.position_id = $set_selected_position
        AND ps.year = $set_selected_year
        ORDER BY ko DESC";[/code]

$player_id = $stat_list[$i]['player_id'];

Here is my query selecting the myplayer_id from the myplayers table (this is the one that holds the users players):

[code]$result = mysql_query("SELECT * from myplayers WHERE user_id = '$id'");
while ($row = mysql_query($result))
{
    $myp = $row['myplayer_id'];
    
}[/code]

Share this post


Link to post
Share on other sites
Getting back to basics......

[code]
if (one == one)
{
  if (two == two)
    {
     echo "do this stuff";
    }
  
  else{
         echo "do this instead";
        }
  }

else{
       echo "you need to log in";
       }
[/code]

....returns "do this stuff"

[code]
if (one == one)
{
  if (two == five)
    {
     echo "do this stuff";
    }
  
  else{
         echo "do this instead";
        }
  }

else{
       echo "you need to log in";
       }
[/code]

....returns "do this instead"

[code]
if (one == ten)
{
  if (two == two)
    {
     echo "do this stuff";
    }
  
  else{
         echo "do this instead";
        }
  }

else{
       echo "you need to log in";
       }
[/code]

....returns "you need to log in"

which is all as it should be

Share this post


Link to post
Share on other sites
Indeed you are/were correct. There was a problem with the myplayer_id variable $mpid.

Anyway, I got this working and it returns the "if" (the other image) below, but only for the 1st match. Obviously, I would like to have it for all occurences/matches where $mpid == $player_id.

[code]
<?php

$sessu = mysql_escape_string($_SESSION['username']);

$result = mysql_query("SELECT id FROM users WHERE username = '$sessu'");
if (!$result) {
   die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($result))
{
    $id = $row['id'];
}


$result = mysql_query("SELECT player_id FROM myplayers WHERE user_id = '$id'");
if (!$result) {
   die('Invalid query: ' . mysql_error());
}

while ($row = mysql_fetch_array($result))
{
    $mpid = $row['player_id'];
}
     echo "<tr>";

if ($logged_in == 1)
{
  if ($mpid == $player_id)
    {
    echo "\t\t<td$row_color align=\"center\"><img src=\"images/ismyp.gif\" border=\"0\"></td>\n";
    }
  
  else {
echo "\t\t<td$row_color align=\"center\"><input type=\"image\" src=\"images/addmyp.gif\" value=\"$player_id\"
name=\"myplayers\"></td>\n";

        }
  }[/code]

Share this post


Link to post
Share on other sites
Perhaps I'm not quite understanding correctly, but you can only expect a single match when there can be only 1 $id......that is, the logged in user.

Share this post


Link to post
Share on other sites
[!--quoteo(post=361341:date=Apr 3 2006, 04:45 PM:name=realjumper)--][div class=\'quotetop\']QUOTE(realjumper @ Apr 3 2006, 04:45 PM) [snapback]361341[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Perhaps I'm not quite understanding correctly, but you can only expect a single match when there can be only 1 $id......that is, the logged in user.
[/quote]


For example, if a user ($id, correct there can only be one occurence) has selected 10 players to track, this only returns one (the first occurence of $mpid, where mpid is the myplayer_id) record. So, I'm trying to get it to return all of the myplayer_id for that logged in user.

Share this post


Link to post
Share on other sites
Ok, maybe (hopefully) this will show a clearer picture of what I want to do.

If the user is logged in with no players currently being tracked, then show this: (the first echo here is the submit botton, a Plus sign image)
[code]
        echo "<td$row_color align=\"center\"><input type=\"image\" src=\"images/addmyp.gif\" value=\"$player_id\" name=\"myplayers\"></td>";
    echo "\t\t<td$row_color><a href='/baseball/players.php?player_id=$player_id'>$fname $lname</td>\n";  
    echo "\t\t<td$row_color>$mlbteamabbv</td>\n";
    echo "\t\t<td$row_color$row_color align=\"center\">$pointsround</td>\n";
    echo "\t\t<td$row_color align=\"center\">$gs</td>\n";
    echo "\t\t<td$row_color align=\"center\">$cg</td>\n";
    echo "\t\t<td$row_color align=\"center\">$wins</td>\n";
    echo "\t\t<td$row_color align=\"center\">$losses</td>\n";
    echo "\t\t<td$row_color align=\"center\">$saves</td>\n";
    echo "\t\t<td$row_color align=\"center\">$ip</td>\n";
    echo "\t\t<td$row_color align=\"center\">$ko</td>\n";
    echo "\t\t<td$row_color align=\"center\">$bb</td>\n";
    echo "\t\t<td$row_color align=\"center\">$ha</td>\n";
    echo "\t\t<td$row_color align=\"center\">$sobb</td>\n";
    echo "\t\t<td$row_color align=\"center\">$k9</td>\n";
    echo "\t\t<td$row_color align=\"center\">$er</td>\n";
    echo "\t\t<td$row_color align=\"center\">$era</td>\n";
    echo "\t\t<td$row_color align=\"center\">$whip</td>\n";
    echo "\t</form></tr>\n";[/code]

if the user is logged in and has players tracked in their profile (if myplayer_id = player_id) show a check mark image next to each player being tracked. (if myplayer_id = player_id). Else show the submit button

this is the check image next to each player currently being tracked
[code] echo "<td$row_color align=\"center\"><img src=\"images/ismyp.gif\" border=\"0\"></td>";[/code]

if the user is logged in AND clicks submit on a new player to track, then do this:

[code]$sessu = mysql_escape_string($_SESSION['username']); // check to see what the logged in username is

$result = mysql_query("SELECT id from users WHERE username = '$sess'"); // finds the user id based on username

while ($row = mysql_fetch_array($result))
{
    $uid = $row['id'];
}

$plid = ($_POST['myplayers']);

    $sql = "INSERT INTO myplayers (user_id, player_id) VALUES ($id, $plid)";
    mysql_query ($sql) or die(mysql_error());   // does the insert into the users table

}  [/code]


if the user is not logged in AND clicks submit next to one of the players...redirect to the login page

[code]if(($logged_in == 0) && isset($_POST['myplayers'])) {
header("Location: http://www.fantasybaseballresearch.com/login.php");
    echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><meta http-equiv="refresh" content="0; url=/login.php">HERE</a> to be redirected</div></body></html>';
    exit;
  }  [/code]

Share this post


Link to post
Share on other sites
Please see the above post.

This is how my current output looks when I'm logged in. It shows the check next to the players that I'm currently tracking...but for the ones I'm not, it does not show the submit...

[img src=\"http://img155.imageshack.us/img155/363/current9kh.gif\" border=\"0\" alt=\"IPB Image\" /]


This is what I have so far (this is only partial):

[code]<?php
for ($i = $start; $i < $page_limit; $i++)
{
    // Row color selector
    $row_color = ( !($i % 2) ) ? ' bgcolor=#ffffff' : '';
    $lname = $stat_list[$i]['lname'];
    $fname = $stat_list[$i]['fname'];
    $gs = $stat_list[$i]['gs'];
    $cg = $stat_list[$i]['cg'];
    $wins = $stat_list[$i]['wins'];
    $losses = $stat_list[$i]['losses'];
    $saves = $stat_list[$i]['saves'];
    $player_id = $stat_list[$i]['player_id'];
    $mlbteamabbv = $stat_list[$i]['mlbteam_abbv'];
    $ip1 = $stat_list[$i]['ip'];
        $ip = format_number($ip1, 1);
    $ko = $stat_list[$i]['ko'];
    $bb = $stat_list[$i]['bb'];
    $ha = $stat_list[$i]['ha'];
    $sobb = $stat_list[$i]['sobb'];
    $k9 = $stat_list[$i]['k9'];
    $er = $stat_list[$i]['er'];
    $era = $stat_list[$i]['era'];
    $whip = $stat_list[$i]['whip'];
        $points = $stat_list[$i]['fbr_points'];
    $pointsround = ROUND($points, 1);

?>
<form name="myplayers" method="post" action="<?php echo $PHP_SELF;?>">
<?php
echo "<tr>";
echo "<td$row_color align=\"center\">";
$sessu = mysql_escape_string($_SESSION['username']);

$result = mysql_query("SELECT id FROM users WHERE username = '$sessu'");
if (!$result) {
   die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($result))
{
    $id = $row['id'];
}

$result = mysql_query("SELECT player_id FROM myplayers WHERE user_id = '$id'");
if (!$result) {
   die('Invalid query: ' . mysql_error());
}

while ($row = mysql_fetch_array($result))
{
$mpid = $row['player_id'];

if ($logged_in == 1)
{
  if ($mpid == $player_id)

    {
echo "<img src=\"images/ismyp.gif\" border=\"0\"></td>";
    }

  elseif (!$result)  {
echo "<input type=\"image\" src=\"images/addmyp.gif\" value=\"$player_id\"
name=\"myplayers\"></td>";
        }
  }
         }
    echo "\t\t<td$row_color><a href='/baseball/players.php?player_id=$player_id'>$fname $lname</td>\n";  
    echo "\t\t<td$row_color>$mlbteamabbv</td>\n";
    echo "\t\t<td$row_color$row_color align=\"center\">$pointsround</td>\n";
    echo "\t\t<td$row_color align=\"center\">$gs</td>\n";
    echo "\t\t<td$row_color align=\"center\">$cg</td>\n";
    echo "\t\t<td$row_color align=\"center\">$wins</td>\n";
    echo "\t\t<td$row_color align=\"center\">$losses</td>\n";
    echo "\t\t<td$row_color align=\"center\">$saves</td>\n";
    echo "\t\t<td$row_color align=\"center\">$ip</td>\n";
    echo "\t\t<td$row_color align=\"center\">$ko</td>\n";
    echo "\t\t<td$row_color align=\"center\">$bb</td>\n";
    echo "\t\t<td$row_color align=\"center\">$ha</td>\n";
    echo "\t\t<td$row_color align=\"center\">$sobb</td>\n";
    echo "\t\t<td$row_color align=\"center\">$k9</td>\n";
    echo "\t\t<td$row_color align=\"center\">$er</td>\n";
    echo "\t\t<td$row_color align=\"center\">$era</td>\n";
    echo "\t\t<td$row_color align=\"center\">$whip</td>\n";

    echo "\t</form></tr>\n";
}

?>
</table>
<?php
if(($logged_in == 0) && isset($_POST['myplayers'])) {
header("Location: http://www.fantasybaseballresearch.com/login.php");
    echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><meta http-equiv="refresh" content="0; url=/login.php">HERE</a> to be redirected</div></body></html>';
    exit;
  }  elseif (($logged_in == 1) && isset($_POST['myplayers']))  {

$plid = ($_POST['myplayers']);
$sessu = mysql_escape_string($_SESSION['username']);

$result = mysql_query("SELECT id from users WHERE username = '$sess'");

while ($row = mysql_fetch_array($result))
{
    $uid = $row['id'];
}

$plid = ($_POST['myplayers']);

    $sql = "INSERT INTO myplayers (user_id, player_id) VALUES ($id, $plid)";
    mysql_query ($sql) or die(mysql_error());

} [/code]

Share this post


Link to post
Share on other sites

×

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.