简体   繁体   English

PHP(codeigniter)-如何检查接下来的两行是否有值?

[英]PHP (codeigniter) - How to check if the next two rows have value?

I wanted to check if the next TWO rows or ID is not NULL . 我想检查接下来的行或ID是否不为NULL

because if there is no succeeding TWO rows or ID then the income remains zero. 因为如果没有连续的行或ID,则收入保持为零。

        foreach ($data as $row)
        {
          if(($row->id + 2) != NULL) //I don't know what is the correct statement here
          {
            echo "<tr>";
            echo "<td>".$row->id."</td>"; 
            echo "<td>".$row->username."</td>"; 
            echo "<td>"."650.00"."</td>"; 
            echo "<td>".$row->remarks."</td>"; 
            echo "<tr>";
          }
          else
          {
            echo "<tr>";
            echo "<td>".$row->id."</td>"; 
            echo "<td>".$row->username."</td>"; 
            echo "<td>".$row->income."</td>"; 
            echo "<td>".$row->remarks."</td>"; 
            echo "<tr>"; 
          }                  
        }

Here is the table I want to achieve. 这是我要实现的表。

==================================
|ID | Username | Income | Remarks|
| 2 | user1    | 650.00 |        |
| 3 | user2    | 650.00 |        |
| 4 | user3    |   0.00 |        |
| 5 | user4    |   0.00 |        |
==================================

If I add a username then the next output will be this: 如果我添加一个用户名,那么下一个输出将是:

==================================
|ID | Username | Income | Remarks|
| 2 | user1    | 650.00 |        |
| 3 | user2    | 650.00 |        |
| 4 | user3    | 650.00 |        |
| 5 | user4    |   0.00 |        |
| 6 | user5    |   0.00 |        |
==================================

You need to change your foreach to get the index. 您需要更改foreach以获得索引。

    foreach ($data as $index => $row)

Then you can address all rows relative to your current row with: 然后,您可以使用以下方法寻址相对于当前行的所有行:

    $row_two_ahead = $data[$index + 2];

you should check however if that row exists before you try to use it or you will get index out of range exceptions: 但是,在尝试使用该行之前,应检查该行是否存在,否则将获得索引超出范围的异常:

    if (isset($data[$index + 2])) {
    }

I solved the problem... 我解决了这个问题

this is the code from my controller: 这是我的控制器的代码:

public function addUsername()
{
  $data['id'] = NULL;
  $data['username'] = $this->input->post('username');
  $data['reward'] = 0.00;
  $data['remarks'] = ' ';

  $this->Matrix_model->addUsername($data);
  $last = $this->Matrix_model->getLast();
  $index = $last - 2; //I minus the last index with two
  $this->Matrix_model->updateIncome($index); //and updated the income of that index
  redirect('http://localhost/matrix/index.php/matrix');
}

and this is the code from my model: 这是我模型中的代码:

public function addUsername($data)
   {
    $this->db->insert("income", $data);
   }

public function getLast()
  {
    return $this->db->insert_id(); //this is the method to access the last id inserted
  }

public function updateIncome($id)
  {
     $this->db->set("reward", 650);
     $this->db->where("id", $id); 
     $this->db->update("income"); 
  }

thank you and I'm sorry if other programmers didn't understand my questions 谢谢,如果其他程序员不理解我的问题,我很抱歉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM