php - Using a variable in an array, when variable is pulled from an sql table
Get the solution ↓↓↓Solution:
$state is not in scope, alter to
function state_selection($state){
And call like
state_selection($state);
A more reliable method instead of prepending would be
'<option value="'.$state_name.'" '.($state_name==$state)?'selected':'').'>'
Answer
Solution:
You cannot access the variable $state in the function without declaring it as global. Add this line to the beginning ofstate_selection()
global $state;
Another option would be to pass in$state
as a parameter ofstate_selection()
Answer
Solution:
Here is some updated code. A few key notes:
- You do not need to loop if you are only pulling 1 record.
- The array inside the function is unnecessary. I do not like globals, so I set it to pass as a parameter.
- Added a default parameter, this will set the state selected to that.
- Removed the assigning the array values to a variable on it's own. This is not necessary and inefficient. Better to access them through the array.
Any questions let me know.
function state_selection($states, $default) {
$str = '<select name="state2">';
foreach ($states as $state_name) {
$str .= '<option value="'.$state_name.'" ' . (($default == $state_name)?"selected":"") . '>'.$state_name.'</option>';
}
$str .= '</select>';
return $str;
}
$states = array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois",
"Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada",
"New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota",
"Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");
$row = mysql_fetch_assoc($result);
$editBlock = '<form method="post" action="./sadd.php">
<p>First Name: <input name="first2" value="' . $row['first'] . '" type="text" size=13 maxlength=25 /></p>
<p>Last name: <input name="last2" value="' . $row['last'] . '" type="text" size=13 maxlength=25 /></p>
<p>City:<input name="city2" value="' . $row['city'] . '" type="text" size=25 maxlength=50 /></p>
<p>State: ' . state_select($states, $row['state']) . ' </p>
<p>Email:<input name="email2" value="' . $row['email'] . '" type="text" size=50 maxlength=75 /></p>
<p>Birthday:<input name="bday2" value="' . $row['bday'] . '" type="text" size=10 maxlength=10 />(ex 1982-06-26)</p>
<p><input type="hidden" name="eb" value="ebf" /></p>
<p><input type="submit" name="submit" value="Update Contact" /></p>
</form>';
echo $editBlock;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: object not found by the @paramconverter annotation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.