php - array structure of database results -
This can be quite a trivial question, but from these methods to structuring an array for return dB results Which method is best? Ask a list of blog posts ... Sorting and grouping posts? Or classifying and grouping elements? Array ([title] = & gt; array ([0] = & gt; untitled [1] = & gt; untitled) [ID] => Array ([0] => 8 [1] => 11))
or
array ([0] => array (['id'] = & gt; 8 ['Title'] = & gt; Untitled) [1] = & gt; Array (['id'] => [['title'] = & gt; untitled])
The first is the easiest way and the way I think it is doing. I just:
while ($ line = $ get result-> (PDO :: FETCH_ASSOC)) {$ post ['title'] [] = $ row ['title']; $ Post ['id'] [] = $ row ['id']; }
and then
$ count = count ($ post ['id']);
But on the other hand, making a better understanding and information appears to be more logical. This is slightly more complex to set up, filter and work in the template.
$ c = 0; While receiving ($ line = $ result-> (PDO :: FETCH_ASSOC)) {$ post [$ c] ['title'] = $ line ['title']; $ Post [$ c] ['id'] = $ line ['id']; $ C ++; }
The second is better and easier to set up:
< Pre> while ($ line = $ result-> get (PDF :: FETCH_ASSOC)) {$ posts [] = $ row; } Total number $ $ $ nrPosts = count ($ post);
This will auto-index the number numerically, and you do not need to copy from $ line to $ post, it will copy the whole line.
The second structure is better:
- It logically groups the data of a post in a structure
- No handling of data for it You can easily handle any blog post with the category of any index / index
Html>
Comments
Post a Comment