1

Please, see Cha's answer where we are pretty close the final solution. We are debugging the code with these data

  1. database in PostgreSQL
  2. test sql-queries

--

Problem related to the Unsolved problem in the code

How can you refer to the php -element in the following array?

I have the following data

Array
(
    [1] => Array
        (
            [tag] => Array
                (
                    [0] => php
                    [1] => scripts
                )

        )

    [2] => Array
        (
            [tag] => Array
                (
                    [0] => ssh
                    [1] => network
                )

        )

)

where the arrays stand for tags_and_id, question_id and tag respectively. In other words, the variable $tags_and_id has the column question_id and that still has a subcolumn tag and that still each single tag.

I would like know how you can refer to the tag php.

I run unsuccessfully getting nothing as an output

echo $tags_and_id[1]['tag'][0];
2
  • 1
    The final PHP code should be in my answer now. Commented Aug 15, 2009 at 19:10
  • 1
    just a tip, you can use print_r($tag) to view the whole array Commented Aug 16, 2009 at 6:46

2 Answers 2

4

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>"
    );
}
?>
Sign up to request clarification or add additional context in comments.

27 Comments

Nothing needs to get changed as long as it ends up with an array that contains a column with the name question_id and tag
print_r it if you have any doubts about what is in your array.
run print_r($result_tags); outside of the foreach loop.
Ok .. I see it. What happens is you are going through each record, and then I'm trying to go through each record again. I'm editting my post with the complete second while.
What do you get out of the actual database (in the while loop). Right now PostGres is being annoying
|
1

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.