1

This is my code .

I want to display the firstname, lastname and phone number, but my code is only displaying 1 number

I want get and show a series of numbers from the database with for and while.

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

   }
}
?>
kguest
  • 3,804
  • 3
  • 29
  • 31
  • Your database query is using things like `WHERE name ='$name'` so maybe only 1 name is matching, hence why only 1 number is being displayed. You should also use up to date coding standards for your database query and dont use `$_POST` – Lodder Feb 21 '14 at 15:28
  • what you will be getting on $results – Karthick Kumar Feb 21 '14 at 15:28
  • you need to use $db->quote($name) – Elin Feb 22 '14 at 18:25

1 Answers1

0

change this code like this,because return inside foreach will be returned in first turn

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

                   $counter++;
               }
 return $tel;
Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30