...

View Full Version : Help - how do i.....



stargate03
01-06-2007, 11:43 PM
Hi there

I have a function in php that lists words alphabetically from a text file

what i want to do is, have a navigation system that when i click on the letter A - i just get list of words beginning with A

I dont want to have to do 26 text files

any ideas

whizard
01-07-2007, 12:05 AM
Maybe like this:


$letter="LETTER I WANT TO LOAD";
$words = file('MY TXT FILE');
foreach($words as $word)
{
if(substr($word,0,1) == $letter)
{
print $word."<br />"
}
}


Not fully sure what your looking for, but hope this helps,
Dan

stargate03
01-07-2007, 12:29 AM
ok i understand that

if someone then clicks on A

and gets - list of keywords in navigation - from text file - such as

Aardvark - link to page about aardvarks
Alps - link to page about the alps
Ants - link to page about ants

Now if they click on B or C etc thats fine - they will get list of keyword/links beginning with correct letter

but if they click on a keyword like Aardvark - i still want them to be shown navigation for A

whizard
01-07-2007, 12:37 AM
First, put the code I already wrote in a function:



function loadKeywords($letter)
{
$words = file('MY TXT FILE');
foreach($words as $word)
{
if(substr($word,0,1) == $letter)
{
print $word."<br />"
}
}
}


Then, on each page, where $keyword is the word that the page is about, such as 'Aardvark'


$letter = substr($keyword,0,1);
loadKeywords($letter);


Dan

stargate03
01-07-2007, 01:22 AM
final thing

i have a function that returns the value of the keyword

ie

keyword();

how do i assign the value of that to the variable $keyword

stargate03
01-07-2007, 02:52 AM
ok

so now i have the correct side nav being pulled up based on first letter of the keyword

now the horrid looking bit

If i have a link say <a href=".... >A</a> as my link to all words from the list beginning with A

How do i pull that up dynamically just using a link, and the first page shown will be first in the list of keywords for that letter

whizard
01-07-2007, 04:54 AM
<a href="list.php?letter=a">A</a>

list.php


$letter = $_GET['letter'];
loadKeywords($letter);


I sense I may not be fully answering your question, since I don't really understand what you said here:


How do i pull that up dynamically just using a link, and the first page shown will be first in the list of keywords for that letter

What do you mean the 'first page'?

Dan

stargate03
01-07-2007, 03:54 PM
Ok

I have used the coding u supplied to enable the following

if i am on a page that begins with letter B - it now shows all the keyword links from the text file in navigation down the side, so i can easily move between keywords of the same letter

but now i need to have navigation along the top so that users can move between different letters

so if i click on the A letter in the navigation, i want to be taken to the first page alphabetically i.e. aardvark

If i click on B - i want to be taken to first page beginning with B i.e Bargains

But as all the keywords are in the same text file, i need a way for the navigation to work out which is the first keyword in the file for the corresponding letter

I could hard code the keyword, but that means if i add a word that is first alphabetically for a certain letter, i would have to recode the link.

hope that helps

Shaffer
01-07-2007, 05:59 PM
Ok, here is how it works.
You have only 1 page to run all of this.
The page contains a variable which is handles by the browser:
www.yoursite.com/letters.php?letter=a
You link to it this way:
<a href="www.yoursite.com/letters.php?letter=a">A</a><br/>
<a href="www.yoursite.com/letters.php?letter=b">B</a><br/>
<a href="www.yoursite.com/letters.php?letter=c">C</a><br/>
<a href="www.yoursite.com/letters.php?letter=d">D</a><br/>
etc.
The script is handled according to the url, since the variable used in the function is set by the url, as said above.
Just use the code that whizard kindly contributed in your file.
Then load it to the browser and tell us what you got. ;-)



Shaffer.

stargate03
01-08-2007, 12:53 AM
Hi there

no that wont work

i have to have the links in the format

<a href="domain name/first word in txt file beginning with a">A</a>
<a href="domain name/first word in txt file beginning with b">B</a>

etc etc

remembering that all words are in one txt file

whizard
01-08-2007, 03:35 AM
You may want to consider a database, though I know you have a ton of keywords, which will be a pain to enter in to a database. Using a database will give you a lot of flexibility.

I guess you could put all the elements that start with a certain letter into an array, and then sort it alphabetically, and get the first one:



function firstKeyword($letter)
{
$words = file('MY TXT FILE');
$i = 0;
foreach($words as $word)
{
if(substr($word,0,1) == $letter)
{
$keyword_array['$i'] = $word;
$i++;
}
}
sort($keyword_array);
//First keyword (alphabetically) will be stored in $keyword_array[0]
return $keyword_array[0];
}


Don't know how efficient that would be though..

HTH
Dan

stargate03
01-08-2007, 11:09 AM
Hi Dan

Excellent everything is working briliantly using the array method

only one problem, for some reason the

return $keyword_array[0];

is returning the last keyword in the list for each letter and not the first

whizard
01-08-2007, 05:40 PM
That's weird..

It should sort it the way you want it...

You could try using Rsort() (Reverse Sort) instead of sort()..

Huh

Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum