Solved

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

Posted on 2007-03-30
8
234 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 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

830 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