Forums

Trouble with if statements

cjcrun 01 Jun, 2009
Hello,
Firstly, thank you for a fantastic compnent! :mrgreen:
On to business...

I am trying to accomplish the following 'simple' scenario, and yet it just doesn't want to play fair!

I have a MySQL table with a column "call_prior" (which defines call priorty) with priorities being high to low, 1-5.
My goal is trying to get the numerical value of call_prior to equal a text translation ("1" = "Emergency")..and later be represented by differing colours.

My code is this: in header -
< ?php
$priority = "{call_prior}"; 
 if ($priority == "1") {$priority = "EMERGENCY";}
   elseif ($priority == "2") {$priority =  "ASAP";}
   elseif ($priority == "3") {$priority =  "HIGH";}
   elseif ($priority == "4") {$priority =  "MED";}
   elseif ($priority == "5") {$priority =  "LOW";}  
 else { $priority = "I am unsure of the priority";}
?>

And then I have my table column echoing $priority. - body:
<tr align='center' style='white-space: nowrap;'>
<td style='border-right: 1px dotted #ccc;'><?php echo $priority; ?></td>
<td>{call_logg1}</td>
<td>{call_id}</td>
etc.


The problem I have is the script runs to the "else" statement regardless of what $priority equals. I have tried changing MySQL columns to int, or varchar (default INT) with no effect. the only time I can get the desired result is if I declare the variable absolutely (i.e $priority = "1")
If I remove the if and else statements, I get the numerical values accordingly.

Any help would be appreciated.
Regards,
CJ
nml375 01 Jun, 2009
Hi,
Try replacing {call_prior} with JRequest::getVar('call_prior');

I would also suggest looking at the switch conditional:

<?php
  switch (JRequest::getInt('call_prior')) {
   case 1:
    $priority = "EMERGENCY";
    break;
   case 2:
    $priority = "ASAP";
    break;
   case 3:
    $priority = "HIGH";
    break;
   case 4:
    $priority = "MED";
    break;
   case 5:
    $priority = "LOW";
    break;
   default:
    $priority = "I am unsure of the priority";
  }
?>


/Fredrik
cjcrun 02 Jun, 2009
Thanks Fredrik I appreciate the quick reply!

I have found the following in applying the recommended code:

As it didn't work😢 I added the following for discovery:
<?php
$priority = "{call_prior}";
$prior = JRequest::getInt('call_prior');
$pri = JRequest::getVar('call_prior');
var_dump("$priority"."$prior"."$pri");
?>


$priority returned a number (as is visible in the call_prior column)
$prior returned 0
$pri returned NULL

That said, it should make sense that :
<?php
switch ($priority = "{call_prior}") {...
returns the proper value and would allow the case definitions to execute properly.

It doesn't. It still runs to the default:"I am unsure of the priority" command

My next thought was that maybe it was to do with the MySQL table...I ensured it was MYISAM utf-8 and that the call-prior was set to a variety of things (tried tinyint(5), int(5), Varchar(255)) still no luck.

I am wondering if there is anything visible I might be missing in the following:
[attachment=0]prior_settings.jpg[/attachment]:

I will also include the scripts I have:

Body :-
<?php
$priority = '{call_prior}';
 switch ($priority) {
   case 1:
    $priority = "EMERGENCY";
    $back = "#f90";
   break;
  case 2:
   $priority = "ASAP";
$back = "#f30";
   break;
   case 3:
    $priority = "HIGH";
   break;
   case 4:
    $priority = "MED";
    break;
   case 5:
    $priority = "LOW";
    break;
   default:
   $priority = "I am unsure of the priority";
   $back = "#39c";
  }

//var_dump($priority);
?>
<tr align="center" style="white-space: nowrap;">
<td style="border-right: 1px dotted #ccc; color: <?php echo $back; ?> ;"><?php echo $priority; ?></td>
<td>{call_logg1}</td>
<td>{call_id}</td>
<td>{call_exter}</td>
<td>{call_statu}</td>
<td>{call_logge}</td>
<td>{call_branc}</td>
<td>{call_termi}</td>
<td align='left'>{edit_record} {call_descr}</td>
</tr>
<tr><td colspan='10' style='border-top: 1px solid #ccc; border-bottom: 1px solid #ccc; padding:2px; background: #ececec;'> </td>
<
/tr>

Sincerely, severely...
Stumped
cjcrun 02 Jun, 2009
We can mark this as closed if you wish.

I have altered my solution to be graphicly reproduced.

I created the following images:
1.png, 2.png, 3.png etc...

and instead of having a If/esle, switch, or whatever, I decided to change the code to:

<td><?php echo "<img src='http://[your site here]/images/M_images/{call_prior}.png' border='0'>"; ?></td>


simple, but effective.

Thanks again for your help Fredrik!
nml375 02 Jun, 2009
Ohh, my apologies. I had ChronoForms in my head.. 😶
(obviously, we should not use JRequest here at all, since we're dealing with database results - my bad)

Good that you did find a solution that works for you.
However, it should not be impossible to get your code working properly. could you please tell me your setting of the "Body Loop"; especially, if it's "Outside Body Loop" - try and changing it to "Inside Body Loop" with your original code (or the switch version, your version).

/Fredrik
This topic is locked and no more replies can be posted.