Return data from database into textarea based on anchor link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
recipeDev
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 8:41 pm

Return data from database into textarea based on anchor link

Post by recipeDev »

Hi All,
I'm working on a recipe website and would like to have a feature where there is a food dictionary.
There are links from a to z at the top of a <textarea> object and would like to return data (MySQL as backend database) starting with the respective letter link that was clicked using the onClick method for the anchor tag into the <textarea> object.
Currently I have this code written has a test sample only for the letter "a":

The problems are:
The letter "a" anchor tag is returned x amount of times (which is not the point to have) with ingredients starting with "a" and when you click any of the sequential "a" anchor tags that respective ingredient displays in the <textarea> object with its definition.
I need all of the ingredients starting with the specific letter clicked displayed at once with its definition in the textarea.
I also need to figure out how to store what links were click into a php variable that will be used in the mysql_query instead of "LIKE 'a%'".

Any help would be very much appreciated.

recipeDev

Code: Select all

 
    
   <form name="ingredient_list" id="ingredient_list"  method="get">
    <div class="FoodDictionaryList">
    <?php 
    $q1=mysql_query("SELECT * FROM tblRTG_Ingredients WHERE Ingredient LIKE 'a%'");
    while($row=mysql_fetch_array($q1))
    echo('<a href="#ingredientDef" onclick="ingredient_list.ingredientDef.value=\'' . $row['Ingredient'] ."-". $row['Definition'] . '\';">a</a>'); ?> | 
    <a href="ingredientDictionary/b-ingredients.php">b</a> | 
    <a href="ingredientDictionary/c-ingredients.php">c</a> | <a href="ingredientDictionary/d-ingredients.php">d</a> | 
    <a href="ingredientDictionary/e-ingredients.php">e</a> | <a href="ingredientDictionary/f-ingredients.php">f</a> | 
    <a href="ingredientDictionary/g-ingredients.php">g</a> | <a href="ingredientDictionary/h-ingredients.php">h</a> |
    <a href="ingredientDictionary/i-ingredients.php">i</a> | <a href="ingredientDictionary/j-ingredients.php">j</a> |
    <a href="ingredientDictionary/k-ingredients.php">k</a> | <a href="ingredientDictionary/l-ingredients.php">l</a> |
    <a href="ingredientDictionary/m-ingredients.php">m</a> | <a href="ingredientDictionary/n-ingredients.php">n</a> |
    <a href="ingredientDictionary/o-ingredients.php">o</a> | <a href="ingredientDictionary/p-ingredients.php">p</a> |
    <a href="ingredientDictionary/q-ingredients.php">q</a> | <a href="ingredientDictionary/r-ingredients.php">r</a> |
    <a href="ingredientDictionary/s-ingredients.php">s</a> | <a href="ingredientDictionary/t-ingredients.php">t</a> |
    <a href="ingredientDictionary/u-ingredients.php">u</a> | <a href="ingredientDictionary/v-ingredients.php">v</a> |
    <a href="ingredientDictionary/w-ingredients.php">w</a> | <a href="ingredientDictionary/x-ingredients.php">x</a> |
    <a href="ingredientDictionary/y-ingredients.php">y</a> | <a href="ingredientDictionary/z-ingredients.php">z</a> 
    
    <br/>
    <textarea name="ingredientDef"  rows="10" cols="60"/>
    
    </textarea>
    </div>
   </form>
 
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: Return data from database into textarea based on anchor link

Post by marty pain »

All you need to do is set each of the letters at the top of the page (A-Z) as a link to the same page, but each with a different alpha index.

For example,

Your page = mypage.php

Link for the letter a = mypage.php?Index=a
Link for the letter b = mypage.php?Index=b
Link for the letter c = mypage.php?Index=c
And so on.

Then use $alphaIndex = $_GET['Index'], and use $alphaIndex in you SQL Query.

Hope that makes sense!
maneetpuri
Forum Commoner
Posts: 60
Joined: Tue Oct 07, 2008 6:32 am

Re: Return data from database into textarea based on anchor link

Post by maneetpuri »

Hi,

What Marty Pain has is a very good solution plus with this you will not have to create separate PHP pages for all alphabets.

Cheers,

~Maneet
recipeDev
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 8:41 pm

Re: Return data from database into textarea based on anchor link

Post by recipeDev »

Here is the revised code but the results are still not rendering the way I would like it.
Below is a screen shot of what is being displayed.
The results needs to be returned on the same php page where the links are with the results in the textarea which resides also on the same php page. As you can see all of the ingredients are being returned even though the letter "a" is to be the index and the anchor is multiplied that x amount of times which is not good.

Any help to refining this with a solution would be appreciated.

recipeDev

Image

Code: Select all

