Solved

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

Posted on 2007-03-30
8
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 143

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 143

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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 look for a specific file type in a local or remote server directory using PHP.

617 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