Jump to content

Recommended Posts

Hello all.

 

I've been working hard in the last few days trying to get up to speed with PHP (and MYSQL) and recently I've got to the stage where I've got some test data in a MYSQL database, and have been outputting some of the data as a png image.

 

So basically, I ask for all records (select * from table) and then the image stuff does it's magic and outputs a nice statistical image.

 

However, I'm kind of at the point where obviously it would be nice to start filtering this data by using different types of Selects, such as...

 

$result = mysql_query("SELECT * FROM Persons
WHERE FirstName='Peter'");

 

AS you will know this displays only where the first name of entries in database is "Peter"

 

But this is hard written into each .php document - but what happens if i quickly need to list only "James" or "Andrew"? Do I need to hard-code every single eventuality or filter/select?

 

Where do I go from here? I need to be using say, drop-down select boxes or tick boxes to quickly filter things and then print out my statistic image. Does anyone know any good tutorials? What is the next step? All the tutorials I have only take me as far as SELECT * or SELECT * WHERE....

 

Thanks for your time

Link to comment
https://forums.phpfreaks.com/topic/220087-just-another-noobish-question/
Share on other sites

That makes a lot of sense and now I feel a little foolish, although it has been a long day  :D

 

SO are we talking something liiiiiiiiike......

 

$result = mysql_query("SELECT * FROM Persons
WHERE $ChosenColumn='$ChosenEntry'");

 

Am I on the right tracks? :confused:

 

That's a good start. I'd start by querying the database for the values in the field that you want to filter by, and build the form element that way. Then use that value in the next query. Let me know if this doesn't make sense.

 

$query = "SELECT `name` FROM `table`";
$result = mysql_query($query);
echo "<select name=\"name\">\n";
while( $array = mysql_fetch_assoc($result) ) {
     echo "<option value=\"{$array['name']}\">{$array['name']}</option>\n";
}
echo'</select>';

 

Then in the script that processes that, use the value of $_POST['name'] in the query string

$name = mysql_real_escape_string($_POST['name']);
$query = "SELECT `field1`, field2`, `field3` FROM `table` WHERE `name` = '$name'";

That's a good start. I'd start by querying the database for the values in the field that you want to filter by, and build the form element that way. Then use that value in the next query. Let me know if this doesn't make sense.

 

$query = "SELECT `name` FROM `table`";
$result = mysql_query($query);
echo "<select name=\"name\">\n";
while( $array = mysql_fetch_assoc($result) ) {
     echo "<option value=\"{$array['name']}\">{$array['name']}</option>\n";
}
echo'</select>';

 

Then in the script that processes that, use the value of $_POST['name'] in the query string

$name = mysql_real_escape_string($_POST['name']);
$query = "SELECT `field1`, field2`, `field3` FROM `table` WHERE `name` = '$name'";

 

Thanks Pikachu. Unfortunately I cannot get this script to work at all. It's as if no value is being assigned to 'name'....

 

Here is the code for both .php pages:

 

<?php

include 'config.php';
include 'opendb.php';

$query = "SELECT `name` FROM `goals`";
$result = mysql_query($query);
echo "<select name=\"name\">\n";

while( $array = mysql_fetch_assoc($result) ) {echo "<option value=\"{$array['name']}\">{$array['name']}</option>\n";}

echo'</select>';


?>

 

<?php

$output = $_POST["name"];

echo $output;

?>

 

Where am I going wrong?

Thank you Dan. This is working exactly the way I want it now. Was going out my mind there!

 

I think I went wrong where I was trying to avoid mixing HTML and PHP (for the form tag and submit button) - all this back slashes and " confused the noob :/

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.