<form name="ingredient_list" id="ingredient_list"  method="get">
    <div class="FoodDictionaryList">
    <?php
    if (isset($_GET['Index']))
    {
    $recid = $_GET['Index'];
    }
    $q1=mysql_query("SELECT * FROM tblRTG_Ingredients WHERE Ingredient LIKE '$recid%'");
    while($row=mysql_fetch_array($q1))
    
    echo('<a href="#ingredientDef?Index=a" onclick="ingredient_list.ingredientDef.value=\'' . $row['Ingredient'] ."-". $row['Definition'] . '\';">a</a>');?> | 
    <a href="ingredientDictionary/b-ingredients.php">b</a> | 
    <a href="ingredientDictionary/c-ingredients.php">c</a> | <a href="ingredientDictionary/d-ingredients.php">d</a> | 
    <a href="ingredientDictionary/e-ingredients.php">e</a> | <a href="ingredientDictionary/f-ingredients.php">f</a> | 
    <a href="ingredientDictionary/g-ingredients.php">g</a> | <a href="ingredientDictionary/h-ingredients.php">h</a> |
    <a href="ingredientDictionary/i-ingredients.php">i</a> | <a href="ingredientDictionary/j-ingredients.php">j</a> |
    <a href="ingredientDictionary/k-ingredients.php">k</a> | <a href="ingredientDictionary/l-ingredients.php">l</a> |
    <a href="ingredientDictionary/m-ingredients.php">m</a> | <a href="ingredientDictionary/n-ingredients.php">n</a> |
    <a href="ingredientDictionary/o-ingredients.php">o</a> | <a href="ingredientDictionary/p-ingredients.php">p</a> |
    <a href="ingredientDictionary/q-ingredients.php">q</a> | <a href="ingredientDictionary/r-ingredients.php">r</a> |
    <a href="ingredientDictionary/s-ingredients.php">s</a> | <a href="ingredientDictionary/t-ingredients.php">t</a> |
    <a href="ingredientDictionary/u-ingredients.php">u</a> | <a href="ingredientDictionary/v-ingredients.php">v</a> |
    <a href="ingredientDictionary/w-ingredients.php">w</a> | <a href="ingredientDictionary/x-ingredients.php">x</a> |
    <a href="ingredientDictionary/y-ingredients.php">y</a> | <a href="ingredientDictionary/z-ingredients.php">z</a> 
    
    <br/>
    <textarea name="ingredientDef"  rows="10" cols="60"/>
    
    </textarea>
    </div>
</form>
recipeDev
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 8:41 pm

Re: Return data from database into textarea based on anchor link

Post by recipeDev »

Basically got it now ... just need to have the textarea to be blank when the page is initially loaded.
Currently it's loading all food dictionary items with definition.

Code: Select all

<form name="ingredient_list" id="ingredient_list"  method="get" >
    <div class="FoodDictionaryList">
    
    
    <a href="index.php?Index=a#ingredientDef">a</a> | <a href="index.php?Index=b#ingredientDef">b</a> | 
    <a href="index.php?Index=c#ingredientDef">c</a> | <a href="index.php?Index=d#ingredientDef">d</a> | 
    <a href="index.php?Index=e#ingredientDef">e</a> | <a href="index.php?Index=f#ingredientDef">f</a> | 
    <a href="index.php?Index=g#ingredientDef">g</a> | <a href="index.php?Index=h#ingredientDef">h</a> |
    <a href="index.php?Index=i#ingredientDef">i</a> | <a href="index.php?Index=j#ingredientDef">j</a> |
    <a href="index.php?Index=k#ingredientDef">k</a> | <a href="index.php?Index=l#ingredientDef">l</a> |
    <a href="index.php?Index=m#ingredientDef">m</a> | <a href="index.php?Index=n#ingredientDef">n</a> |
    <a href="index.php?Index=o#ingredientDef">o</a> | <a href="index.php?Index=p#ingredientDef">p</a> |
    <a href="index.php?Index=q#ingredientDef">q</a> | <a href="index.php?Index=r#ingredientDef">r</a> |
    <a href="index.php?Index=s#ingredientDef">s</a> | <a href="index.php?Index=t#ingredientDef">t</a> |
    <a href="index.php?Index=u#ingredientDef">u</a> | <a href="index.php?Index=v#ingredientDef">v</a> |
    <a href="index.php?Index=w#ingredientDef">w</a> | <a href="index.php?Index=x#ingredientDef">x</a> |
    <a href="index.php?Index=y#ingredientDef">y</a> | <a href="index.php?Index=z#ingredientDef">z</a> 
    
    
    <br/>
    <textarea name="ingredientDef"  rows="10" cols="60"/><?php
    if (isset($_GET['Index']))
    {
    $recid = $_GET['Index'];
    }
    $q1=mysql_query("SELECT * FROM tblRTG_Ingredients WHERE Ingredient LIKE '$recid%'");
    while($row=mysql_fetch_array($q1))
    echo $row['Ingredient'] ." - ". "\n" . $row['Definition']. "\n \n" 
    ?>
    </textarea>
    </div>
    </form>
recipeDev
recipeDev
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 8:41 pm

Re: Return data from database into textarea based on anchor link

Post by recipeDev »

All solved now.
Thanks all.

recipeDev
Post Reply