Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help - how do i.....

    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

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Maybe like this:
    PHP Code:
    $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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    First, put the code I already wrote in a function:

    PHP Code:
    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'
    PHP Code:
    $letter substr($keyword,0,1);
    loadKeywords($letter); 
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ok nearly there

    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

  • #6
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    managed to solve that

    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

  • #7
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    <a href="list.php?letter=a">A</a>

    list.php
    PHP Code:
    $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:
    Quote Originally Posted by stargate03
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #8
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    explanation

    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

  • #9
    New Coder
    Join Date
    Oct 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb

    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.

  • #10
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #11
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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:

    PHP Code:
    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
    Last edited by whizard; 01-08-2007 at 04:40 PM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #12
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #13
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    That's weird..

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

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

    Huh

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •