kemper Posted December 17, 2006 Share Posted December 17, 2006 I want to be able to display results of a certain variable. My variable would be a field containing a certain result. Since two of my fields contain preset results from a dropdown menu, the choices are simple.If my table consists of:[table border="0" cellpadding="0" style="border-collapse: collapse" width="100%" id="table1"] [table][tr] [td][b]age[/b][/td] [td][b]division[/b][/td] [td][b]team[/b][/td] [/tr] [tr] [td]7[/td] [td]A[/td] [td]Team 1[/td] [/tr] [tr] [td]8[/td] [td]A[/td] [td]Team 2[/td] [/tr] [tr] [td]8[/td] [td]B[/td] [td]Team 3[/td] [/tr] [tr] [td]7[/td] [td]A[/td] [td]Team 4[/td] [/tr] [tr] [td]9[/td] [td]A[/td] [td]Team 5[/td] [/tr] [tr] [td]8[/td] [td]A[/td] [td]Team6[/td] [/tr][/table]How would I be able to allow my visitors to select to see all content of rows with 7 in "age" field of table?My overall intentions are to have an option to have visitor select which "age" and show content of the selected "age" via selection from dropdown menu or something to that effect.Assistance is greatly appreciated! Quote Link to comment Share on other sites More sharing options...
mlin Posted December 17, 2006 Share Posted December 17, 2006 Ok, you'll have to base your query off of the user input.Your first query to show all the results in your table show's all ages. Add another query to create your drop-down menu such as: $q = mysql_query("select distinct age from table_name");that'll give you 7,8, and 9 only once.then, when a user submits their age selection, base your new query from that lika so:[code]if (isset($_GET['age'])) { $age = (int)$_GET['age']; $query = msyql_query("select * from table_name where age='$age'");}[/code]Then you can loop through your results as normal Quote Link to comment Share on other sites More sharing options...
kemper Posted December 18, 2006 Author Share Posted December 18, 2006 I was told that I could code something like this:[code]<?php include("config.inc.php"); if ($db == 1) { echo "<center><font face='Arial' size='4' color='#000000'><b>Results:</b></font></center><br><br>\n\n"; } if($db==1) { // mySQL Table $db_con = mysql_connect(MyHost, MyUser, MyPassword) or die("Connetion to database failed!"); mysql_select_db(MyDatabase); $age = $_GET['age']; $sql = "SELECT age, division, team FROM MyTable"; $result = mysql_query($query) or die(mysql_error()); $i = 0; echo "<table width='500' border='0'><tr> <th>Age></th><th>Division</th><th>Team</th></tr>";while ($row = mysql_fetch_array($sql) {echo "<tr><td>" . $row['age'] . "</td><td>" . $row['division'] . "</td><td>" . $row['team'] . "</td></tr>";} echo "</table>\n"; } // mySQL ends?>[/code]This got me error:[code]Parse error: syntax error, unexpected '{' in /home/kemper/public_html/MySite/view.php on line 21[/code]Any suggestions? Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2006 Share Posted December 18, 2006 Your missing a )[code=php:0]while ($row = mysql_fetch_array($sql)) {[/code] Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 18, 2006 Share Posted December 18, 2006 Your missing a closing bracket:while ($row = mysql_fetch_array($sql) {should bewhile ($row = mysql_fetch_array($sql)) { Quote Link to comment Share on other sites More sharing options...
kemper Posted December 18, 2006 Author Share Posted December 18, 2006 OK, I fixed that.Now I am getting:"Query was empty"When accessing www.MySite.com/view.php?age=7My table is populated and contains records with 7 in age field.Assistance is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2006 Share Posted December 18, 2006 For starters, there is no WHERE clause in your query so it doesn't matter what you access the page with, it will allways pull all records.Seconds, "Query was empty" does not appear anywhere in your code.Please post the relevent code. Quote Link to comment Share on other sites More sharing options...
kemper Posted December 18, 2006 Author Share Posted December 18, 2006 [code]<?php include("config.inc.php"); if ($db == 1) { echo "<center><font face='Arial' size='4' color='#000000'><b>Results:</b></font></center><br><br>\n\n"; } if($db==1) { // mySQL Table $db_con = mysql_connect(MyHost, MyUser, MyPassword) or die("Connetion to database failed!"); mysql_select_db(MyDatabase); $age = $_GET['age']; $sql = "SELECT age, division, team FROM MyTable"; $result = mysql_query($query) or die(mysql_error()); $i = 0; echo "<table width='500' border='0'><tr> <th>Age></th><th>Division</th><th>Team</th></tr>";while ($row = mysql_fetch_array($sql)) {echo "<tr><td>" . $row['age'] . "</td><td>" . $row['division'] . "</td><td>" . $row['team'] . "</td></tr>";} echo "</table>\n"; } // mySQL ends?>[/code]This code gave me that message. Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2006 Share Posted December 18, 2006 Actually, looking at the code above, you have a few errors and some unneeded code.What is this for?[code=php:0]$i = 0;[/code]In this line, $sql should be $result.[code=php:0]while ($row = mysql_fetch_array($sql) {[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2006 Share Posted December 18, 2006 [quote]This code gave me that message.[/quote]Im afraid not. Quote Link to comment Share on other sites More sharing options...
kemper Posted December 18, 2006 Author Share Posted December 18, 2006 I am new to this as you can tell.I am modifying scripts that I found elsewhere. This all started with a PHPForm. Results of the form drop into MySQL and a data.dat. I was assuming that $1 = 0 was from it and don't know enought about it to remove. No one else told me to remove it or tell what it is.Im afraid so, about the error message. I just checked it again. Copy and pasted from this posting, updating my MySQL info and received that message agin. Quote Link to comment Share on other sites More sharing options...
kemper Posted December 18, 2006 Author Share Posted December 18, 2006 Dammit! I need to take a class in this stuff. Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2006 Share Posted December 18, 2006 That code can not generate that error. It is that simple.If you want to access the pages using something like the address you showed, try this...[code]<?php include("config.inc.php"); if ($db) { echo "<center><font face='Arial' size='4' color='#000000'><b>Results:</b></font></center><br><br>\n\n"; mysql_connect(MyHost, MyUser, MyPassword) or die("Connetion to database failed!"); mysql_select_db(MyDatabase); $age = $_GET['age']; $sql = "SELECT age, division, team FROM MyTable WHERE age = '$age'"; if ($result = mysql_query($query)) { if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($sql); echo "<table width='500' border='0'><tr><th>Age></th><th>Division</th><th>Team</th></tr>"; echo "<tr><td>" . $row['age'] . "</td><td>" . $row['division'] . "</td><td>" . $row['team'] . "</td></tr>"; echo "</table>\n"; } else { echo "No results match $age"; } } else { echo "Query failed: ". mysql_error() .": $sql"; } }?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2006 Share Posted December 18, 2006 [quote]Dammit! I need to take a class in this stuff.[/quote]No such things I should imagine. Everyone with anything to do with php is self taught. It wouldn't hurt to read [url=http://hudzilla.org/phpwiki/index.php?title=Main_Page]this[/url] though. Quote Link to comment Share on other sites More sharing options...
kemper Posted December 18, 2006 Author Share Posted December 18, 2006 I must be mistyping something in my Database configurations, because I am still not getting results, but I am not getting errors either. That is a good thing.I am getting:"No results match 7"When my table is populated and I keep looking at it to make sure my field names are spelled right and not capitalized along with MyHost, MyUser, MyPassword, MyDatabase and MyTable. Quote Link to comment 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.