• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop slows performance down - online

#1
[eluser]chops[/eluser]
i have the following while loop on a website

Code:
<?php foreach($query->result() as $row): ?>
        <div class="casesummary-home">

&lt;?            $random = rand();
            while (($random > 6) || ($random < 1)){
                $random = rand();
            }
            ?&gt;
            <a href="casestudies"><img src="images/&lt;?=$random ?&gt;/thumbs/image1.jpg">title ?&gt;" />  </a>


        </div>
        &lt;?php endforeach; ?&gt;

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?

#2
[eluser]xwero[/eluser]
Code:
rand(1,6)
or the better random function
Code:
mt_rand(1,6)

#3
[eluser]chops[/eluser]
simple, tanks man Smile

#4
[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.

#5
[eluser]xwero[/eluser]
he just uses way too much code
Code:
&lt;?php foreach($query->result() as $row): ?&gt;
        <div class="casesummary-home">
<a href="casestudies"><img src="images/&lt;?php echo mt_rand(1,6) ?&gt;/thumbs/image1.jpg">title ?&gt;" />  </a>
</div>
&lt;?php endforeach; ?&gt;

#6
[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

#7
[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()
{
    $rows = $this->db->get('table')->result();
    foreach($rows as $row)
    {
       $row->random = mt_rand(1,6);
    }
    return $rows;
}
This way the view doesn't know where the content comes from what makes it easier to reuse it.

#8
[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() {
  // DB stuff here
  $result = $query->result();
  return $result;
}

Controller
Code:
function casestudies() {
  $data['casestudies'] = $this->Model_name->get_studies();
  $this->load->view('casestudies',$data);
}

View
Code:
&lt;?php foreach($casestudies as $casestudy): ?&gt;
        <div class="casesummary-home">
<a href="casestudies"><img src="images/&lt;?php echo mt_rand(1,6) ?&gt;/thumbs/image1.jpg">title ?&gt;" />  </a>
</div>
&lt;?php endforeach; ?&gt;

#9
[eluser]chops[/eluser]
much appreciated guys


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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