Jump to content

How can I display an image if a field for a user has a specific value?


user51

Recommended Posts

Ladies and gentlemen, my question might be simple to you though i spent a lot of time in my phpmyadmin and dreamweaver trying to get it to work, yet here i am.

i am trying to set up a game, database, php system already setup. however im having a problem;

my database has values called ID, name, kind,... etc.

id has a numeric value, name has characters, kind a numeric value.. and so on.

 

i would like to display a  flag (image) that is specific to the user that logs in, for example;

 

if kind->1

display flags/flag.png;

if kind ->2

display flags/flag1.png;

 

this way when the user is displayed in a list or on his own home page he would have a flag next to his name

 

 

i mean the above is not valid but thats what im trying to do.

 

the code im trying looks like this;

 

<?php

 

$sql = "SELECT `kind` FROM `users`";

if ( kind == 3 ) {

echo '<img src="flags/flag.png">';

} else {

echo "<img src="flags/default.png">";

}

?>

 

but this doesnt seem to work

please, what am i doing wrong? is there a way to do this differently?

Thank you in advance for your reply and help.

Link to comment
Share on other sites

You'd do this.

 

<?php
$sql = "SELECT * FROM `users`";
$row = mysql_fetch_assoc($sql);
if ($row['kind'] == '3' ) {
   echo '<img src="flags/flag.png">';
} else {
   echo "<img src="flags/default.png">";
}
?>

 

Though if you want to be more technical, and easier to add more kinds in. You can do a switch. Like so..

 

<?php
$sql = "SELECT * FROM `users`";
$row = mysql_fetch_assoc($sql);
switch($row['kind']) {
       default:
                  echo "<img src="flags/default.png">";
       break;

       case "3":
                   echo '<img src="flags/flag.png">';
       break;
}

 

Note that to add in more cases. You just do..

<?php
case "value here": //COLON NOT A SEMI
echo stuff here...
break;
?>

 

If kind == 3, your case would be case "3":

If you have.. kind - 1 2 3 4 5, you'd simply put..

case "1":

case "2":

case "3":

case "4":

case "5":

With the breaks of course.

It is possible to display information for all 5 in one case. This is called a 'fallthrough'

Looks a bit like this..

 

<?php
switch($row['kind']) {
         default:
                    echo 'default here'
         break;

        case "1":
        case "2":
        case "3":
        case "4":
        case "5":
                    echo 'Display the info that gets displayed for all these cases';
        break;
}
?>

 

The values in the case. eg: 1, is the value of the kind. So.. if a users kind == 1, the case "1" will get displayed for that user..

 

Or for the sake of telling everything, you can use other syntax for this.

 

<?php
switch($row['kind']) :
       default:
                  echo "<img src="flags/default.png">";
       break;

       case "3":
                   echo '<img src="flags/flag.png">';
       break;
endswitch;
?>

 

I hope I made myself clear :)

 

Link to comment
Share on other sites

dear, TeddyKiller;

 

thank you for your quick and thorough reply, however im running into a recurrent problem,

 

i keep on getting the 'default' answer even though the user is logged in.

 

 

 

<?php

switch($row['kind']) :

      default:

                  echo "<img src="flags/default.png">";

      break;

 

      case "3":

                  echo '<img src="flags/flag.png">';

      break;

endswitch;

?>

 

am i doing something wrong?

Link to comment
Share on other sites

You'll need the query above the switch

$sql = mysql_query("SELECT * FROM `users`");
$row = mysql_fetch_assoc($sql);

 

If your wanting a certain image of the current logged in user. You'll need a WHERE clause.

If your users id is in a session. You could do this for a query.

$sql = mysql_query("SELECT * FROM `users` WHERE id='".$_SESSION['user_id']."'");

Hope tihs helps.

Link to comment
Share on other sites

dear TeddyKiller,

that is my mistake, i already did try with all options such as;

 

<?php

 

if ($_SESSION['isLogined']){

 

$sql = mysql_query("SELECT * FROM `users` WHERE id='".$_SESSION['user_ID']."'");

$row = mysql_fetch_assoc($sql);

 

switch($row['kind']) :

      default:

                  echo '<img src="flags/flag1.png">';

      break;

 

      case "2":

                  echo '<img src="flags/flag.png">';

      break;

endswitch;

}

