Thinkphp3.2的模板中同一個foreach中要用到不同表的資料,應該怎樣處理?

來源:互聯網
上載者:User

看到一份Thinkphp做微博開發的代碼,但有些地方沒有看懂:
。控制器中的代碼:

/*
    • 首頁視圖
      */

    1. function Index(){

             //p(S('usermsg'.session('uid')));       //replace_weibo('adsf');       //p(C('FILTER'));       $db=D('Weibo');              //取得目前使用者的ID與目前使用者 所有關注好友的ID              $uid = array(session('uid'));//????       $where=array('fans'=>session('uid'));              if (isset($_GET['gid'])){           $gid = I('gid','','intval');           $where['gid']=$gid;           $uid = '';       }              $result =M('follow')->where($where)->field('follow')->select();       if($result){           foreach ($result as $v){               $uid[] = $v['follow'];           }       }              //組合WHERE條件,條件為目前使用者自身的ID與目前使用者所關注好友的ID       $where = array('uid'=>array('IN',$uid));//where條件可以是二維數組嗎?              //統計資料總條數,用於分頁       $count  = $db->where($where)->count();// 查詢滿足要求的 總記錄數         $Page       = new \Think\Page($count,20);// 執行個體化分頁類 傳入總記錄數和每頁顯示的記錄數(20)       // 進行分頁資料查詢 注意limit方法的參數要使用Page類的屬性       $limit=$Page->firstRow.','.$Page->listRows;       $Page->setConfig('theme',"共 %TOTAL_ROW% 條記錄 %FIRST% %UP_PAGE% %NOW_PAGE% / %TOTAL_PAGE% %DOWN_PAGE% %END% ");       $Page->setConfig('prev','上一頁');       $Page->setConfig('next','下一頁');              //讀取所有微博       $result= $db->getAll($where,$limit);//getAll()方法是驅動拓展裡的       //p($result);       $this->page= $Page->show();// 分頁顯示輸出       $this -> weibo = $result;       $this->display();          }   

      。視圖中的代碼:

                                                                                                                            __ROOT__/Uploads/Face/{$v.face}                                                    __PUBLIC__/Images/noface.gif                        " width='50' height='50'/>                                                

    》》》》》
    重點在這裡:使用者的頭像$V.face 不知是來自哪裡的。
    weibo這個表中並沒有face這一列,也沒看到代碼中去讀取了另一個表中的資訊,
    它的weibo表結構是這樣的:

    頭像資訊可能是來自這個表:

    但首頁這個表中也只有face50 face80 face180 而沒有face這一列。

    想請問它是怎樣把不同表的資料放到同一個foreach中處理的?

    原代碼來自:https://github.com/milkbobo/Thinkphp3.2.3-weibo

    回複內容:

    看到一份Thinkphp做微博開發的代碼,但有些地方沒有看懂:
    。控制器中的代碼:

    /*
    • 首頁視圖
      */

    1. function Index(){

             //p(S('usermsg'.session('uid')));       //replace_weibo('adsf');       //p(C('FILTER'));       $db=D('Weibo');              //取得目前使用者的ID與目前使用者 所有關注好友的ID              $uid = array(session('uid'));//????       $where=array('fans'=>session('uid'));              if (isset($_GET['gid'])){           $gid = I('gid','','intval');           $where['gid']=$gid;           $uid = '';       }              $result =M('follow')->where($where)->field('follow')->select();       if($result){           foreach ($result as $v){               $uid[] = $v['follow'];           }       }              //組合WHERE條件,條件為目前使用者自身的ID與目前使用者所關注好友的ID       $where = array('uid'=>array('IN',$uid));//where條件可以是二維數組嗎?              //統計資料總條數,用於分頁       $count  = $db->where($where)->count();// 查詢滿足要求的 總記錄數         $Page       = new \Think\Page($count,20);// 執行個體化分頁類 傳入總記錄數和每頁顯示的記錄數(20)       // 進行分頁資料查詢 注意limit方法的參數要使用Page類的屬性       $limit=$Page->firstRow.','.$Page->listRows;       $Page->setConfig('theme',"共 %TOTAL_ROW% 條記錄 %FIRST% %UP_PAGE% %NOW_PAGE% / %TOTAL_PAGE% %DOWN_PAGE% %END% ");       $Page->setConfig('prev','上一頁');       $Page->setConfig('next','下一頁');              //讀取所有微博       $result= $db->getAll($where,$limit);//getAll()方法是驅動拓展裡的       //p($result);       $this->page= $Page->show();// 分頁顯示輸出       $this -> weibo = $result;       $this->display();          }   

      。視圖中的代碼:

                                                                                                                            __ROOT__/Uploads/Face/{$v.face}                                                    __PUBLIC__/Images/noface.gif                        " width='50' height='50'/>                                                

    》》》》》
    重點在這裡:使用者的頭像$V.face 不知是來自哪裡的。
    weibo這個表中並沒有face這一列,也沒看到代碼中去讀取了另一個表中的資訊,
    它的weibo表結構是這樣的:

    頭像資訊可能是來自這個表:

    但首頁這個表中也只有face50 face80 face180 而沒有face這一列。

    想請問它是怎樣把不同表的資料放到同一個foreach中處理的?

    原代碼來自:https://github.com/milkbobo/Thinkphp3.2.3-weibo

    Index/Home/Model/CommentModel.class.php

    10 'id','content','time','wid',//'uid',
    11 '_type'=>'LEFT'
    12 ),
    13 'userinfo'=>array(
    14 'username','face50'=>'face','uid',
    15 '_on'=>'comment.uid = userinfo.uid'
    16 ),

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.