Drupal 7 使用 If 条件 mysql 数据库选择查询


Drupal 7 Select query with If condition mysql database

function tablesort_example_page() {
//$date2=date('m-d-Y', strtotime('t.added_date'));
// if('t.status'==1){
// $status='Active';
// }else{
// $status='Inactive';
// }
  // We are going to output the results in a table with a nice header.
  $header = array(
    // The header gives the table the information it needs in order to make
    // the query calls for ordering. TableSort uses the field information
    // to know what database column to sort by.
    array('data' => t('S No'), 'field' => 't.id'),
    array('data' => t('Country Name'), 'field' => 't.country_name'),
    array('data' => t('Status'), 'field' => 't.status'),
    array('data' => t('Added Date'), 'field' => 't.added_date'),
    array('data' => t('Action'), 'field' => 't.id',),
    array('data' => t('Action'), '',),
  );
  // Using the TableSort Extender is what tells the the query object that we
  // are sorting.
  $limit = 10;

  $query = db_select('countries', 't')->extend('TableSort')->extend('PagerDefault')->limit($limit)->orderby('id', ASC);
  $query->fields('t');
  // Don't forget to tell the query object how to find the header information.
  $result = $query
      ->orderByHeader($header)
      ->execute(); 
if('$row->status'==0){
$status='Active';
} else {
$status='Inactive';
}
  $rows = array();
  $i=1;
  foreach ($result as $row) {
//print_r($row);

    // Normally we would add some nice formatting to our rows
    // but for our purpose we are simply going to add our row
    // to the array.
    $rows[] = array(
    $row->id,
    $row->country_name,
    $status, ----> **

在这里我需要执行 If 条件以$status

    //$row->added_date,
    date('d-m-Y H:i:s', strtotime($row->added_date)),
    l('edit', 'mypages/countries/'. $row->id),
    l('delete', 'mypages/delete/'. $row->country_name)
    );
    $i++;
  }

根据我的数据库表,以下是我的表字段。

idcountry_namestatusadded_date

在"状态"中,将有01 现在我的问题是我需要显示状态

if  0 - Inactive
    1 - Active

我建议使用 PHP 三元运算符来测试状态字段中的值,并根据该值输出字符串描述。

$rows[] = array(
  $row->id,
  $row->country_name,
  ($row->status == 0) ? 'Inactive' : 'Active',
  date('d-m-Y H:i:s', strtotime($row->added_date)),
  l('edit', 'mypages/countries/'. $row->id),
  l('delete', 'mypages/delete/'. $row->country_name)
);