theslever Posted November 28, 2010 Share Posted November 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/220087-just-another-noobish-question/ Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 Use a form to submit the criteria, and a variable from that form in the query string as the value to search for. Quote Link to comment https://forums.phpfreaks.com/topic/220087-just-another-noobish-question/#findComment-1140713 Share on other sites More sharing options...
theslever Posted November 28, 2010 Author Share Posted November 28, 2010 That makes a lot of sense and now I feel a little foolish, although it has been a long day SO are we talking something liiiiiiiiike...... $result = mysql_query("SELECT * FROM Persons WHERE $ChosenColumn='$ChosenEntry'"); Am I on the right tracks? Quote Link to comment https://forums.phpfreaks.com/topic/220087-just-another-noobish-question/#findComment-1140714 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 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'"; Quote Link to comment https://forums.phpfreaks.com/topic/220087-just-another-noobish-question/#findComment-1140717 Share on other sites More sharing options...
theslever Posted November 29, 2010 Author Share Posted November 29, 2010 Thanks Pikachu. I'm at work at the moment but will look at this in greater detail tonight so thanks once again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/220087-just-another-noobish-question/#findComment-1140815 Share on other sites More sharing options...
theslever Posted December 2, 2010 Author Share Posted December 2, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/220087-just-another-noobish-question/#findComment-1142386 Share on other sites More sharing options...
ManiacDan Posted December 2, 2010 Share Posted December 2, 2010 You have no form tag or submit button, do you have those? Quote Link to comment https://forums.phpfreaks.com/topic/220087-just-another-noobish-question/#findComment-1142393 Share on other sites More sharing options...
theslever Posted December 2, 2010 Author Share Posted December 2, 2010 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 :/ Quote Link to comment https://forums.phpfreaks.com/topic/220087-just-another-noobish-question/#findComment-1142397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.