Solved

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

Posted on 2007-03-30
8
208 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 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

10 Experts available now in Live!

Get 1:1 Help Now