在表格格式中显示具有空值的关联数组?

时间:2014-12-14 18:45:45

标签: php html arrays codeigniter

我有一个SQL查询,它产生一个关联数组现在我需要一种以表格格式打印这个数组的方法

这是生成的数组:

Array ( [0] => Array ( 
   [username] => Humza 
   [Reputation] => 100 
   [Question_Title] => Did This Work? 
   [Question] => Well Did it? 
   [Rating] => 1001 
   [Category] => Dr Who 
   [AnswerUserName] => God 
   [AnswerUserReputation] => -50 
   [Answer] => Please Work
   [Answer_Rating] => 100 
   [AnswerCategory] => Dr Who )  

现在我使用此功能在表中打印:

function build_table($array){

// start table
$html = '<table>';

// header row
$html .= '<tr>';

foreach($array[0] as $key=>$value){
        $html .= '<th>' . $key . '</th>';
    }

$html .= '</tr>';

// data rows
foreach( $array as $key=>$value){
    $html .= '<tr>';

    foreach($value as $key2=>$value2){

        $html .= '<td>' . $value2 . '</td>';

    }

    $html .= '</tr>';

}

// finish table and return it

$html .= '</table>';

return $html;

}

 $array = $term;

if (empty($array)) {
 echo "No results could found, please try and use a different search term and remember
 your searching for a question title";
}
else
{
echo build_table($array);
}

&GT;

但是如果数组变成这样:

Array ( [0] => Array ( 
  [username] => Humza 
  [Reputation] => 100 
  [Question_Title] => Did This Work? 
  [Question] => Well Did it? 
  [Rating] => 1001 
  [Category] => Dr Who 
  [AnswerUserName] => 
  [AnswerUserReputation] => 
  [Answer] => 
  [Answer_Rating] => 
  [AnswerCategory] => )

如何在表格中打印上述内容?基本上它只会显示问题,而不是答案。由于我的搜索工具应该能够搜索问题并显示它们,无论它们是否有答案。

目前它不会显示任何没有答案的问题,因此完全错过了它们

这是型号:     

function __construct()
{
    parent::__construct();
    $this->load->database();
}

function GuestSearch($Term)
{

/*This will print out the Question Title, Question, Username and Rating atm*/

$sql2 = "SELECT u.username, 
r.Reputation,
q.Question_Title, 
q.Question, 
q.Rating, 
c.Category,
ua.username as AnswerUserName,
ar.Reputation as AnswerUserReputation,
a.Answer,
a.Answer_Rating,
ac.Category as AnswerCategory
FROM Questions q 
join Users u on u.id = q.User_ID
join Answers a on a.Question_ID = q.Question_ID
join Users ua on ua.id = a.Answer_User_ID
join Reputation r on r.User_Rep_ID = u.id
join Categorys c on c.Category_ID = q.Category_ID
join Categorys ac on ac.Category_ID = q.Category_ID
join Reputation ar on ar.User_Rep_ID = a.Answer_User_ID
WHERE q.Question_Title LIKE '%$Term%'";


$results = $this->db->query($sql2);

$data = $results->result_array();

$results->free_result();

return $data;

}
}
 ?>

1 个答案:

答案 0 :(得分:0)

我相信这里的问题是你在答案表上做了一个INNER JOIN;这只会允许有与之相关的答案的问题。

INNER似乎隐含在JOIN中,请参阅:Difference between JOIN and INNER JOIN

SELECT u.username, r.Reputation, q.Question_Title,
    q.Question, q.Rating, c.Category,
    ua.username as AnswerUserName,
    ar.Reputation as AnswerUserReputation,
    a.Answer, a.Answer_Rating,
    ac.Category as AnswerCategory
FROM Questions q 
    INNER JOIN Users u ON u.id = q.User_ID
    LEFT OUTER JOIN Answers a ON a.Question_ID = q.Question_ID
    LEFT OUTER JOIN Users ua ON ua.id = a.Answer_User_ID
    INNER JOIN Reputation r on r.User_Rep_ID = u.id
    INNER JOIN Categorys c ON c.Category_ID = q.Category_ID
    INNER JOIN Categorys ac ON ac.Category_ID = q.Category_ID
    LEFT OUTER JOIN Reputation ar ON ar.User_Rep_ID = a.Answer_User_ID
WHERE q.Question_Title LIKE '%$Term%'

使用此(稍微重新格式化)查询,在答案表和用户/信誉表(关于LEFT OUTER JOIN)上使用Answer_User_ID,将包含没有任何相应答案和问题的问题answerer(与这些相关的字段将为NULL)。