Skip to main content

How to manipulate a rowset data from sql to php



I have asked a question about how to retrieve data from different tables in sql How to get data from 4 tables in 1 sql query?





When I run the query for my sample data I get 3 rows, anyway being many-to-many relationship between courses and categories I will always get more rows for same course.





My question is how do I handle this data in PHP? I get an array and what I want would be something like:







Array (

[0] => stdClass Object (

[name] => course name

[tutor] => tutor name

[categories] => Array ( categories here )

);







Should I just fetch the data from the categories in a foreach loop after I fetch a course? That would mean bad performance for large amount of data.


Comments

  1. Should I just fetch the data from the categories in a foreach loop after I fetch a course? That would mean bad performance for large amount of data.


    I think that depends on what you will do with the data. If it's just to show to user, i think it's a good idea paginate the results.

    But if you really need to fecth all data in one query and store this in one big array, you can do something like this:

    $last_id = -1;
    for( $x=0; $x < count($myArray); $x++ ) {
    if( $last_id != $myArray[$x]['id'] ) {
    //course changed, do something!
    }
    //do more stuff...
    $last_id = $myArray[$x]['id'];
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex