peter-cooper
asked on
Check $row for value and amend
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
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);
You can read Ray's EE article here on switching the mySQL library:
http://www.experts-exchang e.com/Web_ Developmen t/Web_Lang uages-Stan dards/PHP/ A_11177-PH P-MySQL-De precated-a s-of-PHP-5 -5-0.html
http://www.experts-exchang
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.
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);
Had the ternary operator the wrong way round in my code - edited for correction :)
ASKER
@Chris How would I use an elseif with your ternary. Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
ASKER
Sorry chris. My bad. Please ignore last comment as the switch seems to handle all scenarios. Many thanks
ASKER
Thanks once again
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:
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);
Oops - cross posted. Pleased you got it working :)
Open in new window
FYI, mysql_* is deprecated in PHP - you should switch to using mysqli or PDO ;)