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