【问题标题】:print more than 1 row, Codeigniter打印超过 1 行,Codeigniter
【发布时间】:2015-07-15 05:24:45
【问题描述】:

我想回显数组,来自同一个表但不同的 id,来自 db,Codeigniter 中超过 1 行。

型号-----------------

public function DeviceRowInfo($mobno)
{

  $this->db->select('*');
  //$this->db->where("mobile_no", $mobno);
  $this->db->where("profile_id", $profile_id);
  $pid_device_count_query = $this->db->get("ptt_device");
  if ($pid_device_count_query->num_rows() > 0)
  {
     //foreach ($pid_device_count_query->result_array() as $count_rows)
    // {
        return $pid_device_count_query->row_array();
        echo $row['device_identifier'];
        echo $row['device_os_version'];
    //  }
  }
  else
  {
     return 0;
  }

}

控制器---------------------------------------------- -----

public function ViewDevices()
{

     $data['user_device_row']          = $this->security_model->DeviceRowInfo($mobno);
     $data['user_profile_row']         = $this->security`enter code here`_model->ProfileRowInfo($mobno);
     $data['device_count']              = $this->security_model->DeviceCountInfo($mobno);


}

查看---------------------------------------------- --------------------

foreach( $user_device_row as $devi_row ) 
        {

            ?>

        <table class="table table-bordered text-center">
    <tbody>
  <tr>
    <td>Device Count</td>
    <td><?php echo $device_count; ?></td>
          </tr>
  <tr>
    <td>IMEI</td>
    <td><?php echo $devi_row[0]['device_identifier']; ?></td>

  </tr>
  <tr>
    <td>OS Versions</td>
    <td><?php echo $devi_row[0]['device_os_version']; ?></td>

  </tr>
  <tr>
    <td>Register On</td>
    <td><?php  foreach($user_profile_row as $devi_pro_row)
               {

               // set default timezone
               date_default_timezone_set('Asia/Bangkok');

              // timestamp
               $timestamp = $devi_pro_row[0]['registerd_on'];

              // output
               echo date('d/m/Y',$timestamp);

               } ?></td>

  </tr>
  <tr>
    <td>Last Logged in</td>
    <td><?php echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"//$devi_row['device_identifier']; ?></td>

  </tr>
  <tr>
    <td>Active Device</td>
    <td><?php echo  "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"//$devi_row['device_identifier']; ?></td>

  </tr>
            </tbody>

【问题讨论】:

  • 再描述一点
  • 我尝试从数据库表中打印数据,该数据在不同的行中具有相同的关键字,因此我想将它们的每一行打印到我的视图页面。所以在我的视图页面中将显示表取决于具有相同搜索关键字的行。

标签: mysql codeigniter


【解决方案1】:

//型号代码

public function DeviceRowInfo($mobno)
{

  $this->db->select('*');
  $this->db->where("mobile_no", $mobno);
  $this->db->where("profile_id", $profile_id);
  $pid_device_count_query = $this->db->get("ptt_device");
  if ($pid_device_count_query->num_rows() > 0)
  {
      return $pid_device_count_query->result_array();
  }
  else
  {
      return false;
   }

}

//控制器代码

public function ViewDevices()
{

 $data['user_device_row']          =       $this->security_model->DeviceRowInfo($mobno);
 $data['user_profile_row']         = $this->security`enter code here`_model->ProfileRowInfo($mobno);
 $data['device_count']              = $this->security_model->DeviceCountInfo($mobno);

 //Load your view here

$this->load->view('your_view_name',$data);

}

//查看代码

foreach( $user_device_row as $devi_row ) 
{ ?>

<table class="table table-bordered text-center">
<tbody>
<tr>
<td>Device Count</td>
<td><?php echo $device_count; ?></td>
      </tr>
<tr>
 <td>IMEI</td>
<td><?php echo $devi_row['device_identifier']; ?></td>

<?php } ?>

希望这对你有用

【讨论】:

  • 使用后出现此错误,消息:非法字符串偏移。我已经读过,因为它没有将我的查询视为数组。我在哪里将它设置为数组,在控制器或模型上或做任何事情来修复它?。
  • 在控制器中使用return $pid_device_count_query-&gt;result_array(); instread of return $pid_device_count_query-&gt;row_array();
  • 您的代码有帮助,谢谢。抱歉最近告诉你。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
  • 2015-08-20
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多