代码之家  ›  专栏  ›  技术社区  ›  Marcus

如何使用php&codeigner编写此语句

  •  2
  • Marcus  · 技术社区  · 15 年前

    我想知道如何在我的第一个ci应用程序(也是仅有的第二个php应用程序)中编写一个语句,但我陷入了困境。

    我有一个连接表,可以保存各自表中的book_id和user_id。我想知道是谁读了一本书,然后把它回音出来,但我需要格式来解释。以下是查询:

    $readQuery = $this->db->get_where('books_users', array('book_id' => $isbn));
    

    这是我对一个人的逻辑

    // Get my user info
    $user = $this->ion_auth->get_user();
    // Tell me who has read it.
    $this->db->select('user_id');
    $readQuery = $this->db->get_where('books_users', array('book_id' => $isbn));
    // If only one person has read it
    if ($readQuery->num_rows() == 1) {
        $readResult = $readQuery->row();
        // And if that person was me...
        if ($readResult->user_id == $user->id) {
            $message = 'You have read this.';
        // If it was someone else...
        } else {
            $reader = $this->ion_auth->get_user($readResult->user_id);
            $message = "$reader->first_name $reader->last_name has read this";
        }
    // If two people have read it
    }
    

    所以如果只有一个人读过我就好了。但是,当两个人读过它时,我想让它说“名字一和名字二读过这个。”如果登录的人没有读过它。如果他们是这两个人中的一个,应该说“你和名字2读过这个”。

    以此类推,3-5。如果有3-5个人读过,选项将是“名字1,名字2,名字3读过这个”或“你,名字2,名字3读过这个”。最多5个人

    如果它是6个或更多,它应该只说其中两个的名字,即“你,名字2,和其他5个人读过这个。”

    我考虑为每个实例做一个条件,然后将所有用户推送到一个数组中。然后我可以在_Array()中查看登录用户是否读过它并尝试返回报告,但我只是在处理逻辑时遇到了一些问题。

    有更简单的方法吗?

    事先非常感谢, 马库斯

    4 回复  |  直到 15 年前
        1
  •  2
  •   methodin    15 年前
    $users = array();
    while($row)
    {
      if(currentuser)
      {
        array_unshift($users,'You')
      }
      else
      {
        $users[] = row[name]
      }
    }
    
    $count = $max = count($users);
    if($max >= 6)
    {
      $max = 2;
    }
    
    $str = '';
    for($i=0;$i<$max;$i++)
    {
      $str .= ($str == '' ? '' : ($i == $count-1 && $count < 6 ? ' And ' : ', ') ) . $users[$i];
    }
    
    if($count >= 6)
    {
      $str .= ' and '.$count-2.' others'
    }
    

    这应该能帮到你。它几乎是伪代码,但逻辑是实现你想要的(我相信)。显然,如果只显示一些用户,那么您不希望获取所有行,但这很容易实现。

        2
  •  3
  •   Hibiscus    15 年前

    我想说使用一个带有范围的switch语句。你需要做一些杂技来处理是否包括“你”的问题。下面的代码没有经过测试,但给出了一般的想法。

    $message = "";
    $readers = array();
    $me_included = 0; // Counter increment if you're included in the reader list
    
    $this->db->select('user_id');
    $readQuery = $this->db->get_where('books_users', array('book_id' => $isbn));      
    foreach ($readQuery->result() as $row) {
      if ($row->user_id == $user->id) {
        $message = "You";
        $me_included = 1;
      } else {
        $readers[] = $this->ion_auth->get_user($row->user_id);
      }
    }
    
    $reader_count = $sizeof($readers) + $me_included;
    switch(TRUE) 
    {
      // One reader, not you
      case( ($reader_count == 1) && ($me_included == 0) ):
        $message .= $readers[0]->first_name . " " . $readers[0]->last_name . " has read this.";
        break;
      // Only you
      case( ($reader_count == 1) && ($me_included == 1) ):
        $message .= " have read this.";
        break;
      // Two readers
      case( ($reader_count == 2) ):
        for ($i = 0; $i <= sizeof($readers); $i++) {
          if ($i == sizeof($readers)) {
            $message .= " and ";
          }
          $message .= $readers[i]->first_name . " " . $readers[i]->last_name;
        }
        $message .= " have read this.";
        break;
      case( ($reader_count > 3) && ($reader_count < 6) ):
        if ($me_included) {
          $message .= ", ";
        }
        for ($i = 0; $i <= sizeof($readers); $i++) {
          if ($i == sizeof($readers)) {
            $message .= " and ";
          }
          $message .= $readers[i]->first_name . " " . $readers[i]->last_name;
          if ($i != sizeof($readers)) {
            $message .= ", ";
          } else {
            $message .= " have read this.";
          }
        }
        break;
      case( ($reader_count > 6) ):
        if ($me_included) {
          $message .= ", " . $readers[0]->first_name . " " . $readers[0]->last_name . " and " . ($reader_count - 2) . " others have read this.";
        } else {
        for ($i = 0; $i <= 1; $i++) {
          $message .= $readers[0]->first_name . " " . $readers[0]->last_name . ", " . $readers[1]->first_name . " " . $readers[1]->last_name . " and " . ($reader_count - 2) . " others have read this.";
        }
        break;
      }
    
        3
  •  1
  •   Mitchell McKenna    15 年前

    实际上有几个地方我们可以优化这个。

    1)在初始查询中,使用 用户 表,这样就不必为每个名称重复查询多次:

    $readers = $this->db->select('id', 'first_name', 'last_name')->from('books_users')->join('users', 'books_users.id =users.id', 'left')->where('book_id' => $isbn);
    

    2)另外,我们可以避免遍历整个(可能很大)结果集,以查看 通过单独查询阅读:

    $you_read = $this->db->get_where('books_users', array('book_id' => $isbn, 'user_id' => $user->id));
    
        4
  •  0
  •   Marcus    15 年前

    为了完整起见,这里是我使用的最后一个代码。希望它对某人有用。

        // Find out who has read this book
        // Get the users
        $current_user = $this->ion_auth->get_user();
        $users = $this->ion_auth->get_users();
        // Get the readers
        $this->db->select('user_id');
        $query = $this->db->get_where('books_users', array('book_id' => $isbn));
        // If there were results
        $readers = array();
        if ($query->num_rows() > 0) {
            foreach ($users as $user) {
                if ($current_user->id == $user->id) {
                    array_unshift($readers, 'You');
                } else {
                    $readers[] = $user->first_name . ' ' . $user->last_name;
                }
                $count = $max = count($readers);
                if ($max >= 6) {
                    $max = 2;
                }
                $message = '';
                for ($i = 0; $i < $max; $i++) {
                    $message .= ($message == '' ? '' : ($i == ($count - 1) && ($count < 6) ? ' and ' : ', ')) . $readers[$i];
                }
                if ($count >= 6) {
                    $message .= ' and ' . ($count - 2) . ' others';
                }
                $message .= ($count == 1 && !$current_user->id ? ' has ' : ' have ') . 'read this.';
            }
        } else {
            $message = '<a href="' . base_url() . 'books/' . $bookResult->filename . '">Be the first to read this.</a>';
        }
    
    推荐文章