while loop slows performance down - online - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: while loop slows performance down - online (/showthread.php?tid=9798) |
while loop slows performance down - online - El Forum - 07-09-2008 [eluser]chops[/eluser] i have the following while loop on a website Code: <?php foreach($query->result() as $row): ?> this works fine locally, however not when its online, it slows the performance of the page down and eventually doesn't load that code or anything below it. Can anyone suggest on a better method? while loop slows performance down - online - El Forum - 07-09-2008 [eluser]xwero[/eluser] Code: rand(1,6) Code: mt_rand(1,6) while loop slows performance down - online - El Forum - 07-09-2008 [eluser]chops[/eluser] simple, tanks man while loop slows performance down - online - El Forum - 07-09-2008 [eluser]Lone[/eluser] Is this in a view? If so I would recommend using the proper mvc approach and return the query result from your model to the controller which then would pass the result as a variable to your view. while loop slows performance down - online - El Forum - 07-09-2008 [eluser]xwero[/eluser] he just uses way too much code Code: <?php foreach($query->result() as $row): ?> while loop slows performance down - online - El Forum - 07-09-2008 [eluser]chops[/eluser] xwero is right, i did use far too much code, however is there performance benefits with returning the query from the model? i understand that this is the correct mvc way (still new to this so i have many bad habits) cheers while loop slows performance down - online - El Forum - 07-09-2008 [eluser]xwero[/eluser] No there is no performance benefit and i don't think this should go in the query for two reasons - if you want to use that model method again for other adding a RAND function based pseudo field to the result. - The RAND function in mysql only capable of setting a maximum. the minimum is always zero But if you do use the model method only for that page i would recommend you do add the random number to the model but also output the result instead of the query. Code: function somemethod() while loop slows performance down - online - El Forum - 07-09-2008 [eluser]Lone[/eluser] In my opinion there would be no performance benefits and if there was they would be minimal. If there was anything I think it would increase memory usuage as the whole $query variable is moved from the model, to the controller to the view. Heres a quick sample to head you in the right direction: Model Code: function get_studies() { Controller Code: function casestudies() { View Code: <?php foreach($casestudies as $casestudy): ?> while loop slows performance down - online - El Forum - 07-09-2008 [eluser]chops[/eluser] much appreciated guys |