LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2006, 11:28 AM   #1
Tom "Techno" Earl
Member
 
Registered: Sep 2006
Posts: 37

Rep: Reputation: 16
I'm trying to get PHP to filter results from a MySQL query!!!


Hey everybody!

I am trying to build a MySQL database of Fans for a local band, so that I can mail them out a newsletter. I am trying to get it so that I can have a dropdown menu with all the locations loaded into it, then it will open a table with all the people in that area.

I have managed to get it so that it does that so far, however, it is showing an option for EVERY result, rather than filtering out the ones that are the same as each other, is there any way that I can get it to see if there is more than 1 result and then just display that as one option??

If anyone knows what I mean, I would be very pleased if they could tell me the answer!

Thanks

Here is the code I am using at the mo:

This is the main page, which contains the form
PHP Code:
<?php
 
// include the headers for jakeandelwood.co.uk
 
include 'jakeandelwood.html';
?>
<?php
 
// This is a small program designed to query a MySQL database
 // to draw up email addresses based on location, it uses a simple
 // way of querying the database (included in email.php), this 
 // script should run off any database, as long as you have a little
 // knowledge of PHP and MySQL and are able to change the appropriate 
 // fields in the script and on your MySQL database
?> 
<?php
 
// Check to see if the post variable is present, if not load the form
 
if ($_POST) {
 include 
'email.php';
} else { 
?>
<h1>Database look up</h1>
<b>Please use the drop down box below to find email addresses that belong to people in different areas!</b><br><br>
 <center>
 <form action="index.php" method="post">
  <select name="county">
<?php
}
?>
<?php
   
// Open a mysql connection
   
include 'includes/mysql.php';
   
$select1 "SELECT town_city FROM locations";
   
$result1 mysqli_query($link$select1);
// loop through to bring up all the database locations
 
while ($row mysqli_fetch_array($result1)) {
    
$town_city $row['town_city'];
    echo 
"<option>".$town_city."</option>";
}
?>
This is the table page with the results:
PHP Code:
<?php
 
// include the details of the mysql database
 
include 'includes/mysql.php';
 
$input $_POST['county'];
?>
<!--Set up a table-->
<table border="1" cellpadding="5">
 <tr>
  <th>Database ID</th>
  <th>Name</th>
  <th>E-mail address</th>
  <th>Town/City</th>
  <th>County</th>
  <th>Status</th>
 </tr>
<?php
// build and execute the query
$select "SELECT id, name, email, town_city, county, status FROM locations WHERE county=\"$input\"";
$result mysqli_query($link$select);

//loop through the results
while ($row mysqli_fetch_array($result)) {
//get each element and put it in a variable
$id $row['id'];
$name $row['name'];
$email $row['email'];
$town_city $row['town_city'];
$county $row['county'];
$status $row['status'];

//Print out the results
echo <<<END
 <TR>
  <TD>
$id</TD>
  <TD>
$name</TD>
  <TD><A HREF="mailto:
$email">$email</A></TD>
  <TD>
$town_city</TD>
  <TD>
$county</TD>
  <TD>
$status</TD>
 </TR>
END;
}
?>
<!--End the Table-->
</TABLE> 
<?php
 mysqli_close
($link); 
?>
This is the connection data:
PHP Code:
<?php
// Open connection with the MySQL database
$link mysqli_connect('localhost','USERNAME','PASSWORD','DATABASE');
?>
Thanks
 
Old 12-06-2006, 11:54 AM   #2
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Rep: Reputation: 30
ehh buddy... I am afraid it is not 100% clear for me.

So there is a table like that:

Code:
id | name | location
----------------------
 1 | Jack | Hamburg
 2 | Jess | New York
 3 | Steve| Hamburg
And you want to select to location but each one only once? Eg: Hamburg to be displayed once.. Right?
You can do that by adding the DISTINCT to the selection:
Code:
SELECT DISTINCT(location) FROM table
After that you should make a selection like that:
PHP Code:
$res SELECT FROM table WHERE `location` = '".$_GET['form_location']."'
I hope it will help... Ask if you need further help.
And chk this site out:
http://dev.mysql.com/doc/refman/5.0/en/select.html
 
Old 12-06-2006, 12:01 PM   #3
Wells
Member
 
Registered: Nov 2004
Location: Florida, USA
Distribution: Debian, Redhat
Posts: 417

Rep: Reputation: 53
Quote:
I have managed to get it so that it does that so far, however, it is showing an option for EVERY result, rather than filtering out the ones that are the same as each other, is there any way that I can get it to see if there is more than 1 result and then just display that as one option??
So are you saying that your database has multiple entries of the same people, and you just want to show them once if they appear more than once in the query?

You probably need to use either DISTINCT or a GROUP BY to get what you are looking for.
 
Old 12-07-2006, 04:08 AM   #4
Tom "Techno" Earl
Member
 
Registered: Sep 2006
Posts: 37

Original Poster
Rep: Reputation: 16
Hey,

Thanks for the replies,

I'm sorry about the unclearness of my post! I didn't quite know how to put it into words!

But, thanks, you've both got what I meant, and I have tried vargadanis' post, and that works perfectly, had some teething problems at first, but then I realised I'd missed a ' out of the code somewhere a bit further up

Anywayz, got it working now, it's brilliant, I'll post where it is when it's finished, so others can see what I meant.

Thanks

Tom "Techno" Earl
 
Old 07-18-2011, 07:49 AM   #5
emakundi
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
Talking Thanx

Thanx man ive been looking for this for days now.
had to register just 2 reply dis. lol
 
  


Reply

Tags
filter, mysql, php, results


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL results to html forms using PHP xemous Programming 3 08-15-2005 04:27 PM
php/,mysql problem: can't query JJX Linux - General 4 01-06-2005 06:10 PM
php: Why Can't I Query Mysql DB?? flamesrock Programming 7 11-16-2004 01:36 AM
PHP MySQL Query Question vi0lat0r Programming 1 07-15-2004 06:02 AM
PHP show query results Gerardoj Programming 1 05-11-2004 01:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration