//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
你可以__return $ stmt; __在函數內,__ $ stmt = $ function-> getUsers($ gender); __ outside, 然後用foreach循環替換foreach循環(我應該放在這裏)你的功能。 – Waygood 2012-08-16 09:58:07
你只需要返回一個數組,一個迭代器或一個對象。使用該語言作爲工具。這也被稱爲['PHP中的Traversable'](http://php.net/manual/en/class.traversable.php)。 – hakre 2012-08-16 10:04:58
是否有沒有其他方式不把foreach放在index.php中,而foreach只是停留在函數中?因爲我打算多次使用這種類型的功能?謝謝! – 2012-08-16 10:05:27