July 18, 201115 yr Hi, I have this variable $invoice it can start with A Like A74675 or B Like B39947 or C. I want the $item_name to be red if the $invoice started with A and I want it to be blue if the $invoice is started with B and at the end to be black if the $invoice started with C. Is the there any way to achieve that? Youssef
July 18, 201115 yr I'm not great with PHP so there is a very good chance this is the wholly wrong way of going about it or it may not even work. but if(strstr($invoice,'A') === TRUE) { //Do something with A } elseif(strstr($invoice,'B') === TRUE) { //Do something with B } elseif(strstr($invoice,'C') === TRUE) { //Do something with C } else { // Do nothing } Again, PHP really isn't my thing so don't expect it to work but it's always worth a bash, right? =) I think strstr doesn't require perfect match and I think it starts at the beginning (IE won't just match any A in any string) but I'm not certain on that. Edited July 18, 201115 yr by Glowbridge
July 19, 201115 yr Author I'm not great with PHP so there is a very good chance this is the wholly wrong way of going about it or it may not even work. but if(strstr($invoice,'A') === TRUE) { //Do something with A } elseif(strstr($invoice,'B') === TRUE) { //Do something with B } elseif(strstr($invoice,'C') === TRUE) { //Do something with C } else { // Do nothing } Again, PHP really isn't my thing so don't expect it to work but it's always worth a bash, right? =) I think strstr doesn't require perfect match and I think it starts at the beginning (IE won't just match any A in any string) but I'm not certain on that. Thank you this worked perfect .
July 19, 201115 yr The PHP switch statement is good for comparing something against a series of values... <?php $i=$invoice[0]; switch ($i) { case "A": $colour="red"; break; case "B": $colour="blue"; break; default: $colour="black"; } ?> ...then if $item_name holds the actual text you want to output, you could do something like... echo "<span style=\"color:$colour\">$item_name</span>";
July 19, 201115 yr The PHP switch statement is good for comparing something against a series of values... <?php $i=$invoice[0]; switch ($i) { case "A": $colour="red"; break; case "B": $colour="blue"; break; default: $colour="black"; } ?> ...then if $item_name holds the actual text you want to output, you could do something like... echo "<span style=\"color:$colour\">$item_name</span>"; i was actually gonna suggest this method which is the most logical way to do it
July 19, 201115 yr Author The PHP switch statement is good for comparing something against a series of values... <?php $i=$invoice[0]; switch ($i) { case "A": $colour="red"; break; case "B": $colour="blue"; break; default: $colour="black"; } ?> ...then if $item_name holds the actual text you want to output, you could do something like... echo "<span style=\"color:$colour\">$item_name</span>"; This is a quite a good way to do it, thank you.
July 19, 201115 yr \o/ I'm not completely PHP dense. Ok so there is a better way of doing it, but my way didn't make the computer catch fire so I'm happy =) Edited July 19, 201115 yr by Glowbridge
July 19, 201115 yr Author \o/ I'm not completely PHP dense. Ok so there is a better way of doing it, but my way didn't make the computer catch fire so I'm happy =) Hehehehe I am still using your way I am lazy to change it now, so I will keep using it for a while.
Create an account or sign in to comment