简体   繁体   中英

Display Associative Array with Empty Values in table Format?

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;

}
}
 ?>

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM