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からのものであり、値の数が固定されていないため、関数から値を取得するにはどうすればよいですか?前もって感謝します :)

4

3 に答える 3

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 :(
于 2012-08-16T10:00:54.273 に答える
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..
于 2012-08-16T10:10:30.827 に答える
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();
于 2012-08-16T10:08:33.853 に答える