Hello iam是joomla的新手,创建了一个模块mod_testimonial。我已将数据从帮助文件
放入$rows = $db->loadObjectList();
和echo之后$行中的数据是
Array
(
[0] => stdClass Object
(
[name] => james
[testimonial] => Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
[regdate] => 2013-12-31 13:24:29
[id] => 37
)
[1] => stdClass Object
(
[name] => Tom
[testimonial] => Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
[regdate] => 2013-12-31 14:45:56
[id] => 38
)
[2] => stdClass Object
(
[name] => Alyson
[testimonial] => is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
[regdate] => 2014-01-03 14:52:09
[id] => 42
)
)
在我的default.php文件中,我从数组中获取了数据。
<p><?php print_r($rows[0]->testimonial);?><span class="testimonial-by"> —<?php print_r($rows[0]->name);?>,<?php echo date('M Y',strtotime($rows[1]->regdate)); ?>,</span></p>
<p><?php print_r($rows[1]->testimonial);?><span class="testimonial-by">—<?php print_r($rows[1]->name);?>,<?php echo date('M Y',strtotime($rows[2]->regdate)); ?>,</span></p>
<p><?php print_r($rows[2]->testimonial);?><span class="testimonial-by">—<?php print_r($rows[2]->name);?>,<?php echo date('M Y',strtotime($rows[0]->regdate)); ?>,</span></p>
我的帮助文件是
static function getRecord($params)
{
$db = JFactory::getDBO();
$query = "SELECT name,testimonial,regdate,id FROM #__testimonial WHERE published = '1' AND featured='1' ";
$db->setQuery( $query);
$rows = $db->loadObjectList();
return $rows;
}
。在这里确定一切都好,但是如果我要从后端取消发布或取消数据,则会产生这样的错误
Notice: Undefined offset: 2 in modules\mod_testimonial\tmpl\default.php on line 37
Notice: Trying to get property of non-object in modules\mod_testimonial\tmpl\default.php on line 37
File not found
我知道这是因为没有
offset [2] => stdClass Object
(
[name] => Alyson
[testimonial] => is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
[regdate] => 2014-01-03 14:52:09
[id] => 42
)
我想提供一个正确的循环,以便此问题跳过,我不需要更改我的模板。
请非常沮丧地帮助我尝试所有的东西。 谢谢你
答案 0 :(得分:1)
试试这个,
在您的帮助文件上。
function getRecord($params)
{
$db = JFactory::getDBO();
$query = "SELECT name,testimonial,regdate,id FROM #__testimonial WHERE published = '1' AND featured='1' ";
$db->setQuery( $query);
return $db->loadObjectList();
}
在您的模块文件mod_testimonial.php
中有类似
$data = helperClassname :: getRecord($param);
在你的default.php
上有类似的内容。
if(sizeof($data) > 0){
foreach($data as $key=>$value){
echo "<br/>Name".$value->name;
}
}
有关模块结构和调用的更多信息,请查看此sample banner slide show module
希望它的帮助..