Jump to content

How do I query mysql data via a link ($_GET?)? Glossary, term project help!?!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
banker

banker

    Newbie

  • Members
  • PipPip
  • 24 posts
I'm doing a page on my website with a terms glossary.
Thats part of the glossary. The links are of all the letter a-z, and all the terms of the glossary extracted from the database and displayed on glossary.php page.

[ATTACH]2964[/ATTACH]

Here is the simple MYSQL table I made to hold all the terms.
It has 4 columns, id (auto_increment), word (hold the term), definition(holds definition), and alpha(the first letter of the term for sorting purposes) i.e. if alpha is' a' the word starts with an 'a'

[ATTACH]2965[/ATTACH]

My dilemma is sorting only the results based on the letter of the alphabet clicked on the main glossary.php page.

So far I'm able to loop and display all the records on the glossary.php page. I need to sort or limit what records are displayed.

Here is the code I have so far for displaying the records:thumbup1:

<?php 

			

				//connect to the db

				include_once('inc/connect.inc.php');

				


				$sql = "SELECT * FROM terms";

				$query = mysql_query($sql);

				

				//display a to z in links

				$alphabet = range('a','z');


				echo '<div class="letter">';

				foreach($alphabet as $key){

				     echo '<form action="glossary.php" method="GET">';

				     echo '<a href="?s'.$key.'">'.$key.'</a>';

				}

	

				echo '</div><!--/end the letter array link group-->';

				

				echo '<div class="clear-both"></div>';

				

                                //get data from db

				echo '<div id="terms">';

				while($data = mysql_fetch_assoc($query)){

				

				//set local vars

				$id = $data['id'];

				$alpha =$data['alpha'];

				$word = $data['word'];

				$definition = $data['definition'];

				

				echo 	'<b>'.$word.'</b><a name="'.$word.'"></a>

						<p>'.$definition.'</p>';

				}

				echo '</div><!--//end terms-->';

			

			?>


My dilemma. :confused:
I don't know how to limit the terms displayed by letter based on clicking the link.

(i'm guessing theres a way to pass the variable from the link to the mysql query, and returning those values to the glossary.php page.)

HELP!?!?! I'm still a newbie....

#2
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Do you mean like: setting limit for amount of results to display and ordering them by name? (alphabetical)

Then it'd be something like:

                $limit = '0,5'; //e.g. show 5, starting by first encountered result (0)
   	$sql = "SELECT * FROM terms ORDER BY name DESC LIMIT {$limit}";


#3
banker

banker

    Newbie

  • Members
  • PipPip
  • 24 posts
Actually this is not what I was asking for put I figured it out. Here's my solution.

I would pass in a variable through the each link I created of the letters.
This way I don't have to create 26 pages for each listing of terms based from the glossary.

// the links (letters a to z) with sort name value pairs passed in

<a href="glossary.php?sort=a">a</a>
<a href="glossary.php?sort=a">b</a>
<a href="glossary.php?sort=a">c</a>

...


<?php 

if($_GET['sort']=='a'){
       $sqlsort = "SELECT * FROM terms WHERE alpha = 'a' ";
}elseif($_GET['sort']=='b'){
       $sqlsort = "SELECT * FROM terms WHERE alpha = 'b' ";
}elseif($_GET['sort']=='c'){
       $sqlsort = "SELECT * FROM terms WHERE alpha = 'c' ";
}
?>

... i would include this file into the main glossary.php file i created earlier. and create conditional statement for all 26 letters.

To display the terms based on the letter, I would simply add $sqlquery and display results on the page.

FOLLOW UP:Do you know if there's an easier method to write the condition (maybe with a switch statement?!?), so that I don't have to write the elseifs?