Forum Moderators: open

Message Too Old, No Replies

showing output according to user's selection

showing the result from mysql query which user generated

         

kevinshah18

11:35 pm on Oct 15, 2009 (gmt 0)

10+ Year Member



Hey guys,
I am a beginer in programming using php and mysql and my knowledge in them is very basic. I know how to create a table (using phpmyadmin)and enter values in it.
I however have this question.
Say for example i have a table in my database which has following information.

(I am extremely sorry for the pathetic try at making a table.
This is a table i have tried to create here.
If u have any problem understanding this table thn send me a mail at <snip> and i will send u a proper constructed table.)

Number...... Name .... Area ...... .Age ...... Weight
1............ A .........france........ 13 ......67
2............ B ...... ...usa............ 34 ...... 78
3............ C .........uk ............ 15 ......34
4............ D .........canada ...... 76...... 89

Now this table is saved in mysql database.

The user will have multiple choice question such as &#8211;

Select by area () usa () uk ()canada ()france ()any

Select by age () less then 15 ()15-40 ()40-60 ()60-90 ()any

Select by weight () less then 30 () 30-50 ()50-70 ()70-100 ()any

The user will select the respective fields and the output we give should him the respective output in tabular format.

please Help me with this. i am unable to even begin.

Thanks a lot.

[edited by: bill at 2:22 am (utc) on Oct. 16, 2009]
[edit reason] Don't post e-mail addresses please [/edit]

mack

1:46 am on Oct 16, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi kevinshah18,
Welcome to WebmasterWorld :)

Have you created any php code yet to try and do this? if so what errors are you getting?

Mysql select will be your friend...

You will need to create an interface for the user to issue his/her query. What you could do is have a number of check boxes and a text field. The user would then select the aspect they where interested in and type their query.

For example...

Area 0 Age 0 Weight 0 [ ] [go]

the "0" is a checkbook the [] is a text field and [go] is the submit button.

Here is some html I put together to help with the form...


<form action="script.php" method="get">
Area<input name="what" type="checkbox" value="area" />
Age<input name="what" type="checkbox" value="age" />
Weight <input name="what" type="checkbox" value="weight"/>
<input name="query" type="text" />
<input type="submit" value="Submit" />
</form>

With this form you are sending a request to a php script. What information we are looking for (area, age or weight) we call this part “what” and the actual query where the user types in. I have simply called this “query”
So if a user wants to view data for all 40 year olds he/she simply checks the age checkbox and types 40 into the text box. The form will send the user to script.php with the following parameters attached to the URL in the address bar.

script.php?what=age&query=40

What we now need to do is build our php script to handle the actual search (script.php)


<?php
#First lets get query and what from the URL
$what = $_GET['what'];
$query = $_GET['query'];

#Then connect to database
mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

#you will need to change host username password and dbname with your own settings

#Now lets ask mysql to show us all data that matches our criteria.

$result = "select * FROM tablename where $what=$query;

#as an example if the user was searching by age for 30 year olds the query means
#show all data from tablename where age is 30.

#We new need to display the data in a table format.

echo "<table border='1'>";
echo "<tr><b><td>Age</td> <td>Area</td> <td>Weight</td></b></tr>";

while($row = mysql_fetch_array( $result ))
{
echo "<tr><td>";
echo $row['age'];
echo "</td><td>";
echo $row['area'];
echo "</td><td>";
echo $row['weight'];
echo "</td></tr>";
}
echo "</table>";
?>

What the above script does is get the requested data from mysql and show it as one table. If you find your database growing very large you might want to look at displaying the data over a number of pages.

Hope this gives you some ideas.

Mack.