putting data from multiple tables into an array
Posted: Wed Sep 02, 2009 1:58 pm
i am trying to get data from two tables and put them into an array that i can return to my template. - i should mention that im quite new to php.
here are my tables:
horses:
id, name, owner_id
owners:
id, name
i have a function called get_horses() that returns details on all of the horses, but i want it to also get the owner name and put that into the array, how can i do this?
my smarty template:
any help would be great. - please let me know if you need any of the other code.
here are my tables:
horses:
id, name, owner_id
owners:
id, name
i have a function called get_horses() that returns details on all of the horses, but i want it to also get the owner name and put that into the array, how can i do this?
Code: Select all
class horse_details {
function get_horses(){
$query = "SELECT * FROM horses";
$result = mysql_query($query);
$horses = array();
while($tmp = mysql_fetch_assoc($result)) {
$horses[] = $tmp;
}
return $horses;
}
}Code: Select all
{foreach item=horse from=$horses}
<tr>
<td>{$horse.name}</td>
<td>{$horse.breed}</td>
<td><a href="owners.php?action=view&owner_id={$horse.owner_id}">{$i.want.the.owner.name.here}</a></td>
</tr>
{foreachelse}
<tr>
<td colspan="3">No items were found</td>
</tr>
{/foreach}