tags:

views:

268

answers:

2

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];
+4  A: 

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>"
    );
}
?>
Chacha102
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`
Chacha102
Actually, you would have to change the line `$id = $data['questions_question_id'];` to `$id = $data['question_id'];`, but that's it.
Chacha102
Basically, in the first `foreach` statement, the keys inside the brackets of `$data` need to correspond to your column names.
Chacha102
You have to change the array name in the foreach statement to match the array coming out of your database. In this case it should be changed to `$result_tags`. I'll fix it now
Chacha102
+1 for effort and continuous support.
karim79
`print_r` it if you have any doubts about what is in your array.
Chacha102
**Where should I put the `print_r` -clauses and how?** - I run unsuccessufully `print_r($data['tag']);` and `print_r($data);` within the first foreach -loop and outside the for -each loop.
Masi
run `print_r($result_tags);` outside of the foreach loop.
Chacha102
Do any of the other arrays give results? Can you show me your code? You probably need to debug the code so the right variables match up.
Chacha102
I can't read your mind or what you are programming, so the code I give you probably doesn't have the right variables in it. You have to change the right variables to match up with the data you are getting from your database. If you can't do that, you might want to relook at how foreach and such work.
Chacha102
you need to change `$result_tags` in the first foreach to `$row2`
Chacha102
I need you to update your code again, but most likely this is caused by treating the string as an array.
Chacha102
I get such an output: http://dpaste.com/81014/ for the second foreach when `print_r( $end_array[1]['tags'] )` inside the second loop.
Masi
can you show me the `print_r` or the $end_array?
Chacha102
@Cha: I get this http://dpaste.com/81024/ when the command is after the second foreach.
Masi
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.
Chacha102
@Cha: I get this `Warning: Invalid argument supplied for foreach() in /var/www/codes/handlers/searches/handle_questions_by_time.php` on the line where I put *Problem here* in your code.
Masi
`print_r` the the $end_array[1] right before that foreach line.
Chacha102
@Cha: I get this http://dpaste.com/81135/
Masi
@Cha: I get exactly the same result as I have the `print_r` before the first foreach too. -- I expect that the problem is there where I put *Problem Here2* in your code.
Masi
Ok, the best way to solve this is going to be using print_r after each database call to make sure you have the right data coming from the database. Then print_r before the first foreach to see the format that it is now it. You might need to fiddle around with the statement inside the second while to make sure the tags are put in that array, but I don't have your database or PHP set up, so I can't try all that.
Chacha102
Where did you get this line `$question_id = $row[0];`, shouldn't it be `$question_id = $row['question_id'];`?
Chacha102
@Cha: This is my database http://github.com/vilsu/codes/blob/32b9d3c95e8307df280f76c86a66062fefbbc222/3-SQL-queries/postgres_less.sql and this is its test data http://github.com/vilsu/codes/blob/32b9d3c95e8307df280f76c86a66062fefbbc222/3-SQL-queries/test_questions.sql - You already have the main PHP code in your answer.
Masi
I'm compiling the database on my machine. (Need to go out and grab a few apps since this is a new install)
Chacha102
What do you get out of the actual database (in the `while` loop). Right now PostGres is being annoying
Chacha102
@Cha: I found a few bugs in the logic of the code. I fixed them in your answer. - There is still one bug which I have not managed to solve. It is apparently in the first `foreach` -statement, since I am not sure how to refer correctly to the tag in the array `tags_and_Qid`.
Masi
This is an example of the current data in the first while -loop. It seems to be correct http://dpaste.com/81264/
Masi
Because you are iterating through the WHOLE end array every question, the first question will have 1 iteration, the 2nd will have two, the 3rd will have three, etc.
Chacha102
That is why I seperated the Whiles from the foreach, because first we create all the questions, and then we iterate through them.
Chacha102
@Cha: The problem is actually more challenging. I need to have titles too for each tag -set. The titles are generated in the second -while loop. - I have nod managed to combine the array generated at while loop to that generated by foreach -loop: http://stackoverflow.com/questions/1285391/to-combine-challenging-php-arrays
Masi
+1  A: 

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
}
msumme