CI中的MySQL PHP查询

时间:2018-11-07 00:55:53

标签: php mysql codeigniter

我需要帮助。我必须将PHP MySQL函数传递给CodeIgniter,但它不会向我显示数据,它会在 if(array_key_exists($ pos,$ rs))

中停止

查询很好,但不输入条件是否

有解决方案吗?

这是我在CodeIgniter中的代码

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="row">
  <div class="col-sm-6">
    <div class="card">
      <div class="card-body">
        <div>

          <h5 class="card-title">Title</h5>
          <p class="card-text">"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."</p>
          <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
        <div>

          <img src="https://picsum.photos/100/100" alt="sans" />
        </div>
      </div>
    </div>
  </div>
</div>


<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

原始MySQL代码

public function table($hour, $row)
    {
        global $rs;
        if ($rs === null) 
        {
          $this->db->select("CONCAT(t.tbl_row, '_', t.tbl_col) as pos, t.tbl_id, t.sub_id, s.sub_name", false);
          $this->db->join('redips_timetable AS t', 't.sub_id = s.sub_id');
          $rs = $this->db->get('redips_subject AS s')->result(); 
        }
        echo '<tr>';
        echo '<td class="mark dark">' . $hour . '</td>';
        for ($col = 1; $col <= 5; $col++) 
        {
          echo '<td>';
          $pos = $row . '_' . $col;
          if(array_key_exists($pos, $rs)) 
          {
            $elements = $rs[$pos];
            $id = $elements['sub_id'] . 'b' . $elements['tbl_id'];
            $name = $elements['sub_name'];
            $class = substr($id, 0, 2); 
            echo "<div id=\"$id\" class=\"redips-drag $class\">$name</div>";
          }
          echo '</td>';
        }
        echo "</tr>\n";
      }

1 个答案:

答案 0 :(得分:0)

/ * 如果需要查找数组键

您在$ rs变量中有对象 ($ rs = $ this-> db-> get('redips_subject AS s')-> result();) 所以需要将obj转换为每个循环中的数组 * /

public function table($hour, $row)
{
    global $rs;
    if ($rs === null) 
    {
      $this->db->select("CONCAT(t.tbl_row, '_', t.tbl_col) as pos, t.tbl_id, t.sub_id, s.sub_name", false);
      $this->db->join('redips_timetable AS t', 't.sub_id = s.sub_id');
      $rs = $this->db->get('redips_subject AS s')->result(); 
    }
    echo '<tr>';
    echo '<td class="mark dark">' . $hour . '</td>';
    for ($col = 1; $col <= 5; $col++) 
    {
    $arr = get_object_vars($rs[$col]);
      echo '<td>';
      $pos = $row . '_' . $col;
      if(array_key_exists($pos, $arr))
      {
        $elements = $arr[$pos];
        $id = $elements['sub_id'] . 'b' . $elements['tbl_id'];
        $name = $elements['sub_name'];
        $class = substr($id, 0, 2); 
        echo "<div id=\"$id\" class=\"redips-drag $class\">$name</div>";
      }
      echo '</td>';
    }
    echo "</tr>\n";
  }