edvinson
asked on
PHP Looping through results ( within a loop ) . I am stuck.
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. projectsNavTdOffwithConten t = 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($Lette r){
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="cu stomer.php ?letter=%2 3"'>#</td>
//<td style="width: 3px; margin-left: 3px;"></td>
$alphabet = explode('','#ABCDEFGHIJKLM NOPQRSTUVW XYZ');
$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($resu lt)) {
}
}
}
# 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. projectsNavTdOffwithConten
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($Lette
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="cu
//<td style="width: 3px; margin-left: 3px;"></td>
$alphabet = explode('','#ABCDEFGHIJKLM
$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($resu
}
}
}
ASKER
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($Lette r){
global $db, $_DB_name, $_DB_host , $_DB_user , $_DB_pass, $_TABLES, $_USER;
// <td class="projectsNavTdOff" id="td_#" onclick='location.href="cu stomer.php ?letter=%2 3"'>#</td>
//<td style="width: 3px; margin-left: 3px;"></td>
$alphabet = explode('','#ABCDEFGHIJKLM NOPQRSTUVW XYZ');
$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($resu lt)) {
$foundLetters[] = $row->first_letter;
}
/* Now the loops */
foreach ($foundLetters as $theLetter ) {
// !!!!!!!! WHAT DO I DO HERE !!!!!!!!!!!!!!!!
}
}
Am I on the right track?
function printCustomerNavBar($Lette
global $db, $_DB_name, $_DB_host , $_DB_user , $_DB_pass, $_TABLES, $_USER;
// <td class="projectsNavTdOff" id="td_#" onclick='location.href="cu
//<td style="width: 3px; margin-left: 3px;"></td>
$alphabet = explode('','#ABCDEFGHIJKLM
$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($resu
$foundLetters[] = $row->first_letter;
}
/* Now the loops */
foreach ($foundLetters as $theLetter ) {
// !!!!!!!! WHAT DO I DO HERE !!!!!!!!!!!!!!!!
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
looks like angelIII beat me :-)
ASKER
so my little explode thing would not have worked?
explode("", "ABCDEF...");
??
Just curious
explode("", "ABCDEF...");
??
Just curious
It'll work, preg_split is just my preference over explode :-)
ASKER
Well thank you both. I have learned a little more about PHP today.
See ya
See ya
$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 projectsNavTdOffwithConten
else
use projectsNavTdOff
that should do it.