dou29106 2014-12-14 18:45
浏览 50
已采纳

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

I have a SQL query which produces an associative array now I need a way to print this array in table format

This is the array that is produced:

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 )  

Now I print this in a table using this function:

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);
}

?>

However if the array becomes something like this:

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] => )

How can I print the above in a table? basically it would just show the question and not the answer. Since my search facility should be able to search for questions and display them regardless if they have answer or not.

At the moment it won't show any questions that do not have an answer thus completely missing them

This is the model:

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条回答 默认 最新

  • drgweamoi473182981 2014-12-15 02:02
    关注

    I believe the problem here is that you're doing an INNER JOIN on the answers table; this will only permit questions which have an answer associated with them.

    (the INNER appears to be implicit in JOIN, see: 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%'
    

    Using this (slightly reformatted) query, with a LEFT OUTER JOIN on the answers table and users/reputation table (with regards to Answer_User_ID), will include questions without any corresponding answers and the reputation of an answerer (the fields pertaining to these will be NULL).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试