Reputation: 151
I have some pretty simple code that is not doing what I want it to do. I just want it to take the unique numbers of the year category in my table and enter them into a dropdown box, but with this code I only get one option which says: $row['year']
Any help is appreciated.
<!DOCTYPE html>
<html>
<form name="testform" method='POST' action='mainck.php'>
<?Php
require "config.php";
echo "<br>Select year first <select name=year id ='s1' onchange=AjaxFunction();>
<option value=>Select a Year</option>";
$sql="SELECT DISTINCT year from PlayerRegSeason";
foreach ($dbo->query(&sql) as $row) {
echo "<option value=$row['year']>$row['year']</option>";
}
?>
</select>
</form>
</html>
Upvotes: 0
Views: 555
Reputation: 126
As Ben said, some changes would make it more likely to work. But I would keep things that don't need PHP out of it. Be sure to use the HTML properly (it won't solve this problem, but it's a good practice, tags like HEAD and BODY).
If that's still not working, make sure to check your query directly on phpMyAdmin or your MySQL client. Another thing that I usually do when I'm stuck in situations like this is print_r() my return.
PS.: Check again in your foreach($dbo->query(&sql) as $row){ if the & is the sign that you want. (I'm not exactly sure what a & sign would do there, so my bad if I'm wrong.)
I changed some stuff in your code, check it out:
<!DOCTYPE html>
<html>
<body>
<form name="testform" method='POST' action='mainck.php'>
<label for="s1">Select year first</label>
<select name='year' id ='s1' onchange='AjaxFunction();'>
<option value=''>Select a Year</option>
<?php
require "config.php";
echo "";
$sql="SELECT DISTINCT year from PlayerRegSeason";
$return = $dbo->query($sql);
print_r($return);
foreach ($return as $row) {
echo "<option value='".$row['year']."'>".$row['year']."</option>";
}
?>
</select>
</form>
</body>
</html>
Upvotes: 0
Reputation: 731
Missing a lot of quotes in your markup. It's better to be consistent about double vs single quotes, but the following should at least work:
<!DOCTYPE html>
<html>
<form name="testform" method='POST' action='mainck.php'>
<?php
require "config.php";
echo "<br>Select year first <select name='year' id='s1' onchange='AjaxFunction();'>
<option value=''>Select a Year</option>";
$sql="SELECT DISTINCT year from PlayerRegSeason";
foreach ($dbo->query($sql) as $row) {
echo "<option value='".$row['year']."'>".$row['year']."</option>";
}
?>
</select>
</form>
</html>
Upvotes: 1