Im trying to Get the users teams he is a part of to compare the 2 tables
DB TABLES:
CREATE TABLE `teams` (
`teamid` int(11) NOT NULL AUTO_INCREMENT,
`teamname` varchar(45) DEFAULT NULL,
`teamdesc` longtext,
`founder` varchar(45) NOT NULL,
`dateformed` int(11) NOT NULL,
PRIMARY KEY (`teamid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
CREATE TABLE `teammembers` (
`teamid` int(11) NOT NULL,
`member` varchar(45) NOT NULL,
`rank` varchar(45) DEFAULT NULL,
PRIMARY KEY (`teamid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Current function i have to do stuff:
/* removeProject */
function getTeamNames($id){
global $session, $database;
$link = $database->connection;
$stmt = $link->prepare("SELECT teamname FROM teams WHERE teamid=$id");
$stmt->execute();
$r = $stmt->fetch();
return array('teams'=>$r['teamname']);
}
Query to get the team names:
$link = $database->connection;
try{
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$q = "SELECT * FROM teammembers WHERE member=:un";
$prep = $link->prepare($q);
$array = array(
':un' => $user
);
$prep->execute($array);
}catch(PDOException $error){
echo($error);
}
Then the info to display the data:
<select multiple class="form-control">
<?php
while($tid = $prep->fetch()){
$teams = $database->getTeamNames($tid['teamid']);
echo ("<option>$teams</option>");
} ?>
</select>
Its only displaying the data as "Array"
UPDATE!!!
This is how i fixed it :)
while($tid = $prep->fetch()){
$teams = $database->getTeamNames($tid['teamid']);
//echo var_dump($teams);
$team = $teams['teams'];
echo ("<option>$team</option>");
}
Your function function getTeamNames($id)
returns an associative array that's why you are not getting the results.
Your loop should look something like
<?php
while($tid = $prep->fetch()){
$teams = $database->getTeamNames($tid['teamid']);
foreach($teams['teams'] as $team){
echo ("<option>$teams</option>");
}
}
?>
The code is correct, you returned the data as array (return array('teams'=>$r['teamname']);
)
So when you echo the data, use echo("<option>".$teams['teams']."</option>")
or dont return result as an array.