Object-Oriented Programming

Return and display an array

Class and Method

<?php

class ChoreAssignees
{
	Public $php__year;
	Public $php__week_number;

// ---------------------------------------------------------------------

	function __construct($php__year, $php__week_number) {
		$this->servername='localhost';
		$this->username='admin';
		$this->password='*******';
		$this->dbname='schedules';
		$this->whichTable='assigned_chores';
		$this->year=$php__year;
		$this->week_number=$php__week_number;
	}

// ---------------------------------------------------------------------

	function getClientNames($php__year, $php__week_number, $php__chore) {
		
		$this->chore=$php__chore;

		try {
			$conn = new PDO("mysql:host=$this->servername;dbname=$this->dbname", $this->username, $this->password);
			$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			
			$sql = "SELECT * FROM $this->whichTable WHERE 
				year = $this->year AND 
				week = $this->week_number AND 
				chore = '$this->chore'";

			// $stmt = $conn->prepare($sql);
			$stmt = $conn->query($sql);
			
			$array__client_names = array();
			
			while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
				
				$first_name = $row['first_name'];
				$last_name = $row['last_name'];
				
				$client_name = "$first_name $last_name";
				
				array_push($array__client_names,$client_name);
			}
			
			return $array__client_names;
			
		}
		catch(PDOException $e) {
			echo $e->getMessage();
		}
	}
	
// ---------------------------------------------------------------------
}
?>

Creating an Object and Accessing a Method

<?php

$chore_assignees = new ChoreAssignees($year, $week_number);

$client_names__yard = $chore_assignees->getClientNames($year, $week_number, 'yard');
$client_names__yard__fancy = implode("<br>",$client_names__yard);

?>

Display in HTML

<p><$client_names__yard__fancy></p>