如何在joomla模块中检索和显示数据的结果集。

时间:2014-02-21 14:15:50

标签: php joomla foreach while-loop jinput

这是我的代码。

我想显示名字姓氏电话号码,但我的代码只显示1个号码

我想通过forwhile从数据库中获取并显示一系列数字。

mod_phonebook.php

<?php
defined( '_JEXEC' ) or die('Restricted access');
$doc = JFactory::getDocument();
$doc->addStyleSheet(JURI::root().'modules/mod_phonebook/css/main.css');
require_once(dirname(__FILE__).DS.'helper.php');

if(isset($_POST['search'])){

    $jinput = JFactory::getApplication()->input;
    $name = $jinput->get('name','','STRING');
    $lname = $jinput->get('lname','','STRING');
    $tell = $jinput->get('tell','','INT');
    if(myphonebook::searchdata($name,$lname,$tell)) 
    {
        echo "<p class='lbl'>$name:نتایج جستجو شامل</p>";
        echo "<p class='lbl'>شماره پیدا شده=".myphonebook::searchdata($name,$lname,$tell).'</p><br>';
    }
    else
    {
        echo "شماره ای به این نام پیدا نشد";    
    }

}
else
{
    require(JMOduleHelper::getLayoutPath('mod_phonebook')); 
}

?>

helper.php

<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

class myphonebook{
   public static function searchdata($name,$lname,$tell){

       $db = JFactory::getDBO();
       $query = "SELECT * from `phonebook` WHERE `name` ='$name' or  `Family`='$lname'  or  `Numberofroom`='$tell'";
       $db->setQuery($query);
       $results = $db->loadObjectList();
       if($db->query())
       {                
           foreach($results as $row){
               $tel = $row->Telephone;  
               return $tel;
               $counter++;
           }
       }    
       else
       {
           return false;
       }

   }
}
?>

1 个答案:

答案 0 :(得分:0)

像这样更改此代码,因为foreach中的返回将在第一轮返回

 foreach($results as $row){
                   $tel[$counter] = $row->Telephone;  

                   $counter++;
               }
 return $tel;