基本上我试图从表中获取数据。当我拉出所有行时,我将分支作为ID,并且我试图通过分支表交叉引用它,以便我可以获得标题并显示它。这样做的正确方法是什么。我没有太多的功能实验,但我认为一个功能可以在这里派上用场。提前感谢您的帮助!
<table class="table table-striped">
<tbody>
<tr>
<td><h5>Title</h5></td>
<td><h5>Location</h5></td>
<td><h5>Time</h5></td>
</tr>
<?php
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__jobs_description');
// sets up a database query for later execution
$db->setQuery($query);
// fetch result as an object list
$result = $db->loadObjectList();
foreach ( $result as $row ) {
echo "<tr>";
echo "<td>";
echo $row->title;
echo "</td>";
echo "<td>";
getbranchtitle($row->branch);
echo "</td>";
echo "<td>";
echo $row->type;
echo "</td>";
echo "</tr>";
}
function getbranchtitle(){
// Get branch name
$db2 = JFactory::getDbo();
$query2
->select($db->quoteName(array('id', 'title')))
->from($db->quoteName('#__vna_jobs_branch'))
->where('id = '. ($row->branch))
->order('ordering ASC');
$result2 = $db->loadObjectList();
echo $result2->title;
}
?>
</tbody>
</table>
答案 0 :(得分:0)
这应该通过连接而不是当前的解决方案来解决:
...
$query = $db->getQuery(true);
$query->select('j.*');
$query->select('b.title as branchtitle');
$query->from('#__jobs_description', 'j');
$query->leftJoin('#__vna_jobs_branch b on (j.branch=b.id)')
// sets up a database query for later execution
$db->setQuery($query);
...
现在,branchtitle将直接在你的foreach循环中提供
...
echo "<tr>";
echo "<td>";
echo $row->title;
echo "</td>";
echo "<td>";
$row->branchtitle; // from your query join
echo "</td>";
...