TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Ugh, Query in a while statement (http://www.talkphp.com/absolute-beginners/3232-ugh-query-while-statement.html)

Aaron 08-11-2008 04:53 PM

Ugh, Query in a while statement
 
At first it hurt to put a query in a while statement, then it hurt to rely on my query for organization. Now it hurts because I realized it doesn't work. It works with one category, but when you add a different category it blows up Any category below the first one doesn't get the list items.
I figure it's an error in the HTML, but I still don't like the query in the while statement.
Can I get some help? *!*
PHP Code:

try{
  
// Get the categories
  
$query "
    SELECT DISTINCT `category`
    FROM `aaron_eglast`.`egle_post`
    ORDER BY `category` ASC"
;
    
$sidePanelCategories $my->query($query);
    if (
mysql_num_rows($sidePanelCategories) == 0)
      throw new 
exception('Sorry, the side panel could not be brought up because it could not be found. (Error Code: 55302)');
  
    
    
// Get SP Pages
    
$query "
    SELECT `category`, `slug`, `title`, `order`
    FROM `aaron_eglast`.`egle_post`
    ORDER BY `category` ASC, `order` ASC"
;
    
$sidePanelPages $my->query($query);
    if (
mysql_num_rows($sidePanelPages) == 0)
      throw new 
exception('Sorry, the side panel could not be brought up because it could not be found. (Error Code: 55303)');
  
$b "";
    while (
$a mysql_fetch_array($sidePanelCategories)){
          
$b .= '<li class="topLevel"><h3>'.$a['category'].'</h3><ul>';
                while (
$c mysql_fetch_array($sidePanelPages)){
                  if (
$c['category'] == $a['category']){
                        
$b .= '<li>';
                        if (
MOD_REWRITE == true)
                          
$b .= '<a href="'ROOT '/' strtolower($c['category']) . '/' $c['slug'] . '.html' '">';
                      else
                          
$b .= '<a href="'ROOT '/arch/' strtolower($c['category']) . '/index.php' '?page=' $c['slug'] . '">';
                            
$b .= $c['title'];
                          
$b .= '</a></li>';
                    }
                }
                
            
$b .='</ul></li>';
    }
    if (
$b == false)
      throw new 
exception('There was an error generating the index of the local pages.
          I am sorry, but the pages are unable to be displayed at this moment.'
);
  else
      
$sp $b;
      
$smarty->assign('sp',$sp);
}
catch(
exception $e){
  
$smarty->assign('error',$e->getMessage());



buggabill 08-11-2008 06:33 PM

It looks like you will need to reset the record pointer for $sidePanelPages like so:

php Code:
<?php

try{
  // Get the categories
  $query = "
    SELECT DISTINCT `category`
    FROM `aaron_eglast`.`egle_post`
    ORDER BY `category` ASC"
;
    $sidePanelCategories = $my->query($query);
    if (mysql_num_rows($sidePanelCategories) == 0)
      throw new exception('Sorry, the side panel could not be brought up because it could not be found. (Error Code: 55302)');
 
   
    // Get SP Pages
    $query = "
    SELECT `category`, `slug`, `title`, `order`
    FROM `aaron_eglast`.`egle_post`
    ORDER BY `category` ASC, `order` ASC"
;
    $sidePanelPages = $my->query($query);
    if (mysql_num_rows($sidePanelPages) == 0)
      throw new exception('Sorry, the side panel could not be brought up because it could not be found. (Error Code: 55303)');
    $b = "";
   
    while ($a = mysql_fetch_array($sidePanelCategories)){
          $b .= '<li class="topLevel"><h3>'.$a['category'].'</h3><ul>';
                while ($c = mysql_fetch_array($sidePanelPages)){
                  if ($c['category'] == $a['category']){
                        $b .= '<li>';
                        if (MOD_REWRITE == true)
                          $b .= '<a href="'. ROOT . '/' . strtolower($c['category']) . '/' . $c['slug'] . '.html' . '">';
                      else
                          $b .= '<a href="'. ROOT . '/arch/' . strtolower($c['category']) . '/index.php' . '?page=' . $c['slug'] . '">';
                            $b .= $c['title'];
                          $b .= '</a></li>';
                    }
                }
               
                // reset pointer
                mysql_data_seek($sidePanelPages, 0);
               
            $b .='</ul></li>';
    }
    if ($b == false)
      throw new exception('There was an error generating the index of the local pages.
          I am sorry, but the pages are unable to be displayed at this moment.'
);
  else
      $sp = $b;
      $smarty->assign('sp',$sp);
}
catch(exception $e){
  $smarty->assign('error',$e->getMessage());
} 
?>

as you may want to start from the beginning of that resultset looking for matching categories, and if the pointer is at the end, it may error out.

Aaron 08-11-2008 06:36 PM

Okay, but how would I get that query outside of that demonic while statement? :(

edit: It works! I have no idea how it did, but it did.

.. I need to read up more on RDMS functions.


All times are GMT. The time now is 05:53 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0