filtering a database by client selection

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
jofan
Forum Newbie
Posts: 4
Joined: Wed Jun 30, 2010 4:20 am

filtering a database by client selection

Post by jofan »

Hi All,

Newbie to PHP but slowly getting there, hopefully someone could clarify the following:

I have created a database to hold business records, reviews etc.
I can access the database and display its contents.
I would now like to provide the viewer with options for filtering the data.
I was assuming that i can construct a form with the filter fields on it and then submit this back to the server with the selected values.
Then my PHP would use these to pick out what is required from the database and display it?

Is this the correct way to do this?
I am not 100% sure if the PHP code is unique to each user, or would all users / viewers end up with the same results - still not got my head around this one???
Should i use session variables for this approach?

Looking forward to clarification

Regards, J
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: filtering a database by client selection

Post by Jade »

You'd need to create a search form. Is the data you store unique to each user? If that's the case then you'll need a way of identifying them from one another ie a login/password form prior to gaining access to the search form.

You would use PHP to generate the SQL query run by the database and your filters would be determined by the values they filled in on the form: http://www.tutorialized.com/view/tutori ... search/416
jofan
Forum Newbie
Posts: 4
Joined: Wed Jun 30, 2010 4:20 am

Re: filtering a database by client selection

Post by jofan »

Hi Jade,

Thanks for the reply, i will read through your link very soon.

I can confirm that the database does not need to be unique to each user, only the filtered results.
Its this bit im struggling to understand, but hopefully your link will help.

Just out of interest, is the php code unique to each viewer (ie any variables set up, are they unique or would they be the same value to everyone?)

Kind regards, J
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: filtering a database by client selection

Post by Jade »

The PHP code will be the same for all users, however the selections they make will differ. Take google for instance. There is only one search field on the page but if both you and I go to Google we'll end up getting different results based on the criteria we've entered. The same concept applies to what you're trying to do.
jofan
Forum Newbie
Posts: 4
Joined: Wed Jun 30, 2010 4:20 am

Re: filtering a database by client selection

Post by jofan »

Hi Jade, anyone who can help...

I have set up my search form (this is designed on the same page where the results will be printed).
The form 'action' calls a php file that processes the results.
This is working fine, however it calls up a new page rather than the one i want my results in.

Is there anyway that the action can return to the original page?
If so, do i copy my results script contents into this page?
Is there any example code / Tutorial available on this?

Kind regards, J
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: filtering a database by client selection

Post by Jade »

There are a few ways you can do it.

1. Have the form post back to itself. Let's say you have a file called test.php.

Code: Select all

<?php

if ($_POST['mybutton'])
        echo "I entered " . $_POST['myvalue'];

?>
<form action=test.php method=post>
Enter a value: <input type="text" name="myvalue" value="<?php echo $_POST['myvalue']; ?>" />
<input type="submit" name="mybutton" value="Submit My Value" />
</form>
2. Store your result in a session then have the second page redirect back to the first page (not really an optimum solution).

Here's your test.php file:

Code: Select all

<?php 
session_start();

if ($_SESSION['myvalue'])
        echo "I entered " . $_SESSION['myvalue'];
?>
<form action=test2.php method=post>
Enter a value: <input type="text" value="<?php echo $_SESSION['myvalue']; ?>" />
<input type="submit" name="mybutton" value="Submit My Value" />
</form>
This would be in test2.php:

Code: Select all

<?php
session_start();

if ($_POST['mybutton'])
        $_SESSION['myvalue'] = $_POST['myvalue'];

header("Location: test.php");
exit;
?>
3. Include a your second file on your first file.

Here is test.php:

Code: Select all

<?php
include('test2.php');
?>
<form action=test.php method=post>
Enter a value: <input type="text" name="myvalue" value="<?php echo $myvalue; ?>" />
<input type="submit" name="mybutton" value="Submit My Value" />
</form>
Here is test2.php:

Code: Select all

<?php
if ($_POST['mybutton'])
{
        echo "I entered " . $_POST['myvalue'];
        $myvalue = $_POST['myvalue'];
}
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: filtering a database by client selection

Post by Benjamin »

:arrow: Moved to PHP - Code
jofan
Forum Newbie
Posts: 4
Joined: Wed Jun 30, 2010 4:20 am

Re: filtering a database by client selection

Post by jofan »

Thanks Jade,

I played about with this prior to you posting, and ended up simply removing my script name from the actions field (action ="").
As far as i am aware, this will return to the same page?
I am then using issset function to determine what submit button was used.

So far, its all working ok...hope this method is acceptable

Regards, J
Post Reply