php - Querying inside of a for loop -


i wondering if there way doing more efficiently. right now, have class retrives statuses database. it's pretty simple , shouldn't effect performance much.

public function ($var1, $var2, $var3) {     $feed = array(); //initialize empty array     //query database     $statement = $this->database->prepare("select id, name, excerpt, post, timestamp, tags, title posts col1 = ? , col2 = ? , col3 = ? order id desc limit 15");     $statement->execute(array($var1, $var2, $var3));      while($row = $statement->fetch(pdo::fetch_assoc))     {         $posts[] = array( "id" => $row["id"], /*etc...*/ );      }      return $posts; } //end 

and page set know not efficient @ all:

<?php ($count = 1; $count <= $total; $count++): //display calendar           echo $count;                 $feed = $feed->get($count, $total, $var3);      foreach ($feed $post):          echo $post["id"];                endforeach;  endfor; ?> 

i hope makes sense. there's lot more html thrown in there , everything. right there 18 rows in database, , takes 10 seconds load page. bad. have set way because of design of site. foreach loop has within loop because whole thing set calendar.

my question whether more efficient select of rows, save them outside of loop , work array, or whether it's better run each query inside foreach loop way i'm doing (i've read lot, , know people huge no no). , kind of issues run if used former option , there million rows in database.

i hope makes sense. i'll update question if doesn't. right though 30 queries being made access 1 or 2 rows. other option come selecting of rows in table, , working array, if there pretend 1 million rows in db, feel affect performance lot more.

am right, , solutions? thanks

i want point out did resolve issue. if wondering why foreach loop querying sow because accidentally deleted line connected facebook api within foreach loop every time gather poster's information. if ever stumbles upon question, sure want clarify making many facebook->api calls bad thing. save info in database , query instead.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -