2016-03-04 57 views
0

基本上我試圖從表格中獲取數據。當我拉出所有的行時,我將分支作爲ID,並且我試圖與分支表交叉,以便我可以獲得標題並顯示它。這樣做的正確方法是什麼?我對函數沒有太多的經驗,但我認爲函數可以在這裏派上用場。預先感謝您的幫助!Joomla數據庫 - 從第一個表格的結果中獲取不同表格的結果

<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

這應該有加入的,而不是你目前的解決方案來解決:

... 
$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>"; 
... 
相關問題