我是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 :(得分:2)
我能看到的两个问题是,首先,Codeigniter Controller被称为CI_Controller而不是C_Controller(至少在你使用的版本中)所以:
class Spoonful extends C_Controller {
应该是
class Spoonful extends CI_Controller {
此外,您实际上并没有将任何数据传递给视图,而是使用视图加载函数的第二个参数来执行此操作:
$this - > data['collectives'] = $collectives;
$this - > load - > view('spoonful');
应该是
$this - > data['collectives'] = $collectives;
$this - > load - > view('spoonful', $this->data);