2012-08-16 81 views
0
//this is my code from class.php 
class Functions 
{  
function getUsers($sex) 
{ 
    $stmt = $pdo->prepare('SELECT lname,fname from users WHERE gender = :gender'); 
    $stmt->execute(array(':gender'=>$sex)); 

    foreach($stmt as $row) 
    { 
     echo $row['lname'].'-'.$row['fname']; 
     //this is what i think but i don't need to echo because the echo must be in index.php.What is the right way? 
    } 
} 
$function = new Functions(); 
} 

//this is for my index.php 
include 'class.php' 
$function->getUsers($gender); 

//this is what i think but it is probably wrong or incomplete. 
foreach(what should i put here?) 
{ 
    echo '<li>' names should be here? '</li>' 
} 
//the output should get all the user based on the gender :(

問題(沒有固定值的個數)從一個foreach函數中:我怎樣才能檢索我的函數的值,因爲它的值是從的foreach和值的數目是不固定的?感謝提前:)檢索多個值在PHP

+0

你可以__return $ stmt; __在函數內,__ $ stmt = $ function-> getUsers($ gender); __ outside, 然後用foreach循環替換foreach循環(我應該放在這裏)你的功能。 – Waygood 2012-08-16 09:58:07

+0

你只需要返回一個數組,一個迭代器或一個對象。使用該語言作爲工具。這也被稱爲['PHP中的Traversable'](http://php.net/manual/en/class.traversable.php)。 – hakre 2012-08-16 10:04:58

+0

是否有沒有其他方式不把foreach放在index.php中,而foreach只是停留在函數中?因爲我打算多次使用這種類型的功能?謝謝! – 2012-08-16 10:05:27

回答

1
//this is my code from class.php 
class Functions { 
    function getUsers($sex) { 
     $stmt = $pdo - > prepare('SELECT lname,fname from users WHERE gender = :gender'); 
     $stmt - > execute(array(':gender' = > $sex)); 

     foreach($stmt as $row) { 
      $names[] = $row['lname'].'-'.$row['fname']; 
      //this is what i think but i don't need to echo because the echo must be in index.php.What is the right way? 
     } 
     return $names; 
    } 
} 

//this is for my index.php 
include 'class.php' 
$function = new Functions(); 
$names = $function->getUsers($gender); 

//this is what i think but it is probably wrong or incomplete. 
foreach($names as $name) { 
    echo '<li>'.$name.'</li>' 
} 
//the output should get all the user based on the gender :(

//y u discriminate? just kidding.. 
+0

謝謝,我會試試這個 – 2012-08-16 10:21:19

2

您應該返回從getUsers的用戶,然後遍歷他們在index.php來顯示

//this is my code from class.php 
class Functions 
{  
function getUsers($sex) 
{ 
    $stmt = $pdo->prepare('SELECT lname,fname from users WHERE gender = :gender'); 
    $stmt->execute(array(':gender'=>$sex)); 
    if(is_array($stmt)){ 
    return $stmt; 
    }else{ 
    return false; 
    } 
} 

} 

//this is for my index.php 
include 'class.php' 
$function = new Functions(); 
$gender = "male" 
$users = $function->getUsers($gender); 
if($users){ 
foreach($users as $row) 
{ 
     echo '<li>' . $row['lname'].'-'.$row['fname'] . '</li>' 
}   

}else{ 
    echo "no users!"; 
    } 
    //the output should get all the user based on the gender :(
+1

你還應該添加一個測試,作爲一個數組的$用戶,否則你可能會得到一個foreach錯誤,其禮貌的狀態是沒有用戶 – Waygood 2012-08-16 10:04:04

+0

是否沒有其他方式不把foreach在index.php和因爲我打算多次使用這種類型的功能?謝謝! – 2012-08-16 10:06:21

+0

添加錯誤trpaaing謝謝 - @Waygood, – alfrodo 2012-08-16 10:09:34

0

可以保存所有的值在數組中,並返回該數組

class Functions 
    {  
    function getUsers($sex) 
    { 
     $stmt = $pdo->prepare('SELECT lname,fname from users WHERE gender = :gender'); 
     $stmt->execute(array(':gender'=>$sex)); 

     foreach($stmt as $row) 
     { 
      $userdetails[] = $row['lname'].'-'.$row['fname']; 
      //this is what i think but i don't need to echo because the echo must be in index.php.What is the right way? 
     } 
    return $userdetails; 
    } 

    } 

另一個錯誤,我在你的代碼中發現的是,如果你想創建類的實例一個它應該課外聲明

$function = new Functions(); 
+0

感謝您的意見!我真的需要:) – 2012-08-16 10:20:48