2014-01-08 14 views
1

Hello iam new to joomla創建模塊mod_testimonial。我這個已經把數據放到$rows = $db->loadObjectList();從輔助文件,注意:未定義的偏移:第37行的default.php中的2注意:嘗試在第37行的default.php中獲取非對象的屬性文件未找到

和回聲後$行的數據是

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; 
    } 

.Till這裏所有的OKK,但如果我將會取消或後端unfeatured數據會引發錯誤這樣

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

檢查數組有值,則只能訪問其索引值 –

+0

該是感謝,但如何獲得該數組在一個循環中,以便獲得連續性並顯示可用的數據。我已經嘗試了每個循環,但沒有變化,完全空白,請幫助我。 –

+0

可能重複[參考 - 這是什麼錯誤在PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Sumurai8

回答

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

希望它有助於..

+0

非常感謝,非常感謝你。 –

+0

很高興聽到它幫助你解決 –

相關問題