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

时间:2016-02-25 12:41:44

标签: php mysql arrays drupal drupal-7

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, ----> **
  

这里我需要为$status

执行If条件
    //$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

1 个答案:

答案 0 :(得分:0)

我建议使用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)
);