Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 07:31 PM   PM User | #1
dnnhater
New Coder

 
Join Date: Jul 2011
Location: Sunshine State
Posts: 79
Thanks: 18
Thanked 0 Times in 0 Posts
dnnhater is an unknown quantity at this point
A-Z links based on query

Hi all -
I would like to generate a list of links from a-z based on the records returned from a query (for example, records are returned for A, B and F so they have links, C, D and E do not have a record and are not linked: <a href="#">A</a> <a href="#">B</a> C D E <a href="#">F</a>)

I have tried using:
PHP Code:
    public function getCarrierByAlphabet() {
        
$alphabet range('A''Z');
        foreach (
$alphabet AS $alpha) {
            
$getCarrierByAlphabetQuery mysql_query("SELECT carrierName FROM carriers WHERE carrierName LIKE '$alpha%' ORDER BY carrierName") or die ('Get Carrier By Alphabet Query Error --- MySQL Error No: '.mysql_errno().' --- '.mysql_error());
            
$alphaCount mysql_num_rows($getCarrierByAlphabetQuery);
            
$alphaLinks = ($alphaCount '<a href="'.WEBSITE_URL.'/viewCarriers.php?alpha='.$alpha.'">'.$alpha.'</a>' $alpha);
            echo 
$alphaLinks;
        }
        return 
$alphaLinks;
    } 
this list that echos displays and works, but the values returned displays only Z

am I supposed to have a foreach on the page this is displaying on?
dnnhater is offline   Reply With Quote
Old 02-01-2013, 07:50 PM   PM User | #2
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
You can do that with one query as opposed to a query within the foreach loop:
PHP Code:
    public function getCarrierByAlphabet()
    {

        
// only run once per request
        
static $chars;
    
        if ( ! isset( 
$chars ) )
        {
    
            
// All characters we want displayed, A-Z
            
$chars range'A''Z' );
    
            
// Query for existing carrier names
            // - Grab only the first character
            // - Uppercase it
            // - Select distinct so we have at most 26 results (assuming all letters)
            //   regardless of the number of carriers in the database
            
$res mysql_query'
                SELECT DISTINCT UPPER( LEFT( `carrierName`, 1 ) ) AS `alpha`
                FROM `carriers`
            ' 
);
    
            
// Build an array of all *existing* characters
            
$alphas = array();
        
            while ( 
$row mysql_fetch_assoc$res ) )
            {
                
$alphas$row['alpha'] ] = $row['alpha'];
            }
    
            
// Insert links to existing characters
            
foreach ( $chars as & $char )
            {
    
                if ( isset( 
$alphas$char ] ) )
                {
                    
$char '<a href="' WEBSITE_URL '/viewCarriers.php?alpha=' $char '">' $char '</a>';
                }
                
            }

            
// Convert the $chars array to a string using new lines
            
$chars implode"\n"$chars );

        }

        return 
$chars;

    } 
Usage:
PHP Code:
echo $obj->getCarrierByAlphabet(); 
You'll notice I'm returning the HTML and echoing it, as opposed to echoing from within the method itself. Small standard preference that will allow you to be more flexible with your output.
__________________
ZCE

Last edited by kbluhm; 02-01-2013 at 09:33 PM..
kbluhm is offline   Reply With Quote
Users who have thanked kbluhm for this post:
dnnhater (02-01-2013)
Old 02-01-2013, 09:36 PM   PM User | #3
dnnhater
New Coder

 
Join Date: Jul 2011
Location: Sunshine State
Posts: 79
Thanks: 18
Thanked 0 Times in 0 Posts
dnnhater is an unknown quantity at this point
that worked quite loverly - wound up tweaking it just a wee bit more because I decided there was no point to the links pointing a different page (been a couple of months since I've worked on this and have no idea what I was thinking about)

thanks for the help!
dnnhater is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:35 AM.


Advertisement
Log in to turn off these ads.