Please, see Cha\'s answer where we are pretty close the final solution. We are debugging the code with these data
If print_r
is failing, then something is wrong before that. Your data is probably not being structured in the way you think it is being structured.
And can I just recommend that if you only have the tag and the question name, you just store as a single level array that looks like the following:
array(
'php' => '7',
'sql' => '7',
'python' => '3'
)
If you have other things you're going to store then yeah, you'll have to do what you're doing. In that case, if you can't access the array with the keys in brackets, then your keys are probably not what you think they are, or there is something wrong with your code somewhere.
I looked at your code, and why are you accessing query results like $row[0]
, instead of $row['question_id']
?
Also, you can't print_r($result_tags)
, because data from a query is not actually stored by PHP, it is only referenced. therefore you have to use a while loop to go through it, as the script will call one row of results at a time.
You say: foreach( $result_tags as $question_id => $data )
This doesn't make sense, because there is no value for $result_tags
except for a reference to the query that can be used to call one row of results at a time. Instead you want to do:
while( $row2 = pg_fetch_array( $result_tags )) {
$question_id = $row2['questions_question_id'];
$data = $row2['data'];
$end_array[$question_id]['tags'][] = $data;
// you can't have your $data['tag'] as that refers to the
// value of an array that doesn't exist
}
If you want to add more information about each question, you can simply add to $end_array[1]
.
This is the array my first foreach
statement creates:
// The end result turns out to be something like this:
array(
1 => array(
'tags' => array(
0 => "php",
1 => "sql"),
)
);
<?php
$dbconn = pg_connect("host=localhost port=5432 dbname=noa user=noa password=123");
if( empty($_GET) ) {
// to get titles and question_ids
$result_titles_tags = pg_prepare( $dbconn, "query777",
"SELECT question_id, title
FROM questions
WHERE question_id IN
(
SELECT question_id
FROM questions
ORDER BY was_sent_at_time
DESC LIMIT 50
)
ORDER BY was_sent_at_time
DESC LIMIT 50;"
);
$result_titles = pg_execute( $dbconn, "query777", array());
// TAGS
$result_tags = pg_prepare( $dbconn, "query9",
"SELECT question_id, tag
FROM tags
WHERE question_id IN
( SELECT question_id
FROM questions
ORDER BY was_sent_at_time
DESC LIMIT 50
);"
);
$result_tags = pg_execute( $dbconn, "query9", array());
and the code in the question initially
// Go through each Tag
while( $tags_and_Qid = pg_fetch_array( $result_tags )) {
// Add the Tag to an array of tags for that question
$end_array [ $tags_and_Qid['question_id'] ] ['tag'] [] = $tags_and_Qid['tag'];
}
// First compile the Data
// Go through each question
while( $titles_and_Qid = pg_fetch_array( $result_titles ) ) {
echo ("<div class='question_summary'>"
. $titles_and_Qid['title']
// Problem here!
// How can you print the titles such that
// they are assigned to each tag -set?
);
$i = 0;
// Then Loop Through each question
foreach( $end_array as $tags_and_Qid['question_id'] => $tags_and_Qid['tag'] )
{
echo ("\n\nITERATION NUMBER IS " . $i); // The code is buggy here
// For instance, we get 9 iterations for 3 questions
// Create the starting HTML
echo ("<div class='tags'>");
// Go through each tag
// not sure about this
foreach( $end_array[$tags_and_Qid['question_id']] ['tag'] as $tag )
{
echo ( "<a class='post_tag' href='?tag="
. $tag
. "'>"
. $tag
. "</a>"
);
}
// end the html
echo '</div>';
$i++;
}
echo ("</div>");
}
// to end the list of questions
echo ("</div>"
);
}
?>