?>

 

however i am still getting the default image displayed, it seems that the query is not running the table.

 

i did try the code in phpmyadmin;

 

when i input;

 

$sql = "SELECT `kind` FROM `users` WHERE 1 LIMIT 0, 30 ";

 

it does display the column where the kind is displayed,

the switch, such as you already displayed above (thank you) should work, if the session has begun, and the session has begun.

 

i even tried;

 

$user=getUserDetails($_SESSION['isLogined']," kind ");

 

but i still get the default image displayed.

 

am i stuck in the twilight zone? :confused:

 

Link to comment
Share on other sites

I'm not sure on the problem.

However, try remove

$sql = mysql_query("SELECT * FROM `users` WHERE id='".$_SESSION['user_ID']."'");
$row = mysql_fetch_assoc($sql);

Replacing it with

$row['kind'] == '2';

See if it works like that. If so, then its the query problem. You'd might not have a user with the kind of value 2

Link to comment
Share on other sites

dear TeddyKiller,

once again i tried the modifications on the page but still it displays the default value.

the table does contain all values possible, in terms of "kind" and i also tried login in a different user but no luck.

 

at this time ill just disconnect and try some more research then ill post it if i do any progress.

 

thank you for all your effort.

 

user51

Link to comment
Share on other sites

Looks like the query is not pulling out the right user, or none at all. The first step to debugging a query is to put it in a variable and print it out to see exactly what is getting exectuted:

 

$query = "SELECT * FROM `users` WHERE id='".$_SESSION['user_ID']."'";

die($query);

 

Then copy and paste it into phpMyAdmin to see what results are being returned. Also when selecting a single row ie by the row ID, it's good practice to put LIMIT 1 onto the end of the query, this tells the MySQL server to not bother looking any further once it finds a row.

Link to comment
Share on other sites

Only other option is, something wrong with the switch (cant see where) or you don't have session start() at the top of the page, along with that $_SESSION['isLogined'] may not be set. :)

 

session is started, i know that because what im tying to display, as i explained earlier is a flag of users kind, all the other stats are displaying, attack , defense, and stuff like that.

 

<? numecho ($user->attackTurns) ?>

 

the above code is how i display ammo, it displays a number.

 

Looks like the query is not pulling out the right user, or none at all. The first step to debugging a query is to put it in a variable and print it out to see exactly what is getting exectuted:

 

$query = "SELECT * FROM `users` WHERE id='".$_SESSION['user_ID']."'";

die($query);

 

Then copy and paste it into phpMyAdmin to see what results are being returned. Also when selecting a single row ie by the row ID, it's good practice to put LIMIT 1 onto the end of the query, this tells the MySQL server to not bother looking any further once it finds a row.

 

phpmyadmin does spit the number 1 when i run test queries on it however it does not recognize

 

id='".$_SESSION['user_ID']."'";

 

......

 

 

Link to comment
Share on other sites

If the 'kind' value doesn't change often, maybe you should have the value initially pulled when the user logs in and enter it into the $_SESSION variable. Then you dont need to call back to the DB so much. Then you can dump $_SESSION to see what the person is getting, also make sure default is at the end of your switch.

Link to comment
Share on other sites

guys, i am a dumb @@@, the query is runing just fine as of ;

 

<?php

 

if ($_SESSION['isLogined']){

 

$sql = mysql_query("SELECT `kind` FROM `users`");

$row = mysql_fetch_assoc($sql);

 

switch($row['kind']) :

     

 

      case "1":

                  echo '<"case 1">';

      break;

 

  case "2":

                  echo '<"case 2">';

      break;

 

  case "3":

                  echo '<"case 3">';

      break;

 

  case "4":

                  echo '<"case 4">';

      break;

 

  default:

                  echo '<"default">';

      break;

 

endswitch;

}

?>

 

 

---------------------

 

however the query is sorting players  in the order they registered, so every time i try to run the query, it shows me the value of the player first registered, in my case,  its kind =2,

 

today i was able to display case 2 for one of the test characters im developing but.. as i forementioned im running to the same problem

 

now im trying to call the query such as this;

 

$sql = "SELECT DISTINCT `kind`,`ID`  FROM users LIMIT 0, 30 ";

 

phpmyadmin responds good to this command but i still get the default,

 

 

i feel im getting closer but.... still no luck

 

my next question is ...

 

how do i make sure that i call this specific user id and kind?

 

 

i mean, i would assume that it would be already called if the session has begun but its not doing it.

 

something like

 

if user_ID = (this specific player) then call the 'kind' row, if 'kind' row = (this specific player) then echo <image>

 

Link to comment
Share on other sites

session is started on index.php as of

 

<?

session_start();

include "system.php";

?>

 

system includes

a library.php file with

 

 

 

$conf["mail"]="..............";

 

$conf["sites_database_name"] = ".......";

$conf["sites_database_name"] = "......";

$conf["sites_database_login"] = "..........";

$conf["sites_database_login"] = "..........";

$conf["sites_database_password"] = "...........";

$conf["sites_database_password"] = ".........";

 

 

the page called from index.php is main.php

 

main.php starts with a

 

<?php

session_start();

?>

 

main.php also contains the login page.

 

 

  <TR>

                        <TD height="24" align=middle>

                        <div align="center"><font color="black" class="style1">Username:</font></div></TD></TR>

                      <TR>

                       

                        <TD height="39" align=middle><div align="center">

                          <input name=uname class=style2 />

                        </div></TD>

                </TR>

                      <TR>

                        <TD height="98" align=middle>

                          <div align="center"><span class="style1"><font color=black>Password:</font></span></div>

                    <label>

                          <div align="center">

                            <input name="psword" type="text" class="style2" value="" />

                          </div>

                    </label></TD></TR>

                      <TR>

                        <TD height="30" align=top style="PADDING-TOP: 5px"><div align="center">

                          <input name="submit" type=submit class=style1 style="WIDTH: 250px" value=Login />

                        </div></TD>

                </TR>

                  </TBODY></TABLE>

              <INPUT

            type=hidden value=@@@@@@@@@@@@@@@@@ name=hash>

            </FORM></TD></TR>

 

 

 

calls such as

 

