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 :(

Problem: How can i retrieve the values from my function since its values are from foreach and the number of values are not fixed? Thanks in advance :)

5
  • you could return $stmt; inside the function, $stmt=$function->getUsers($gender); outside, then replace the foreach loop (what should I put here) with the one inside your function. Commented Aug 16, 2012 at 9:58
  • You only need to return something that either is an array, an iterator or an object. Use the language as a tool. This is also called Traversable in PHP. Commented Aug 16, 2012 at 10:04
  • is there no other way not to put the foreach in index.php and the foreach will just stay in function? Because i'm planning to use this same kind of function many times? thanks! Commented Aug 16, 2012 at 10:05
  • You could use getAll() from PEAR which gets all results in an array and then return it rather than looping yourself? pear.php.net/manual/en/package.database.db.db-common.getall.php Commented Aug 16, 2012 at 10:08
  • A class called Functions? Sounds like a collection of nothing but barnacle methods (link for 10K+ users only :-( ) Commented Aug 16, 2012 at 10:13

3 Answers 3

2

You should return the users from getUsers and then loop over them to display in the 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 :(
Sign up to request clarification or add additional context in comments.

3 Comments

You should also add a test for $users being an array, otherwise you may get a foreach error. And its polite to state there are no users
is there no other way not to put the foreach in index.php and the foreach will just stay in function? Because i'm planning to use this same kind of function many times? thanks!
@SuiGo You could put the for each in the function and build the string in there and return that instead of the array
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..

Comments

0

you can save all the values in an array and return that array

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

    }

another mistake that i found in your code is that if you want to create a instance of class it should be declared outside class

$function = new Functions();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.