Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see three solutions, I present them from the easiest to the heaviest, but most effective.</p> <h2>Caching, but limited to one request</h2> <p>This solution would be to include a static variable in this function and use this as a temporary store for the students and classes. This would result in fewer queries, because you query each student only once and each class only once.</p> <p>Something like this</p> <pre><code>function format($studentid, $classid){ static $students = array(); static $classes = array(); if !isset($students[$studentid]) $students[$studentid] db_lookup($studentid); if !isset($classes[$classid]) $classes[$classid] db_lookup($classid); $student_name = $students[$studentid]; $class_name = $classes[$studentid]; (...) </code></pre> <p>instead of </p> <pre><code>function format($studentid, $classid){ $student_name = db_lookup($studentid); $class_name = db_lookup($classid); (...) </code></pre> <p>This solution is very easy to implement, but it caches the result only for one request, for instance if you display a table wich contains many times the same course.</p> <h2>Caching between requests</h2> <p>For caching between requests, you need to use a cache solution like for instance the PEAR package Cache_Lite. It allows to cache the result of a function call with a fixed value (e.g. <code>db_lookup($studentid=123)</code> ) and store the result in the cache. Cache_Lite implements a memory cache, a file cache and a database cache. I used it with memcache and it worked well.</p> <p>This solution requires more work, and it will either use diskspace or memory.</p> <h2>Code refactoring</h2> <p>The most efficient solution, but the one that requires the most effort is to refactor your code. It does not make sense to query 500 times the database for one row each time. You should rewrite the code, so that the query gets all data, and then format the data for each row of your record set.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload