• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling the database from a view file?

#9
[eluser]obiron2[/eluser]
OK,

Before you got to your view, you must have built and array of posts.

1st improvement:
In the view you are looping through the posts array and fetching the number of comments back as a query. You could do this just as easily in the controller and add a new element to each posts array/object which is the number of comments and then use that in your view. This has 2 benefits. The person maintaining the view doesn't have to know anything about the database structure and the view should render faster as the data is pre-fetched.

2nd improvement:
There is a direct relationship between the post and the number of comments so you could get the number of comments while you are getting the blog posts with a simple join and return the count of the posts as part of the data array. This requires less trips to the database and could be significantly faster. IF you wanted to show the comments as well, this is a different issue as there will be n comments for each post and you need a nested array.

obiron


Messages In This Thread
Calling the database from a view file? - by El Forum - 02-08-2009, 11:42 AM
Calling the database from a view file? - by El Forum - 02-08-2009, 12:04 PM
Calling the database from a view file? - by El Forum - 02-08-2009, 01:18 PM
Calling the database from a view file? - by El Forum - 02-08-2009, 01:54 PM
Calling the database from a view file? - by El Forum - 02-08-2009, 02:16 PM
Calling the database from a view file? - by El Forum - 02-08-2009, 02:49 PM
Calling the database from a view file? - by El Forum - 02-08-2009, 02:54 PM
Calling the database from a view file? - by El Forum - 02-10-2009, 12:36 AM
Calling the database from a view file? - by El Forum - 02-11-2009, 08:12 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.