Solved

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

Posted on 2007-03-30
8
225 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.htaccess 5 38
PHP curl issue VERBOSE output 18 44
Number of hours between date in DB and now 8 18
What does != "" mean in programming 8 34
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 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