Jump to content


Photo

php if/else, mysql, etc ?


  • Please log in to reply
9 replies to this topic

#1 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 02 April 2006 - 11:57 PM

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 :

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

if there is no match, then show this:

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

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

#2 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 03 April 2006 - 02:10 AM

Do you mean like this?

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";
       }


#3 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 03 April 2006 - 02:34 AM

Thats probably pretty close. I tried it out and it only shows the else....

I tried this and it only showed the if :)

  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";
}

Maybe if provide more info, it will help.

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

$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";

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

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


#4 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 03 April 2006 - 02:52 AM

Getting back to basics......

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

else{
       echo "you need to log in";
       }

....returns "do this stuff"

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

else{
       echo "you need to log in";
       }

....returns "do this instead"

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

else{
       echo "you need to log in";
       }

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

which is all as it should be

#5 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 03 April 2006 - 03:48 AM

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.

<?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";

        }
  }


#6 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 03 April 2006 - 05:48 AM

Still can't figure this one out :( Anyone have any ideas?

#7 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 03 April 2006 - 08:45 PM

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.

#8 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 03 April 2006 - 09:27 PM

[!--quoteo(post=361341:date=Apr 3 2006, 04:45 PM:name=realjumper)--][div class=\'quotetop\']QUOTE(realjumper @ Apr 3 2006, 04:45 PM) View Post[/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.


#9 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 03 April 2006 - 10:14 PM

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)
        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";

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
echo "<td$row_color align=\"center\"><img src=\"images/ismyp.gif\" border=\"0\"></td>";

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

$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

}  


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

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;
  }  



#10 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 03 April 2006 - 10:51 PM

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

<?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());

}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users