2011-05-02 98 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);