2011-05-02 61 views
0

我是新來的PHP,我想了解我的網站是如何與數據庫進行交互的基礎知識。有人可以快速查看並告訴我他們是否看到下面的代碼有問題?幫助與PHP控制器功能

這裏的控制器

class Spoonful extends C_Controller { 


    public function index() { 

     $collectives = $this - > get_mailchimp_collectives(); 
     $this - > data['collectives'] = $collectives; 

     $this - > load - > view('spoonful'); 

    } 

    private function get_mailchimp_collectives() { 

     $constraints['select'] = 'collective.*, member.username, image.path AS avatar'; 

     $constraints['join'] = array(
     array('table' = > 'member', 'on' = > 'member.member_id=collective.member_id'), array('table' = > 'collective_tag', 'on' = > 'collective_tag.collective_id=collective.collective_id'), array('table' = > 'tag', 'on' = > 'tag.tag_id=collective_tag.tag_id'), array('table' = > 'image', 'on' = > 'image.image_id=member.avatar_id')); 

     $constraints['where'] = array('tag.value' = > 'mailchimp'); 
     $constraints['order_by'] = array('collective.added' = > 'DESC'); 
     $constraints['limit'] = array('lower' = > 3); 

     $collectives = $this - > db - > get('collective', $constraints); 

     if (!empty($collectives['result_set'])) return $collectives['result_set']; 


     return NULL; 
    } 



} 

功能和這裏的視圖的片段:

<!-- START COLLECTIVES --> 
<?php if (!empty($collectives)){?> 
    <?php foreach ($collectives AS $collective){?> 
    <tr> 
     <td bgcolor="#FFFFFF"> 

      <table cellpadding="0" cellspacing="0"> 
       <tr> 
        <td colspan="9" style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/qc_table_top.gif"); ?>" width="566" height="17"></td> 
       </tr> 
       <tr> 
        <td width="1" bgcolor="#D9D9D9" style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/none.gif"); ?>" width="1" height="1"></td> 
        <td width="13" style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/none.gif");?>" width="13" height="1"></td> 
        <td width="66" valign="top"> 
         <table cellpadding="0" cellspacing="0"> 
          <tr> 
           <td colspan="3" style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/avatar_top.gif"); ?>" width="66" height="5"></td> 
          </tr> 
          <tr> 
           <td style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/avatar_right.gif"); ?>" width="5" height="45"></td> 
           <td style="font-size:0;line-height:0;"><a href="<?php echo site_url("people/{$collective['username']}"); ?>" target="_blank"><img src="<?php echo $collective['avatar']; ?>" width="46" height="45" border="0"></a></td> 
           <td style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/avatar_spike.gif"); ?>" width="15" height="45"></td> 
          </tr> 
          <tr> 
           <td colspan="3" style="font-size;0;line-height:0;"><img src="<?php echo site_url("images/spoonful/avatar_bottom.gif"); ?>" width="66" height="5"></td> 
          </tr> 
         </table>             </td> 
        <td width="15" style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/none.gif"); ?>" width="15" height="1"></td> 
        <td width="338" valign="top"> 

         <a href="<?php echo site_url("question/{$collective['collective_id']}/{$this->utilities->get_url_title($collective['title'])}"); ?>" style="text-decoration:none;"><font style="font-size:15px;line-height:18px;font-weight:bold;" color="#242424" face="Arial, Helvetica, sans-serif"><?php echo $collective['title']; ?></font></a><br> 
         <font style="font-size:11px;" color="#A5A5A5" face="Arial, Helvetica, sans-serif">by <a style="text-decoration:none;" href="<?php echo site_url("people/{$collective['username']}"); ?>"><font color="#2294B8"><?php echo $collective['username']; ?></font></a></font>             </td> 
        <td width="30" style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/none.gif"); ?>" width="30" height="1"></td> 
        <td width="81" valign="top"> 
         <table cellpadding="0" cellspacing="0"> 
          <tr> 
           <td style="font-size:0;line-height:0;"><a href="<?php echo site_url("question/{$collective['collective_id']}/{$this->utilities->get_url_title($collective['title'])}");?>"><img src="<?php echo site_url("images/spoonful/answer.gif"); ?>" width="81" height="27" border="0" alt="ANSWER"></a></td> 
          </tr> 
          <tr> 
           <td style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/none.gif"); ?>" width="1" height="8"></td> 
          </tr> 
          <tr> 
           <td width="81" align="center" style="text-align:center;"><a style="text-decoration:none;" href="<?php echo site_url("question/{$collective['collective_id']}/{$this->utilities->get_url_title($collective['title'])}");?>"><font style="font-size:11px;" color="#8E8E8E" face="Arial, Helvetica, sans-serif">View Answers</font></a></td> 
          </tr> 
         </table>             </td> 
        <td width="21" style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/none.gif");?>" width="21" height="1"></td> 
        <td width="1" bgcolor="#D9D9D9" style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/none.gif"); ?>" width="1" height="1"></td>  
       </tr> 
       <tr> 
        <td colspan="9" style="font-size:0;line-height:0;"><img src="<?php echo site_url("images/spoonful/qc_table_bottom.gif"); ?>" width="566" height="21"></td> 
       </tr> 
      </table>          </td> 
    </tr> 
    <tr> 
     <td style="font-size=0;line-height:0;"><img src="<?php echo site_url("images/spoonful/none.gif"); ?>" width="1" height="9"></td> 
    </tr> 
    <?php }?> 
<?php }?> 

但出於某種原因,我只是不能得到它加載。如果我註釋掉這些行:

if (!empty($collectives['result_set'])) 
    return $collectives['result_set']; 

然後視圖加載沒有數據,但是當我離開它時,我得到一個空白頁。會喜歡一些建議。

非常感謝!

+0

確保錯誤報告已啓用,所以你可以看到,PHP拋出任何錯誤 – JohnP 2011-05-02 05:53:06

回答

2

你兩個問題,我可以看到的是,首先是所有的笨控制器被稱爲是CI_Controller不C_Controller(在版本,您正在使用至少)這樣:

class Spoonful extends C_Controller { 

應該

$this - > data['collectives'] = $collectives; 

    $this - > load - > view('spoonful'); 
class Spoonful extends CI_Controller { 

另外,你實際上並沒有傳遞任何數據來看,您認爲負載函數的第二個參數,從而做到

應該

$this - > data['collectives'] = $collectives; 

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