xwishmasterx Posted March 30, 2011 Share Posted March 30, 2011 I have simple piece of code to get the value of 'tmap' from a members id: $query = "SELECT tmap FROM vtp_members WHERE id='$id'"; what I need is to echo different things depending on the value of 'tmap', but don't know how to do this: example: ----------- if tmap is 0 echo "tmap is 0"; elseif tmap is 1 echo "tmap is 1"; and so on to max number of 5 --------------------------- anyone can show how to write this? Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/ Share on other sites More sharing options...
gristoi Posted March 30, 2011 Share Posted March 30, 2011 You might want to use a switch statement. It has the same effect as elseif, but easier syntax to follow: switch(tmap) { Case 0: echo 'selected 0'; Break; Case 1: echo 'selected 1'; Break; Default: echo 'no tmap selected'; Break; Just remember that you should always use break after each switch statement otherwise the code will continue executing . Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194144 Share on other sites More sharing options...
xwishmasterx Posted March 30, 2011 Author Share Posted March 30, 2011 thanks for your reply. I can't get it too work (bear in mind I am really new to php ) if I setup my code like this it returns error: $query = "SELECT tmap FROM vtp_members WHERE id='$id'"; switch(tmap) { Case 0: echo 'selected 0'; Break; Case 1: echo 'selected 1'; Break; Default: echo 'no tmap selected'; Break; Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194150 Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 That is a really painful way to do that. One much better way -- use an array: $tmapmsg = array('tmap 0 msg', 'tmap 1 msg', 'tmap 2 msg', 'tmap 3 msg', 'tmap 4 msg', 'tmap 5 msg'); //... do your query, fetch row echo $tmapmsg[$row['tmap']]; Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194151 Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 thanks for your reply. I can't get it too work (bear in mind I am really new to php ) $query = "SELECT tmap FROM vtp_members WHERE id='$id'"; You need to read the section of php manual on mysql_query() Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194153 Share on other sites More sharing options...
xwishmasterx Posted March 30, 2011 Author Share Posted March 30, 2011 I have read a great deal of info on this, but I think it's one part of it I do not understand. If you use this code: $query = "SELECT tmap FROM vtp_members WHERE id='$id' "; $result = mysql_query($query); $numrows = mysql_num_rows($result); if($numrows == '1') something here won't it just return 'something here', as it found a result? It doesn't matter what the tmap value is. Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194159 Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 When you query the database the server generates a result set on the server. You then have to fetch the data. There's various functions, but I usually recommend mysql_fetch_assoc() which gives you an associative array where the array keys are the names of the columns in your result set. So your 'something here' should be: $row = mysql_fetch_assoc($result); Assuming I guessed your structure correctly the array code I provided should work. Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194163 Share on other sites More sharing options...
xwishmasterx Posted March 30, 2011 Author Share Posted March 30, 2011 This php coding, makes me feel SO stupid so if my code is: $query = "SELECT tmap FROM vtp_members WHERE id='$id' "; $result = mysql_query($query); $row = mysql_fetch_assoc($result); if($row == '1') { echo "tmap is 1";} elseif($row == '2') { echo "tmap is 2";} else { echo "tmap is not 1 or 2";} how come it echoes "tmap is not 1 or 2" although tmap value is in fact 2? Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194169 Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 You need to understand php arrays. I wrote you a detailed response. $row is an "associative array". If you don't understand what it is, you should read the manual. A hint would have been looking at the code I provided you above. if ($row['tmap'] == 1) { ---- I personally don't know if tmap is the right key or not because I don't know your database structure. I'm guessing that is the right name. For learning about things, the var_dump() function is very helpful. var_dump($row); And you'll get some output that shows you what $row actually contains after the fetch. You should get output that indicates that you have an arrray structure, and the contents of that array. Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194176 Share on other sites More sharing options...
gristoi Posted March 30, 2011 Share Posted March 30, 2011 If you look at gizmola's last message he advised you that the result returns an associative array. What this means is that the data returned from the server is like this: Array('tmap'=> 1, 'field2'=> something ........... So when you are trying $row=1 you are not accessing what you need from the array. Try if ($row['tmap']==1) Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194177 Share on other sites More sharing options...
xwishmasterx Posted March 30, 2011 Author Share Posted March 30, 2011 I still can't get it too work like that, however I looked around for examples and cooked up this: <?php include("system.php"); if (isset($r)){ $r = $_GET['r']; }else {$r = 'No User Id';} $r=intval($r); if($r!=0){ SetCookie("r",$r,time()+36000); } ?> <?php $tmap = mysql_query("SELECT * FROM vtp_members WHERE id=".$_GET['r']." ") or die(mysql_error()); $result = mysql_fetch_array( $tmap ); echo "tmap value for ".$_GET['r']." is ".$result['tmap'] . " "; ?> the result: "tmap value for 58002 is 2 " which is correct! Is there something in the code that could cause problems? Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194183 Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 I'm not sure what it is that you couldn't get working. mysql_fetch_array() is exactly the same as mysql_fetch_assoc() only it returns a 2nd copy of the data that is numerically indexed along with the associative array. There is some weird code going on... for example, i have no idea why you are setting a cookie. The obvious thing that jumps out, is that you have some hygiene in regards to the $_GET['r'] parm where you store it to the $r variable, and then you do $r=intval($r); The purpose of that is to make sure that they don't try and slip in a sql injection, but later you drop back to using the $_GET['r'] variable in your query, defeating the purpose of this. Once you clean the data, you should use $r, and no longer use $_GET['r'] which is data that can not be trusted. Also, purely from a performance standpoint, you might as well know that casting the value to an (int) is much faster than calling the intval() function. I won't go into this much, but just take it on faith that you should instead do: $r = (int)$r; However the more important thing is that later, use $r, and not $_GET['r'] in your query. As it is, it seems you're making some progress and no doubt learning a lot as you go. This thread already has a lot of helpful tips in it that you might want to re-read and study. Truthfully, the code at the top of your script looks to have a number of issues, but since we don't really know your objective, it's hard to provide any further practical advise. Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194187 Share on other sites More sharing options...
xwishmasterx Posted March 30, 2011 Author Share Posted March 30, 2011 i might look into that first piece of code which was something I just grabbed (needing the $r value). Allthough my code work now, I still have some problems as I need the values from 'tmap' to be retuned with an increase of '1'. so when using .$result['tmap'] . it returns the value of tmap, lets say 2 I need it too return 3 (tmap+1 = so 2+1 =3) How can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194212 Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 The beauty of php is that you simply add to the value. . $result['tmap'] + 1 . Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194227 Share on other sites More sharing options...
xwishmasterx Posted March 30, 2011 Author Share Posted March 30, 2011 it seems I got this working;) Can you do me a favor and rewrite the code too use $r=intval($r); and $r = (int)$r; I tried using the $r but it returns "0". Think my problem is I do not know when to use dots: $r or .$r and so on. The cookie has no purpase here, so I have deleted that Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194245 Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 There is nothing to rewrite. $r=intval($r) and $r = (int)$r do the same thing -- convert $r to a php "integer" type variable. They just use different methods to get to the same place. Substitute the $r = (int)$r for the intval line. The syntax is "casting" the variable to the type shown in the parens ie. (int). In most cases php will do internal typecasting for you, but this is a special case where if someone tried to enter a string as the url parameter, all that will happen in the casting process is that php will set $r to be '0'. Your code seemed to handle this fine. Show me the exact code you have and I'll look at it again. There is logic where it is checking to see if isset($r) which doesn't make any sense from what I can see. That probably can go away. Quote Link to comment https://forums.phpfreaks.com/topic/232141-simple-if-elseif-how-does-it-work/#findComment-1194515 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.