473,382 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Problem displaying linkable alphabet on page

Hi

I am trying to do something with PHP and having problems - a bit of a
a PHP novice I'm afraid.

What I am trying to achieve is to display the alphabet and numbers 0-9
on a web page. I want the letters/numbers to be clickable to specific
web pages if there are companies on my database that start with that
letter. If they are not on the database I just wantthe letters
displayed but not clickable.

I'm ok on all of the seperate components but I cannot seem to fit it
all together

1. I do a select from a SQL database using this string to reduce the
company name to it's initial letter.

$query = 'SELECT DISTINCT LEFT(company_name,1) FROM members ORDER BY
company_name ASC';
2. I know I can use the following to get the numbers & letters
displayed.

for ($numbers = 0; $numbers <= 9; $numbers++) {

echo "<a href='members.php?coletter=$numbers'>$numbers </a>";
}

What I can't seem to work out is how to do a loop to do this check and
decide what to echo

After a few hours of trying various things from the manuals and web I
am now looking for some assistance.

Any help would be appreciated.
Aug 16 '05 #1
5 1688
skinnybloke schrieb:
Hi
I'm ok on all of the seperate components but I cannot seem to fit it
all together
1. I do a select from a SQL database using this string to reduce the
company name to it's initial letter.

$query = 'SELECT DISTINCT LEFT(company_name,1) FROM members ORDER BY
company_name ASC'; ok!

2. I know I can use the following to get the numbers & letters
displayed.

for ($numbers = 0; $numbers <= 9; $numbers++) { only numbers in this case
echo "<a href='members.php?coletter=$numbers'>$numbers </a>";
}

What I can't seem to work out is how to do a loop to do this check and
decide what to echo


// get the first element from your query
$row=mysql_fetch_row($result);
$match=$row?$row[0]:false;

for( .... ) { // loop through your chars
if ($row && $match==$number) { // we have a match
echo "<a href='members.php?coletter=$numbers'>$numbers </a>";
// make your output

//advance in the database
$row=mysql_fetch_row($result);
$match=$row?$row[0]:false;
}
}
hope it helps
however ther are much "cooler" solutions to achive this this goal
eg:
for( .... ) { // loop through your chars
// now check if there ar entries in your DB starting with that letter
$query="SELECT COUNT(*) FROM members WHERE company_name like ${letter}*";
//
$row=mysql_fetch_row($result);
if($row[0] > 0) {
echo ....
}
}

HIH
Jo
Aug 16 '05 #2
"skinnybloke" <th**************@yahoo.co.uk> kirjoitti
viestissä:27********************************@4ax.c om...
Hi

I am trying to do something with PHP and having problems - a bit of a
a PHP novice I'm afraid.

What I am trying to achieve is to display the alphabet and numbers 0-9
on a web page. I want the letters/numbers to be clickable to specific
web pages if there are companies on my database that start with that
letter. If they are not on the database I just wantthe letters
displayed but not clickable.

I'm ok on all of the seperate components but I cannot seem to fit it
all together

1. I do a select from a SQL database using this string to reduce the
company name to it's initial letter.

$query = 'SELECT DISTINCT LEFT(company_name,1) FROM members ORDER BY
company_name ASC';
Now that you have a query result containing the letters and numbers, you can
put them in an array like so:
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) $alphanums[] = $row[0];

2. I know I can use the following to get the numbers & letters
displayed.

for ($numbers = 0; $numbers <= 9; $numbers++) { Now we just add a little condition that decides whether or not a link is
displayed
if(in_array($numbers, $alphanums)) // We check if it is in the array we
did earlier
echo "<a href='members.php?coletter=$numbers'>$numbers </a>";
else // If it wasn't -> just print a number without <a href>.
echo "$numbers"; }


That should do it.

-Kimmo
Aug 16 '05 #3
So a "cooler" solution would be to hit the database once for each
letter/number that might be in there as opposed to hitting it just once
at the beginning?

Aug 16 '05 #4
On Tue, 16 Aug 2005 19:25:49 +0300, "Kimmo Laine"
<et*******************@Mgmail.com> wrote:

Hi Kimmo - thanks for that - I spent hours trying to sort this out and
in the end you've shown me that it was the "While" loop that I had
screwed up.

One other small issue

I currently only have 3 records on the database and all start with an
alpha character yet the script is showing the numeric 0 as a linkable
field.

Is this another of those PHP anomolies? Any ideas on how I put this
right

Aug 17 '05 #5
To anyone interested I corrected this by:
if ( in_array( strval( $numbers ), $alphanums ) )
On Wed, 17 Aug 2005 08:07:28 GMT, skinnybloke
<th**************@yahoo.co.uk> wrote:
On Tue, 16 Aug 2005 19:25:49 +0300, "Kimmo Laine"
<et*******************@Mgmail.com> wrote:

Hi Kimmo - thanks for that - I spent hours trying to sort this out and
in the end you've shown me that it was the "While" loop that I had
screwed up.

One other small issue

I currently only have 3 records on the database and all start with an
alpha character yet the script is showing the numeric 0 as a linkable
field.

Is this another of those PHP anomolies? Any ideas on how I put this
right


Aug 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Peter Olcott | last post by:
The Halting Problem can not be solved within the degree of expressability of a TM. My solution only worked because of its more limited degree of expressability. There is no such thing as a void...
8
by: Jack Addington | last post by:
I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. ...
3
by: pmud | last post by:
Hi, I have 2 drop down lists on an asp.Net page. The 1st contains alphabets. When the user selects an alphabet frm the first list, the second drop down list should be populated with names from...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
8
by: knoxautoguy | last post by:
This problem has consumed a lot of my time, and I'm aftraid someone will tell me that my whole approach to this objective is wrong; however, I would like to know if there is a way to do this. I...
18
by: Mark | last post by:
Hello. I am looking for a way to download and cache a web page that the user has not yet requested, and write the web page to the browser cache without displaying it. My intention is to improve...
0
by: donald | last post by:
Hi all, Trying to having a div center and then div at the side got which i get a problem with The only way i can see to have this done is using a neg number for the margin-top i can't use...
22
by: Wilson | last post by:
i am learning to program using c++ and was set a task of making a simple encryption algorithim. I choose to start with one where simply each letter is replaced with its equivilent in the alphabet...
11
by: dav3 | last post by:
I am trying to code a little console program (no swing, no awt, just console). But I am having some trouble and I am not sure if its my logic, or my code, or possibly both?! Anyways heres what I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.