Solved

Check $row for value and amend

Posted on 2014-02-10
11
367 Views
Last Modified: 2014-02-10
I  need help to check a row for a value and change it. The data is being returned to flexigrid via json and works well and without any errors. however, what I am trying to do is check the row; $row['status'] for a value = 1 and if that row is that value then change it to value = "In".

I have tried various ways to code using an if statement, but it does not error, just dosen't display the data in the grid. What is the correct way to code this row? Thanks

$results = mysql_query($sql);
while ($row = mysql_fetch_assoc($results)) {
$data['rows'][] = array(
'id' => $row['Id'],
'cell' => array($row['Id'], $row['rack'].$row['column'].'-'.$row['row'].'-'.$row['bay'], $row['customer'], $dropdown, $service, $row['department'], if ($row['status']==1){$row['status']="In"}, $row['custref'], $row['size'], $row['authorisation'], date('d/m/Y H:m:s',strtotime($row['intake_date'])), date('d/m/Y',strtotime($row['destroy_date']))));
}
echo $json->encode($data);

Open in new window

0
Comment
Question by:peter-cooper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39847395
You could set the value before building the array

$status = ($row['status']==1) ? 'In' : $row['status'] ;

$data['rows'][] = array(
'id' => $row['Id'],
'cell' => array($row['Id'], $row['rack'].$row['column'].'-'.$row['row'].'-'.$row['bay'], $row['customer'], $dropdown, $service, $row['department'], $status, $row['custref'], $row['size'], $row['authorisation'], date('d/m/Y H:m:s',strtotime($row['intake_date'])), date('d/m/Y',strtotime($row['destroy_date']))));
}

Open in new window

FYI, mysql_* is deprecated in PHP - you should switch to using mysqli or PDO ;)
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39847405
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39847406
I can't test this but I think the right answer might be here.  I reformatted it a bit for readability and added error checking on the query.

Going forward, you're going to need the information in this article.  PHP 5.5 is current, so the removal of MySQL from PHP is already underway.

$results = mysql_query($sql);
if (!$results) trigger_error('FAIL: $sql BECAUSE: ' . mysql_error(), E_USER_ERROR);
while ($row = mysql_fetch_assoc($results)) 
{
    if ($row['status'] == '1') $row['status'] = 'In';
    $data['rows'][] = array
    ( 'id' => $row['Id']
    , 'cell' => array
      ( $row['Id']
      , $row['rack'].$row['column'].'-'.$row['row'].'-'.$row['bay']
      , $row['customer']
      , $dropdown
      , $service
      , $row['department']
      , $row['status']
      , $row['custref']
      , $row['size']
      , $row['authorisation']
      , date('d/m/Y H:m:s',strtotime($row['intake_date']))
      , date('d/m/Y',strtotime($row['destroy_date']))
      )
    )
    ;
}
echo $json->encode($data);

Open in new window

0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39847421
Had the ternary operator the wrong way round in my code - edited for correction :)
0
 

Author Comment

by:peter-cooper
ID: 39847463
@Chris How would I use an elseif with your ternary. Thanks
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39847504
The ternary is simply a shortcut for if /else. If you need ifelse, then write it out in full:

<?php
$status = '';

if ( $row['status']==1 ):
   $status = 'In';
elseif ( $row['status']==2 ):
   $status = 'Out';
else:
   $status = $row['status'];
endif;

Open in new window

If you have more that a couple of conditions to check against, then a switch statement may be easier:

$status = '';

switch ( $row['status'] ):
   case 1:
      $status = 'In';
      break;

   case 2:
      $status = 'Out';
      break;

   case 3:
      $status = 'Something Else';
      break;

   default;
      $status = $row['status'];

endswitch;

Open in new window

0
 

Author Comment

by:peter-cooper
ID: 39847658
Thank you Chris. Just one more question. Suppose I have a column in a grid that contains different values that I need to change such as; 1,2,3 and I need them to become In,Out,Temp what is the best way to code that. With my existing code as you have helped with, it changes a value based on one condition ie; if (i==2){echo "Hello"} and based on 'i' will change the condition accordingly. But what if I want to change all values in a column not based on a condition. so 1 becomes In, 2 become Out etc. Thanks
0
 

Author Comment

by:peter-cooper
ID: 39847684
Sorry chris. My bad. Please ignore last comment as the switch seems to handle all scenarios. Many thanks
0
 

Author Closing Comment

by:peter-cooper
ID: 39847685
Thanks once again
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39847694
Hi Peter,

Not entirely sure I follow. Reading through your original post, it looks like you build each row of your grid from a database loop, creating an array element for each row. If that's the case, then putting the if / switch statement inside the loop will run the condition for each row to come out of your database. Does something like this answer your question:

while ($row = mysql_fetch_assoc($results)) {
   $status = '';
   switch ( $row['status'] ):
      case 1:
         $status = 'In';
         break;

      case 2:
         $status = 'Out';
         break;

      case 3:
         $status = 'Temp';
         break;

      default;
         $status = $row['status'];

   endswitch;
   
   $data['rows'][] = array(
      'id' => $row['Id'],
      'cell' => array(
         $row['Id'],
         $row['rack'].$row['column'].'-'.$row['row'].'-'.$row['bay'],
         $row['customer'],
         $dropdown,
         $service,
         $row['department'],
         $status,
         $row['custref'],
         $row['size'],
         $row['authorisation'],
         date('d/m/Y H:m:s',strtotime($row['intake_date'])),
         date('d/m/Y',strtotime($row['destroy_date']))
      )
   );
}
echo $json->encode($data);

Open in new window

0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39847699
Oops - cross posted. Pleased you got it working :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question