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

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

here i need to execute the If condition for $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++;
  }

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

(id, country_name, status, added_date)

在 Status 中会有 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)
);

关于php - Drupal 7 使用 If 条件 mysql 数据库选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35627649/

相关文章:

C++ 指针与数组和指针转换

php - 在我的计算机上使用 PHP 从 MySQL InnoDB 数据库 [XAMPP] 获取结果比在其他计算机上花费的时间长得多

mysql - Eloquent :过滤数据透视表和模型表

mysql - Arduino UNO,更新MySQL表中的数据

mysql - 在 View 中使用 `CAST ( 1 AS BIT )`

php - Laravel updateOrCreate 方法——更新你匹配的同一列

c - 如何在 C 中定义/使用 2 维字符串数组(3 维字符?)?

php - 将多维 php 数组插入 mysqli 数据库

php - Laravel 5 Auth Post Submit - VerifyCsrfToken.php 第 46 行中的 TokenMismatchException

php - Zend PDF 内存管理