Changeset 2309


Ignore:
Timestamp:
Apr 26, 2008, 1:38:04 AM (16 years ago)
Author:
rvelices
Message:
  • less mysql queries on picture page (under some circumstances)
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/picture_rate.inc.php

    r2299 r2309  
    2929if ($conf['rate'])
    3030{
    31   $query = '
     31  if ( NULL != $picture['current']['average_rate'] )
     32  {
     33    $query = '
    3234SELECT COUNT(rate) AS count
    3335     , ROUND(AVG(rate),2) AS average
     
    3638  WHERE element_id = '.$picture['current']['id'].'
    3739;';
    38   $row = mysql_fetch_array(pwg_query($query));
     40    $row = mysql_fetch_array(pwg_query($query));
     41  }
     42  else
     43  { // avg rate null -> no rate -> no need to query db
     44    $row = array( 'count'=>0, 'average'=>NULL, 'std'=>NULL );
     45  }
    3946  $template->assign('rate_summary', $row);
    4047
  • trunk/picture.php

    r2299 r2309  
    613613
    614614    'LEVEL_SEPARATOR' => $conf['level_separator'],
    615    
     615
    616616    'FILE_PICTURE_NAV_BUTTONS' => 'picture_nav_buttons.tpl',
    617617
     
    638638      );
    639639  }
    640  
     640
    641641  $template->assign(
    642642    array(
     
    816816
    817817// related categories
    818 foreach ($related_categories as $category)
    819 {
     818if ( count($related_categories)==1 and
     819    isset($page['category']) and
     820    $related_categories[0]['category_id']==$page['category']['id'] )
     821{ // no need to go to db, we have all the info
    820822  $template->append(
    821     'related_categories',
    822       count($related_categories) > 3
    823         ? get_cat_display_name_cache($category['uppercats'])
    824         : get_cat_display_name_from_id($category['category_id'])
    825     );
     823      'related_categories',
     824      get_cat_display_name( $page['category']['upper_names'] )
     825    );
     826}
     827else
     828{ // use only 1 sql query to get names for all related categories
     829  $ids = array();
     830  foreach ($related_categories as $category)
     831  {// add all uppercats to $ids
     832    $ids = array_merge($ids, explode(',', $category['uppercats']) );
     833  }
     834  $ids = array_unique($ids);
     835  $query = '
     836SELECT id, name, permalink
     837  FROM '.CATEGORIES_TABLE.'
     838  WHERE id IN ('.implode(',',$ids).')';
     839  $cat_map = hash_from_query($query, 'id');
     840  foreach ($related_categories as $category)
     841  {
     842    $cats = array();
     843    foreach ( explode(',', $category['uppercats']) as $id )
     844    {
     845      $cats[] = $cat_map[$id];
     846    }
     847    $template->append('related_categories', get_cat_display_name($cats) );
     848  }
    826849}
    827850
Note: See TracChangeset for help on using the changeset viewer.