Solved

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

Posted on 2007-03-30
8
217 Views
Last Modified: 2013-12-13
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)) {
      
                  
            
            }      
            
      }
      
}
0
Comment
Question by:edvinson
  • 3
  • 3
  • 2
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18823433
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.
                 
0
 
LVL 1

Author Comment

by:edvinson
ID: 18823535
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 !!!!!!!!!!!!!!!!
      
      }      
            
      
}
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18823646
something like this:

    /* Now the loops */
   
   foreach ( $alphabet  as $letter )
   {
      $class = "projectsNavTdOff";
      $letter_url = $letter;
      if ($letter == "#") { $letter_url = "%23"; }


      if ($letter == $Letter)
      {
          $class = "projectsNavTdOn";
      }
      else if ( in_array ($letter, $foundLetters))
      {
         $class = "projectsNavTdOffWithContent";
      }

       print "<td class=\"$class\" id=\"td_#\" onclick='location.href=\"customer.php?letter=$letter_url\"'>$letter</td>\n";    
   }



   
0
 
LVL 4

Assisted Solution

by:secondv
secondv earned 250 total points
ID: 18823679
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 = preg_split('//', '#ABCDEFGHIJKLMNOPQRSTUVWXYZ', -1, PREG_SPLIT_NO_EMPTY);
      $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 ($alphabet AS $alpha)
      {
            if ($alpha == $Letter)
            {
                  $class = 'projectsNavTdOn';
            }
            else if (in_array($alpha, $foundLetters))
            {
                  $class = 'projectsNavTdOffwithContent';
            }
            else
            {
                  $class = 'projectsNavTdOff';
            }
            echo "<td class=\"$class\" id=\"td_#\" onclick='location.href=\"customer.php?letter=$alpha\"'>$alpha</td>\n";
      }
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:secondv
ID: 18823691
looks like angelIII beat me :-)
0
 
LVL 1

Author Comment

by:edvinson
ID: 18823741
so my little explode thing would not have worked?

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

??

Just curious
0
 
LVL 4

Expert Comment

by:secondv
ID: 18823756
It'll work, preg_split is just my preference over explode :-)
0
 
LVL 1

Author Comment

by:edvinson
ID: 18823774
Well thank you both. I have learned a little more about PHP today.

See ya
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now