<? numecho ($user->cash ?>

do display as numbers on the php page,

 

i also am using a standalone php to verify that the user is in fact loged in

 

 

<?

if ($_SESSION['isLogined']){

echo "LOGED IN";

 

}

else {

echo "<br><center>You are not loged in. Try again.</center><br>";

 

exit;

}

?>

 

which echoes loged in right above the script im trying to run.

 

:confused: :confused: :wtf:

 

 

latter ill try to create a separate login and retrieve the values from the table, maybe i can call them that way.

 

Link to comment
Share on other sites

if you are having the code "session_start" on both the index.php and main.php then there could be a conflict there if you are "including" the main.php file into the index.php file. Doing so would effectively be firing the session_start twice in one file which php throws up an error for.

 

To fix the issue just remove the "session_start" in the main.php file

Link to comment
Share on other sites

dear teddykiller, i apologize for not using the php tags, i didnt think i would have such a hard time on this problem,

below is the source code called from

 

index.php.

<?php
session_start();
include "system.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<script src="javascript/functions.js" type="text/javascript"></script>
<title>mark 1</title>
<P align=center><FONT color=#000000> 
                  <span class="style4">
                  <?=getOnlineUsersCount()?>
                  online</span><span class="style4"></span></span></FONT></P>
<?php
include "main.php";

?>
</body>
</html>

 

this is main.php


<? 
        	if (!$_SESSION['isLogined']){

        ?>	
<style type="text/css">
<!--
.style1 {font-size: xx-large}
.style2 {font-size: large}
body {
background-image: url(if/Background%20Test.png);
}
a {
font-size: medium;
}
#Layer1 {
position:absolute;
width:46px;
height:31px;
z-index:1;
top: 1175px;
}
a:link {
color: #FF0000;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #999999;
}
a:hover {
text-decoration: underline;
color: #999999;
}
a:active {
text-decoration: none;
color: #FF0000;
}
.style6 {color: #A4A4A4}
.style7 {color: #070707}
.style9 {font-size: xx-large; font-weight: bold; }
-->
</style>

      <title>HomeBase</title><div align="center">
        <TABLE cellSpacing=0 cellPadding=0 width=272 border=0>
          <TBODY>
            <TR>
             
          </TR>
          
            <TR>
              <TD class=menu_cell_repeater>
                <FORM action=""  login.php method=post><INPUT type=hidden name=username> 
                  <INPUT type=hidden name=pword> 
                  <TABLE 
            width=270 height="203" align=center class=small 
            style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 5px; PADDING-TOP: 0px">
                    <TBODY>
                      
                      <TR>
                        <TD height="24" align=middle>
                        <div align="center"><font color="black" class="style1">Username:</font></div></TD></TR>
                      <TR>
                        
                        <TD height="39" align=middle><div align="center">
                          <input name=uname class=style2 />
                        </div></TD>
                </TR>
                      <TR>
                        <TD height="98" align=middle>
                          <div align="center"><span class="style1"><font color=black>Password:</font></span></div>
                    <label>
                          <div align="center">
                            <input name="psword" type="text" class="style2" value="" />
                          </div>
                    </label></TD></TR>
                      <TR>
                        <TD height="30" align=top style="PADDING-TOP: 5px"><div align="center">
                          <input name="submit" type=submit class=style1 style="WIDTH: 250px" value=Login />
                        </div></TD>
                </TR>
                  </TBODY></TABLE>
              <INPUT 
            type=hidden value=@@@@@@@@@@@@@@name=hash> 
            </FORM></TD></TR>
            <TR>
              <TD height="27" align=middle class=menu_cell_repeater_vert><A 
            href="register.php" class="style2">Register</A></TD>
          </TR>
            <TR>
              <TD height="27" align=middle class=menu_cell_repeater_vert><A 
            href="forgotpass.php" class="style2">Forgot 
              Login?</A></TD>
          </TR>
            <TR>
              <TD></A></TD>
          </TR>
            <?
      		}else{
      		setLastSeen($_SESSION['isLogined'],time());
			$user=getUserDetails($_SESSION['isLogined']);
			$userR=getUserRanks($_SESSION['isLogined']);
        ?>
            
            <TR>
            <TD><div align="center"><A href="logout.php"><IMG alt=Logout 
            src="imm/logout.png" width=137 
        border=0></A></div></TD></TR>
            <?
      	}
        ?>
  </TBODY>
        </TABLE>
      </div>
      <P align="center">
       <A href="index.php"> <?=$conf["race"][$user->race]["name"]?></A><span class="style2">
      <? numecho ($userR->rank) ?> <? 
    	if ($_SESSION['isLogined']){
    		
?>
      
      <div align="center">
        <table width="279" height="261" border="0">
          <tr>
            <td width="108">
              
              <div align="left">
                Ammo
                <table width="108" height="42" border="0" background="if/Ammo.png">
                  <tr>
                    <td width="50"><a href="armory.php"></a></td>
                    <td width="48"><div align="center" class="style2"><a 
                  style="FONT-SIZE: 9pt; COLOR: black" 
                  href="armory.php">
                      <? numecho ($user->attackTurns) ?>
                    </div></td>
                  </tr>
                </table>
            </div></td>
            <td width="110">
              
              <div align="left">
                Cash
                <table width="108" height="42" border="0" background="if/Cash.png">
                    <tr>
                      <td width="50"> </td>
                      <td width="48"><div align="center"><font color="#250202">
                        <? numecho ($user->gold) ?>
                      </font></div></td>
                    </tr>
                </table>
            </div></td>
          </tr>
          <tr>
            <td>
              
              <div align="left">
                Untrained
                <table width="108" height="42" border="0" background="if/Mercs.png">
                  <tr>
                    <td width="47"> </td>
                    <td width="51"><div align="center" class="style2">
                      <? numecho ($user->untrainedSold) ?>
                    </div></td>
                  </tr>
                </table>
            </div></td>
            <td>
              
              <div align="left">
                Cash/Hour
                <table width="108" height="42" border="0" background="if/Cash per Time.png">
                    <tr>
                      <td width="51"> </td>
                      <td width="47"><div align="center" class="style2"><a 
                  style="FONT-SIZE: 9pt; COLOR: black">
                        <? numecho (getUserIncome($user));?>
                      </a></div></td>
                    </tr>
                </table>
            </div></td>
          </tr>
          <tr>
            <td>
              
              <div align="left">
                Timer
                <table width="108" height="42" border="0" background="if/Next Turn.png">
                  <tr>
                    <td width="48"> </td>
                    <td width="50"><div align="center" class="style2"">
                      <?
				$nextTurnMin=getNextTurn($user); 
				$nextTurnMin=round($nextTurnMin/60);
				echo $nextTurnMin;
			?>
                    </div>                    </td>
                  </tr>
                </table>
            </div></td>
            <td>
              
              <div align="left">
                Rank
                  <table width="108" height="42" border="0" background="if/Rank.png">
                    <tr>
                      <td width="47"> </td>
                      <td width="51"><div align="center" class="style2">
                        <? numecho ($userR->rank) ?>
                      </div></td>
                    </tr>
                </table>
            </div></td>
          </tr>
          <tr>
            <td>
              
              <div align="left">
                Attack units 
                <table width="108" height="42" border="0" background="if/Attacks.png">
                  <tr>
                    <td width="49"><div align="center"></div></td>
                    <td width="49"><div align="center" class="style2"><div align="center" class="style2"><a 
                  style="FONT-SIZE: 9pt; COLOR: black" 
                  href="train.php">
                      <? numecho ($user->trainedAttackSold)?>
                    </div>                    </td>
                  </tr>
                </table>
            </div></td>
            <td>
              
              <div align="left">
                Defense Units
                  <table width="108" height="42" border="0" background="if/Defences.png">
                    <tr>
                      <td width="48"><div align="center"></div></td>
                      <td width="50"><div align="center" class="style2"><div align="center" class="style2"><a 
                  style="FONT-SIZE: 9pt; COLOR: black" 
                  href="train.php">
                        <? numecho ($user->trainedDefSold)?>
                      </div></td>
                    </tr>
                </table>
            </div></td>
          </tr>
          <tr>
            <td><div align="left">
              <div align="left">Mail
              </div>
              <table width="108" height="42" border="0" background="if/mail.png">
              <tr>
                <td width="51"> </td>
                <td width="47"><div align="center" class="style2"><a 
                  style="FONT-SIZE: 9pt; COLOR: black" 
                  href="inbox.php">
                    <?=getMessagesCount($user->ID)?>
                </a></div></td>
              </tr>
            </table></td>
            <td><div align="left">
              <div align="left">Help
              </div>
              <table width="108" height="42" border="0" background="if/Help.png">
              <tr>
                <td width="51"> </td>
                <td width="47"> </td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td><div align="left">
              Engage Enemy 
              <table width="108" height="42" border="0" background="if/Attack Icon.png">
                <tr>
                  <td width="48"> </td>
                  <td width="50"><div align="center" class="style2""><a href="battle.php">X</a></div></td>
                </tr>
              </table>
            </div></td>
            <td><div align="left">
              <table width="108" height="42" border="0" background="if/Next Turn.png">
                <tr>
                  <td width="48"> </td>
                  <td width="50"> </td>
                </tr>
              </table>
            </div></td>
          </tr>
          <tr>
            <td><div align="left">
              <table width="108" height="42" border="0" background="if/Next Turn.png">
                <tr>
                  <td width="48"> </td>
                  <td width="50"> </td>
                </tr>
              </table>
            </div></td>
            <td><div align="left">
              <table width="108" height="42" border="0" background="if/Next Turn.png">
                <tr>
                  <td width="48"> </td>
                  <td width="50"> </td>
                </tr>
              </table>
            </div></td>
          </tr>
          <tr>
            <td><a href="battle.php">battlefield</a></td>
            <td><span class="style9">
              <?=$user->userName?>
            </span></td>
          </tr>
        </table>
        <p>
// this is where im trying to run my script//	

        	<?php
session_start();

$sql = "SELECT * FROM `userdetails` WHERE `race` = 1 LIMIT 0, 30 ";

if(!$result) 
{ 
   echo "Query failed.<br />\n$sql<br />\n" . mysql_error(); 
} 
elseif(mysql_num_rows($result) == 0) 
{ 
   echo "No rows returned by query:<br />\n$sql"; 
}
?>			
        </p>
        <p>




	</p>
        <table width="279" height="261" border="0">

          
        </table>
     
      </div>
      <?
}
?>
      


 

 

messages such as

 

<?=getMessagesCount($user->ID)?>

 

display perfectly.

 

maybe you can see what im missing here.

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.