Link to home
Start Free TrialLog in
Avatar of edvinson
edvinsonFlag for United States of America

asked on

PHP Looping through results ( within a loop ) . I am stuck.

I am making a Nav bar, it is simple the letters A-Z representing first letter of customers:

# A B C D E F G H I J ...

I have 3 classes that style the TD based upon whether the letter has a customer.

1. projectsNavTdOn  = The active letter ( for instance if we click 'A' the 'A' is highlighted )
2. projectsNavTdOff = No customers with this letter
3. projectsNavTdOffwithContent = There is a customer in this letter, but we are not on that page.

Here is a screenshot, that will help you understand better:

http://img235.imageshack.us/img235/296/navig5.jpg

In this case, we are letter A ( it's highlighted ) , but we also have customers in the Letter C.

Now my code. I am stuck. Basically I need to loop through both , don't I???

Thanks!

// @param $Letter = Active Page Letter

function printCustomerNavBar($Letter){

      global $db, $_DB_name, $_DB_host , $_DB_user  , $_DB_pass, $_TABLES, $_USER;
      
       // Each iteration should contain the following:
      // <td class="projectsNavTdOff" id="td_#" onclick='location.href="customer.php?letter=%23"'>#</td>
      //<td style="width: 3px; margin-left: 3px;"></td>
      
      $alphabet = explode('','#ABCDEFGHIJKLMNOPQRSTUVWXYZ');
      
      $sql = "Select upper( Left( customers_name, 1)) AS first_letter
                  FROM customers
                  WHERE customers_users_id = {$_USER['masterId']}";
                  
      $result = $db->query($sql);
      
      /* Loop Through Results */
      for(var $i=0; $i<count($alphabet); $i++){
      
            while ($row = $db->fetchNextObject($result)) {
      
                  
            
            }      
            
      }
      
}
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

add distinct to get each letter only once:

    $sql = "Select DISTINCT upper( Left( customers_name, 1)) AS first_letter
                  FROM customers
                  WHERE customers_users_id = {$_USER['masterId']}";

anyhow, you should do it like this:
1) run the above query, and fill a simply array with the values found
2) run the loop from A-Z, and consider 3 cases:
  if the letter is the selected one then
     use the projectsNavTdOn  
  else
     if the letter is in the array filled above then
       use projectsNavTdOffwithContent
     else
       use projectsNavTdOff
     
that should do it.
                 
Avatar of edvinson

ASKER

Ok I have made some of the changes, but I am fairly new to this, and a bit confused.

Am I on the right track?


function printCustomerNavBar($Letter){

      global $db, $_DB_name, $_DB_host , $_DB_user  , $_DB_pass, $_TABLES, $_USER;
      
      // <td class="projectsNavTdOff" id="td_#" onclick='location.href="customer.php?letter=%23"'>#</td>
      //<td style="width: 3px; margin-left: 3px;"></td>
      
      $alphabet = explode('','#ABCDEFGHIJKLMNOPQRSTUVWXYZ');
      
      $sql = "Select DISTINCT upper( Left( customers_name, 1)) AS first_letter
                  FROM customers
                  WHERE customers_users_id = {$_USER['masterId']}";
                  
      $result = $db->query($sql);
      
      $foundLetters = array();
      
      /* Fill Simple Array With Results */
      while ($row = $db->fetchNextObject($result)) {
      
            $foundLetters[] = $row->first_letter;      
            
      }
      
      /* Now the loops */
      
      foreach ($foundLetters as $theLetter ) {
      
            // !!!!!!!! WHAT DO I DO HERE !!!!!!!!!!!!!!!!
      
      }      
            
      
}
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
looks like angelIII beat me :-)
so my little explode thing would not have worked?

explode("", "ABCDEF...");

??

Just curious
It'll work, preg_split is just my preference over explode :-)
Well thank you both. I have learned a little more about PHP today.

See ya