TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-11-2008, 04:53 PM   #1 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Red face 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());

__________________
Signatures are nothing but incriminating.

Last edited by Aaron : 08-11-2008 at 05:15 PM.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 08-11-2008, 06:33 PM   #2 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

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.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll

Last edited by buggabill : 08-11-2008 at 06:37 PM. Reason: reword response...
buggabill is offline  
Reply With Quote
The Following User Says Thank You to buggabill For This Useful Post:
Aaron (08-11-2008)
Old 08-11-2008, 06:36 PM   #3 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

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.
__________________
Signatures are nothing but incriminating.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:51 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design