Php Drop Down Menu Passing Last Row In Array  | Newbie | | Join Date: Sep 2006 Location: Cleveland, OH
Posts: 18
| |
I have an issue with a drop down menu that I am hoping you all can help me with. The issue is when I press "Submit", my code prints back values from a form I developed (eventually I will write them to a database). In this form, I have a dynamically built drop down (php/ mysql query). No matter which value I select, it always prints the LAST row in the table (array) as the selected value. My code follows:
1) I connect to database
2) I am building a set of forms using a loop statement where the variable '$a' increments by one each time through the loop.
3) I run code to get drop down array -
$TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations");
-
echo "<form action=something.php method=POST>
-
<select name=TVStation[$a]>";
-
while ($r = mysql_fetch_array($TVSQL))
-
{ $TVStation[$a] = $r["TVStation"]; echo "<option
-
value='$TVStation[$a]'>$TVStation[$a]</option>"; }
-
echo "</select></td>";
-
4) The web page for reference is: http://theweekly13.com/test/gamebuilder.php
Thanks for any help.
Cheers!
Seth
| |
best answer - posted by TheServant |
Again, your select name on line 2 is an array. This will not be able to be sent. The name needs to be scalar ("BBC" or "TVStation1") instead of an array like (TVStation[1]).
$_POST does not take variables in their arrays like you have ($_POST['$TVStration[$a]']). It takes the anme of the input that was sent, so if your select was called "TVStation1" (note it is not an array), then to call the value of that you would use $_POST['TVStation1'] (not no $ signs which denote variables).
To replace your code use: - $TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations ORDER BY TVStation ASC");
-
echo "<select name=TVStation".$a."><option value=''>Select TV</option>"; /*** Changed your name to output a scalar rather than an array*/
-
while ($z = mysql_fetch_array($TVSQL))
-
{
-
$TVStation_temp = $z["TVStation"]; /*** Changed your option value variable to a scalar variable rather than an array*/
-
echo "<option value='$TVStation_temp'>$TVStation_temp</option>"; /*** Changed your option value variable to a scalar variable rather than an array (corresponding to previous change)*/
-
}
-
-
echo "</select></td>";
-
echo( $_POST['TVStation'.$a] );/*** Added an echo (if you don't use echo or print, nothing will be displayed), changed name to correspond to new select name in the form of TVStation1*/
-
I have tried to use your code without changing anything more than needs to be changed, but read my comments and make sure you understand why that is the case. Two basic changes are: No array variable names or values, and no variables or arrays within $_POST.
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 919
| | | re: Php Drop Down Menu Passing Last Row In Array
Hi Seth,
I am not sure why you are using an array in your loop, but you have not defined what $a is. Your select name is TVStation[$a] which is not a variable like you have in your while loop (which is $TVStation[$a]?? Is that intentional?
In your while loop, you reset what $TVStation[$a] is every loop, and because $a is not incrimented as you said your 'array' $TVStation is not an array, but rather a single value which will be the last TVStation pulled from MySQL.
To fix this you need:
in your while loop.
How are you printing your selected value? Is it: - echo $_POST['TVStation[??]'];
where ?? is what ever $a's original value is? Give us some more information with how you display that as well us you're updated form code (if you choose to update that) and we can see where the mix up is.
|  | Newbie | | Join Date: Sep 2006 Location: Cleveland, OH
Posts: 18
| | | re: Php Drop Down Menu Passing Last Row In Array
I am using
. Whenever I use $POST_, I lose the values and nothing prints.
I am using
to increment the variable each time the loop runs. Is
a better method to accomplish this?
Here is the entire code in case you are wondering - a lot of this is in-progress and requires clean-up: -
<?php
-
-
//Week Variable
-
-
$Season_Start = mktime(0,0,0,8,30,2009);
-
$Season_End = mktime(0,0,0,12,8,2009);
-
$Current_Date = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
-
-
// Gets session variable for 'Year'
-
// Gets session variable for 'Week'
-
-
$Year = date("Y",$Season_Start);
-
$h = (7*24*60*60);
-
IF ($Current_Date < $Season_Start)
-
{
-
$Week = 1;
-
-
}
-
ELSE
-
{
-
IF ($Current_Date < $Season_End)
-
{
-
$Week = CEIL((($Current_Date-$Season_Start)/$h));
-
}
-
ELSE
-
{
-
$Week = CEIL(($Season_End-$Season_Start))/$h +1;
-
}
-
}
-
-
//Connect to Database
-
-
$dbhost = "XXXXXX";
-
$dbuser = "XXXX";
-
$dbpass = "XXXXXXX";
-
-
function dbConnect($db="") {
-
global $dbhost, $dbuser, $dbpass;
-
-
$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass)
-
or die("The site database appears to be down.");
-
-
if ($db!="" and !@mysql_select_db($db))
-
die("The site database is unavailable.");
-
-
return $dbcnx;
-
}
-
-
dbconnect("theweekl_test");
-
-
?>
-
-
<!doctype html public "-//w3c//dtd html 3.2//en">
-
<html>
-
<head>
-
<title>The Weekly13 :: Gamebuilder</title>
-
-
<!-- Java script for OnChange of League to bring up Gamebuilder -->
-
-
<SCRIPT language=JavaScript>
-
-
function reload_A(form)
-
{
-
var val=form.League.options[form.League.options.selectedIndex].value;
-
self.location='gamebuilder.php?League=' + val ;
-
}
-
-
function reload_C(form)
-
{
-
var val=form.League.options[form.League.options.selectedIndex].value;
-
var val2=form.GameCount.options[form.GameCount.options.selectedIndex].value;
-
-
self.location='gamebuilder.php?League=' + val + '&GameCount=' + val2 ;
-
}
-
-
</script>
-
-
<style type="text/css">
-
-
p1 {
-
font-family: eurostile, arial, verdana;
-
font-size: 22px;
-
border: 4px solid #cd0000;
-
text-align: center;
-
color: #000066;
-
font-weight: 900;
-
}
-
-
</style>
-
-
</head>
-
-
<body
-
style="background-image: url(http://theweekly13.com/images/core/pabstheaderbg.jpg);">
-
-
<?php //header
-
$curl = curl_init();
-
curl_setopt ($curl, CURLOPT_URL, "http://theweekly13.com/include/header.php");
-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
-
$result = curl_exec ($curl);
-
curl_close ($curl);
-
echo $result;
-
-
echo "<br><br>";
-
-
$WeekIDSQL = mysql_query("SELECT WeekID FROM NCF_weeks WHERE Week = $Week");
-
$WeekID = mysql_result($WeekIDSQL, 0);
-
-
@$League=$_GET['League'];
-
-
/* Data for League DropDown Box */
-
-
$queryUL = mysql_query("SELECT League, LeagueID FROM NCF_leagues
-
ORDER BY LeagueID ASC");
-
-
echo "<form method=POST name=League_GameCount action=' '>";
-
-
/* First Drop down Menu */
-
-
echo "<table
-
style=\"width: 500px; text-align: left; margin-left: auto; margin-right: auto;\"
-
border=\"0\" cellpadding=\"4\" cellspacing=\"4\"><tbody><tr><td style=\"width: 250px; text-align: right;\">
-
<p>Select League:</p></td><td style=\"width: 237px;\">";
-
-
echo "<select name='League' onchange=\"reload_A(this.form)\"> <option value=''><p>Select League</p></option>";
-
-
while($r = mysql_fetch_array($queryUL)) {
-
if($r['League']==@$League){
-
echo "<option selected value='$r[League]'>$r[League]</option>"."<BR>";}
-
else{echo "<option value='$r[League]'>$r[League]</option>";}
-
}
-
echo "</select></td></tr>";
-
-
$_SESSION['$League'] = $League;
-
-
$queryGC = mysql_query("SELECT GameCount, GameCountID FROM NCF_gamecount ORDER BY gamecountID ASC");
-
-
/* Second Drop down Menu */
-
-
echo "<tr><td style=\"text-align: right;\"><p>Select Game Count:</p></td>
-
<td><select name='GameCount' onchange=\"reload_C(this.form)\"><option value=''><p>Change Game Count</p></option>";
-
while($t = mysql_fetch_array($queryGC)) {
-
echo "<option value='$t[GameCount]'>$t[GameCount]</option>";
-
}
-
-
$_SESSION['$GameCount'] = $GameCount;
-
-
echo "</select></td></tr></tbody></table>";
-
echo "<hr style=\"width: 800px; height: 4px; text-align: center;\">";
-
echo "</form>";
-
-
/* ONCE GAMECOUNT IS SELECTED, GAME INFORMATION BOXES APPEAR */
-
-
if(isset($GameCount) )
-
{
-
-
echo "<form method=POST name=Gamebuilder action=''>";
-
-
echo "<table style=\"background-color: #FFFF99; width: 800px; text-align: left;
-
margin-left: auto; margin-right: auto;\"
-
border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>
-
<td style=\"text-align: left; \"><p1>  You are building ".$GameCount.' Games for the
-
'.$League.' league for Week '.$Week." </p1></td></tr></tbody></table><br>";
-
-
$a = 1;
-
WHILE ($a <= $GameCount) {
-
-
IF ($a < 10) {$GameNum = "0".$a; } else {$GameNum = $a;}
-
-
$LeagueNumSQL = mysql_query("SELECT LeagueNumber FROM NCF_leagues WHERE League LIKE '$League'");
-
$LeagueNum = mysql_result($LeagueNumSQL, 0);
-
-
$GameID[$a] = $Year.$WeekID.$GameNum.$LeagueNum;
-
-
echo "<table style=\"width: 800px; text-align: left; margin-left: auto; margin-right: auto;\"
-
border=\"3\" cellpadding=\"1\" cellspacing=\"1\">
-
<tbody><tr><td style=\"background-color: rgb(255, 255, 153);\" colspan=\"6\" rowspan=\"1\"><p>
-
Game ".$a." :: Game ID ".$GameID[$a]."</p></td></tr>";
-
-
echo "<tr><td style=\"width: 289px; text-align: center;\" colspan=\"2\" rowspan=\"1\">Date/
-
Time: <input size=\"25\" name=\"DateTimes[$a]\" value=\"YYYY-MM-DD HH:MM:SS\"></td>";
-
-
echo "<td style=\"width: 100px; text-align: center;\">";
-
-
$TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations");
-
echo "<form action=something.php method=POST> <select name=$TVStation[$a]>";
-
while ($r = mysql_fetch_array($TVSQL))
-
{ $TVStation[$a] = $r["TVStation"]; echo "<option value='$TVStation[$a]'>$TVStation[$a]</option>"; }
-
echo "</select></td>";
-
-
echo "<td style=\"width: 389px;\" colspan=\"3\" rowspan=\"1\">
-
GameNotes: <input size=\"45\" name=\"GameNotes".$a."\"></td>
-
</tr><tr><td colspan=\"3\" rowspan=\"1\">Away Team Information</td><td colspan=\"3\" rowspan=\"1\">Home Team
-
Information</td></tr><tr><td style=\"text-align: right; width: 75px;\"># <input
-
size=\"5\" name=\"AwayRank".$a."\">.</td>";
-
-
echo "<td style=\"width: 189px; text-align: center;\">";
-
-
$AwayTeamSQL = mysql_query("SELECT College FROM NCF_teams");
-
echo "<form action=something.php method=POST><select name=AwayTeam>";
-
while ($r = mysql_fetch_array($AwayTeamSQL))
-
{ $AwayTeam = $r["College"]; echo "<option value='$AwayTeam'>$AwayTeam</option>"; }
-
echo "</select></td>";
-
-
echo "<td style=\"width: 100px;\">Line: <input size=\"5\" name=\"AwayLine".$a."\" value=\"0.0\"></td>
-
<td style=\"text-align: right; width: 75px;\"># <input size=\"5\" name=\"HomeRank".$a."\">.</td>";
-
-
echo "<td style=\"width: 189px; text-align: center;\">";
-
-
$HomeTeamSQL = mysql_query("SELECT College FROM NCF_teams");
-
echo "<form action=something.php method=POST><select name=HomeTeam>";
-
while ($r = mysql_fetch_array($HomeTeamSQL))
-
{ $HomeTeam = $r["College"]; echo "<option value='$HomeTeam'>$HomeTeam</option>"; }
-
echo "</select></td>";
-
-
echo "</td><td style=\"width: 100px;\">Line: <input size=\"5\" name=\"HomeLine".$a."\" value=\"0.0\"></td>
-
</tr><tr><td style=\"width: 389px;\" colspan=\"3\" rowspan=\"1\">
-
Record: <input size=\"4\" name=\"AwayOAW".$a."\">-<input size=\"4\" name=\"AwayOAL".$a."
-
\">, <input size=\"4\" name=\"AwayCW".$a."\">-<input size=\"4\" name=\"AwayCL".$a."\">
-
</td><td style=\"width: 389px;\" colspan=\"3\" rowspan=\"1\">Record: <input size=\"4\"
-
name=\"HomeOAW".$a."\">-<input size=\"4\" name=\"HomeOAL".$a."\">, <input size=\"4\"
-
name=\"HomeCW".$a."\">-<input size=\"4\" name=\"HomeCL".$a."\"></td></tr></tbody></table><br>";
-
-
$GameInsert = "INSERT INTO NCF_games (GameID, Game, WeekID, League, Year, DateTimes, Awayteam, AwayLine,
-
HomeTeam, HomeLine, GameNotes, GameTV, Notes) VALUES ($GameID, $a, $WeekID, $LeagueNum, $Year, $DateTimes.$a,
-
$AwayTeam, $AwayLine, $HomeTeam, $HomeLine, $GameNotes, $TVStation, $CurrentDate)";
-
-
$GameInfoInsert = "INSERT INTO NCF_gameinfo (GameID, AwayTeam, AwayRank, AwayOAW, AwayOAL, AwayCW, AwayCL,
-
HomeTeam, HomeRank, HomeOAW, HomeOAL, HomeCW, HomeCL, Notes) VALUES ($GameID, $AwayTeam, $AwayRank,
-
$AwayOAW, $AwayOAL, $AwayCW, $AwayCL, $HomeTeam, $HomeRank, $HomeOAW, $HomeOAL, $HomeCW,
-
$HomeCL, $CurrentDate)";
-
-
$a = $a + 1;
-
}
-
-
echo "<div style=\"text-align: center;\"><input value=\"Reset Form\" type=\"reset\"> <input
-
name=\"submitok\" value=\"Submit\" type=\"submit\"></div></form> ";
-
-
$UserCountSQL = mysql_query("SELECT COUNT(UserName) FROM NCF_userstemp
-
WHERE UserLeague = $LeagueNum AND UserName NOT LIKE '%SELECT%'");
-
$UserCount = mysql_result($UserCountSQL, 0);
-
-
$UserSelectSQL = mysql_query("SELECT UserName FROM NCF_userstemp
-
WHERE UserLeague = $LeagueNum AND UserName NOT LIKE '%SELECT%'");
-
-
$UserIDSelectSQL = mysql_query("SELECT UserID FROM NCF_userstemp
-
WHERE UserLeague = $LeagueNum AND UserName NOT LIKE '%SELECT%'");
-
-
-
if (isset($_POST['submitok'])) {
-
-
$b = 1;
-
WHILE ($b <= $GameCount) {
-
-
echo $GameID[$b]." ".$DateTimes[$b]." ".$TVStation[$b]."<br>";
-
$b = $b +1;
-
-
}
-
-
}
-
}
-
else { echo "<br><br><div style=\"text-align: center;\"><p1>Enter Game Count & Select League</p1></div>"; }
-
-
?>
-
-
</body>
-
</html>
-
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 919
| | | re: Php Drop Down Menu Passing Last Row In Array
I won't read all your code, but I think I see your problem.
As explained before in your while loop when you have: - while ($r = mysql_fetch_array($TVSQL))
-
{ $TVStation[$a] = $r["TVStation"]; echo "<option...
You are resetting $TVStation[$a] and overwriting it every time, so when you echo $TVStation[$a] you will end up with the last $r["TVStation"] that you set it at, which coincides with the last item on your select dropdown. It is not your dropdown that's failing.
The problem is you can't $_POST an array without serializing first, which basically turns the array into a string which can be unserialized (turned back into an array) after the $_POST data has been sent and received. You can read about it here or from any Google search about it.
If you do not use $_POST, you are not looking at any data from the form, but only that which you have in the PHP of the current page. If that doesn't make sense, you should read up on how forms are submitted and data is retrieved, and then the difference between $_POST and $_GET which will help you a lot.
I don't think that you need to be serializing, but instead, simply not to have your select name as an array... In other words, don't send arrays in $_POST.
And yes, $a++; is valid and faster, if you simply want an increment of 1.
|  | Newbie | | Join Date: Sep 2006 Location: Cleveland, OH
Posts: 18
| | | re: Php Drop Down Menu Passing Last Row In Array
I am still having trouble getting what's going on with the code I use. I use the exact same coding logic twice before on the same page but outside of the loop statement without issue.
I am using the loop statement because I am building multiple events; quantity determined by the user. The TV Station is a part of the event (the channel that the game is on), which can change for each event.
In your first response, you say: Quote:
In your while loop, you reset what $TVStation[$a] is every loop, and because $a is not incremented as you said your 'array' $TVStation is not an array, but rather a single value which will be the last TVStation pulled from MySQL.
. I increment $a at the end of the loop. Also I don't see how I am resetting $TVStation[$a] When I view page source, I get the variables "TVStation[1]", "TVStation[2]", etc. However when I echo $TVStation[$a]; I always get the last row of the table, as if it is preselected for me.
As always, thanks for your help and patience. I am a hobbyist programmer, so most of my code is cobbled together from what I read on the net or get from sites like this one.
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 919
| | | re: Php Drop Down Menu Passing Last Row In Array
You are quoting me from my first post which was a response to your first where you had no mention of your incrementing of $a, but you included that part in your second post which I acknoledged in my second post, and simply pointed out another incrementing technique available to PHP (and some other languages): - $a++; /* instead of */ $a = $a + 1;
On line 189 in the code above you are setting a value to $TVStation[$a]. Everytime you set a value you overwrite the previous value and so the last value which $TVStation[$a] is set at will be the last record from the database. That is why when you echo it you get the last row.
If you want to submit the form and use what was submitted (for example with an echo), you need to use $_POST or $_GET. These do not take arrays as variables and so the bottom line is: You cannot use an array as a form element name*.
* Forget what I mentioned before about serializing, I can't think of a way to use that in a form which is $_POSTed.
Hope that helps.
|  | Newbie | | Join Date: Sep 2006 Location: Cleveland, OH
Posts: 18
| | | re: Php Drop Down Menu Passing Last Row In Array
I apologize for confusing the two quotes.
I changed the code to this -
$TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations ORDER BY TVStation ASC");
-
echo "<select name=TVStation[$a]><option value=''>Select TV</option>";
-
WHILE ($z = mysql_fetch_array($TVSQL))
-
{
-
$TVStation[$a] = $z["TVStation"];
-
echo "<option value='$TVStation[$a]'>$TVStation[$a]</option>";
-
}
-
-
echo "</select></td>";
-
$_POST['$TVStation[$a]'];
-
It still does not work. I tried about 50 different flavors, including just using a plain old HTML select option list and could not solve the issue. I am at a loss of how to accomplish this, maybe I can find a way without looping - which will be a real pain, but it's doable.
Thanks for your help.
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 919
| | | re: Php Drop Down Menu Passing Last Row In Array
Again, your select name on line 2 is an array. This will not be able to be sent. The name needs to be scalar ("BBC" or "TVStation1") instead of an array like (TVStation[1]).
$_POST does not take variables in their arrays like you have ($_POST['$TVStration[$a]']). It takes the anme of the input that was sent, so if your select was called "TVStation1" (note it is not an array), then to call the value of that you would use $_POST['TVStation1'] (not no $ signs which denote variables).
To replace your code use: - $TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations ORDER BY TVStation ASC");
-
echo "<select name=TVStation".$a."><option value=''>Select TV</option>"; /*** Changed your name to output a scalar rather than an array*/
-
while ($z = mysql_fetch_array($TVSQL))
-
{
-
$TVStation_temp = $z["TVStation"]; /*** Changed your option value variable to a scalar variable rather than an array*/
-
echo "<option value='$TVStation_temp'>$TVStation_temp</option>"; /*** Changed your option value variable to a scalar variable rather than an array (corresponding to previous change)*/
-
}
-
-
echo "</select></td>";
-
echo( $_POST['TVStation'.$a] );/*** Added an echo (if you don't use echo or print, nothing will be displayed), changed name to correspond to new select name in the form of TVStation1*/
-
I have tried to use your code without changing anything more than needs to be changed, but read my comments and make sure you understand why that is the case. Two basic changes are: No array variable names or values, and no variables or arrays within $_POST.
|  | Newbie | | Join Date: Sep 2006 Location: Cleveland, OH
Posts: 18
| | | re: Php Drop Down Menu Passing Last Row In Array
Okay, so this is starting to work. The $TVStation variable is echoing back within the loop - where you have - echo( $_POST['TVStation'.$a] );
Thanks for your help. This is a big step forward.
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 919
| | | re: Php Drop Down Menu Passing Last Row In Array
No worries, let me know how you go. Once you understand what form of input each function/procedure takes, it will come naturally, and within a month or two, you will look back at this script you're writing and re-do the whole thing more efficiently.
|  | Newbie | | Join Date: Sep 2006 Location: Cleveland, OH
Posts: 18
| | | re: Php Drop Down Menu Passing Last Row In Array
I got this to work in its entirety today. My biggest confusion was that I did not know $Variable[#] was an array! Now that I know that, what you were saying makes perfect sense.
You're right, I will go through the code and do some massive cleaning up. This is actually my second time through. The first time was all hard coded tables without looping. It was arduous to deal with.
Thanks again for your help.
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 919
| | | re: Php Drop Down Menu Passing Last Row In Array
Glad you have it. Hope to see you around Bytes more often, I'm sure we can help speed your understanding and get you teaching others soon ;)
|  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,576 network members.
|