Venorize Posted November 6, 2011 Share Posted November 6, 2011 [About the code] Basically what I have is a web page that de-codes a complex XML document, and displays it as a table. This is working successfully. One of the columns in the tables displays a "rank" of an object as a number (0-9). This is working successfully. What I did was write a code that would translate that rank number into a specified text, depending what number it was. [The Code] (This is only the partial code with the problem in it, the entire code of the page is very extensive.) $RankNum = "{$Member->Rank}"; // {$Member->Rank} successfully displays the rank number on it's own. Below 'should' translate that number into the name of the rank. if('$RankNum' == 0) { echo('test1');}; if('$RankNum' == 1) { echo('test2');}; if('$RankNum' == 2) { echo('test3');}; if('$RankNum' == 3) { echo('test4');}; if('$RankNum' == 4) { echo('test5');}; [The Problem] All this code keeps displaying "test1" for every value in the entire column, regardless if it equaled 1 or 2 or higher... I know this has to be some simple coding mistake, but I've been sitting here for an hour trying to figure out what I had done wrong. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/ Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 You need to learn your basic PHP syntax if('$RankNum' == 0) { echo('test1');}; should be if($RankNum == 0) { echo('test1'); } http://php.net/manual/en/control-structures.if.php Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/#findComment-1285599 Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 To elaborate on that, you don't need any quotes around a variable. But if you are using single quotes, the value inside the quotes is NOT parsed for variables. If you are using double quotes, then the value inside IS parsed for variables. So you were comparing a string that starts with a dollar sign followed by 'ranknumber' with a number, instead of comparing the value of the variable. Also, this: $RankNum = "{$Member->Rank}"; Can just be this: $RankNum = $Member->Rank; There is no reason for the quotes or the braces. But since you used double quotes, that variable was properly parsed. Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/#findComment-1285600 Share on other sites More sharing options...
Venorize Posted November 6, 2011 Author Share Posted November 6, 2011 I guess I was overdoing it trying to keep everything in the correct brackets and such. I've only been at this for about a month. I've got a pretty successful database going based off what I learned by googling... Usually I can figure out my own mistakes, But this one just had me stuck for hours and I figured it was a good time to ask for help. This works now, thank you so much guys Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/#findComment-1285603 Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 It's nothing that probably 99% of us didn't learn the exact same way you did! Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/#findComment-1285605 Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2011 Share Posted November 6, 2011 I would use a 'lookup' array so that you don't need to keep writing or changing the php code every time you add or change a rank (just change the definition of the array) - <?php $rank_text = array(); $rank_text[0] = 'test1'; $rank_text[1] = 'test2'; $rank_text[2] = 'test3'; $rank_text[3] = 'test4'; $rank_text[4] = 'test5'; $RankNum = 3; // testing testing... if(isset($rank_text[$RankNum])){ echo $rank_text[$RankNum]; } else { echo "RankNum: $RankNum is not valid"; } Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/#findComment-1285607 Share on other sites More sharing options...
Venorize Posted November 6, 2011 Author Share Posted November 6, 2011 Thanks again everyone, PFMaBiSmAd I actually might use that for a different function, I see how it can be helpful if the variables change enough, but my ranks will never change past the 100 that I have already (I only listed a few as an example). Now I wasn't going to post about this, but I might as well ask since your all so incredibly helpful The only other issue I have is that one of the columns lists a Date, down to the millisecond. An example of one of the things listed is: 2011-10-29T04:19:36.75 That is how the format is in the XML document it is extracting it from. Is there a way I can have it cut off ANYTHING at that "t" and after? I don't need the time, just the date listed. Even further, if possible, to re-arrange the date from "2011-10-29" to maybe like "Oct 29, 11" ? Again this data is being extracted by php from an XML document which is updated weekly, and there is nearly two thousand lines of data it extracts, so changing them manually would be out of the question. Finally, like I said above, the table is already completed, I already have the code that extracts all the dates and displays them in the table column. I just need the code to read a date it gives "2011-10-29T04:19:36.75" and display it without the timestamp after it (and possible in a better format if that's even possible). To change it like I did with the ranks isn't possible with nearly 2,000 lines of data. lol. Thanks again, I tried to explain the issue as clearly as I could! Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/#findComment-1285646 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 Use the strpos function to find the T, then use substr to grab everything in front of the T. Alternately, you can use list in combination with explode, using T as a delimiter. From there, you can use strtotime and date to convert the string into a timestamp, and then a formatted date. Alternately, you can use explode with - as a delimiter to split the string into each part, and convert the month into it's text equivalent using the array method provided above by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/#findComment-1285651 Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2011 Share Posted November 6, 2011 If you have 100 ranks, you will find that the code I posted will execute significantly faster than using 100 if(){} statements. Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/#findComment-1285681 Share on other sites More sharing options...
Venorize Posted November 10, 2011 Author Share Posted November 10, 2011 Use the strpos function to find the T, then use substr to grab everything in front of the T. Alternately, you can use list in combination with explode, using T as a delimiter. From there, you can use strtotime and date to convert the string into a timestamp, and then a formatted date. Alternately, you can use explode with - as a delimiter to split the string into each part, and convert the month into it's text equivalent using the array method provided above by PFMaBiSmAd Substr worked wonders, it solved all my display issues! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/250566-problems-executing-multiple-if-statements/#findComment-1286878 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.