Jump to content

Problems executing multiple IF Statements.


Venorize

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

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.