Jump to content

getting data to list box when fields are atleast two word


jkkenzie

Recommended Posts

i get this error:" Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ']' in E:\wamp\www\AnnayBase\others\admin\index.php on line 766" when i run below code.

 

Others work if the fields are joined.

 

<?php 

$result2 = mysql_query("SELECT DISTINCT `Current Residence`
FROM `members`  
ORDER BY `Current Residence`"); 

echo "<select name='Current Residence'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[Current Residence]'>$nt[Current Residence]</option>";
}
echo str_replace('"', '"', trim($row["Current Residence"]));
echo "</select>";
?>

First tip, always include strings used as array keys in double quotes

 

<?php 

$result2 = mysql_query("SELECT DISTINCT `Current Residence`
FROM `members`  
ORDER BY `Current Residence`"); 

echo "<select name='Current Residence'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[\"Current Residence\"]'>$nt[\"Current Residence\"]</option>";
}
echo str_replace('"', '"', trim($row["Current Residence"]));
echo "</select>";
?>

using

echo "<option value='$nt[\"Current Residence\"]'>$nt[\"Current Residence\"]</option>";

does not work.

 

some code are here below , otherwise they are more than this text area can accommodate:

<td class="hr"><?php echo htmlspecialchars("Division")." " ?></td>
<td class="dr"><?php 

$result2 = mysql_query("SELECT DISTINCT strDivision
FROM divisions  
ORDER BY strDivision"); 

echo "<select name='Division'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[strDivision]'";
if($nt[strDivision] == $strDivision) { echo " selected"; }
echo ">$nt[strDivision]</option>";
}
echo str_replace('"', '"', trim($row["Division"]));
echo "</select>";
?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Location")." " ?></td>
<td class="dr"><?php 

$result2 = mysql_query("SELECT DISTINCT strLocation
FROM locations  
ORDER BY strLocation"); 

echo "<select name='Location'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[strLocation]'";
if($nt[strDivision] == "--- Select Location ---") { echo " selected"; }
echo ">$nt[strLocation]</option>";
}
echo str_replace('"', '"', trim($row["Location"]));
echo "</select>";
?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("SubLocation")." " ?></td>
<td class="dr"><?php 

$result2 = mysql_query("SELECT DISTINCT strSubLocation
FROM sublocations  
ORDER BY strSubLocation"); 

echo "<select name='SubLocation'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[strSubLocation]'";
if($nt[strDivision] == "--- Select Sublocation  ---") { echo " selected"; }
echo ">$nt[strSubLocation]</option>";
}
echo str_replace('"', '"', trim($row["SubLocation"]));
echo "</select>";
?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Country")." " ?></td>
<td class="dr"><?php 

$result2 = mysql_query("SELECT DISTINCT strCountry
FROM countries  
ORDER BY strCountry"); 

echo "<select name='Country'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[strCountry]'";
if($nt[strDivision] == "--- Select Country ---") { echo " selected"; }
echo ">$nt[strCountry]</option>";
}
echo str_replace('"', '"', trim($row["Country"]));
echo "</select>";
?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Country Of Residence")." " ?></td>
<td class="dr"><?php 

$result2 = mysql_query("SELECT DISTINCT strCountryOfResidence
FROM `country of residence`  
ORDER BY strCountryOfResidence"); 

echo "<select name='Country Of Residence'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[strCountryOfResidence]'";
if($nt[strDivision] == "--- Select Country Of Residence ---") { echo " selected"; }
echo ">$nt[strCountryOfResidence]</option>";
}
echo str_replace('"', '"', trim($row["Country Of Residence"]));
echo "</select>";
?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Form Of Payment")." " ?></td>
<td class="dr"><?php 

$result2 = mysql_query("SELECT DISTINCT strFormOfPayments
FROM `form of payment`  
ORDER BY strFormOfPayments"); 

echo "<select name='Form Of Payment'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[strFormOfPayments]'";
if($nt[strDivision] == "--- Select Form of Payment ---") { echo " selected"; }
echo ">$nt[strFormOfPayments]</option>";
}
echo str_replace('"', '"', trim($row["Form Of Payment"]));
echo "</select>";
?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Bank")." " ?></td>
<td class="dr"><?php 

$result2 = mysql_query("SELECT DISTINCT strBanks
FROM `banks`  
ORDER BY strBanks"); 

echo "<select name='Bank'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[strBanks]'";
if($nt[strDivision] == "--- Select Bank ---") { echo " selected"; }
echo ">$nt[strBanks]</option>";
}
echo str_replace('"', '"', trim($row["Bank"]));
echo "</select>";
?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Current Residence")." " ?></td>
<td class="dr"><?php 

$result2 = mysql_query("SELECT DISTINCT `Current Residence`
FROM `members`  
ORDER BY `Current Residence`"); 

echo "<select name='Current Residence'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[Current Residence]'>$nt[Current Residence]</option>";
}
echo str_replace('"', '"', trim($row["Current Residence"]));
echo "</select>";
?></td>
</tr>
</table>
<?php } ?>

The problem is in this line

<?php
echo "<option value='$nt[Current Residence]'>$nt[Current Residence]</option>";
?>

The string "Current Residence" must be quoted if it is used as an index into an array. Use either

<?php
echo '<option value="' . $nt['Current Residence'] . '">' . $nt['Current Residence'] . "</option>";
?>

or

<?php
echo "<option value='{$nt['Current Residence']}'>{$nt['Current Residence']}</option>";
?>

 

Ken

I had to make the field one word by using mysql "AS"  as follows:

$result2 = mysql_query("SELECT DISTINCT `Current Residence` AS strres
FROM `members`  
ORDER BY `Current Residence` "); 

echo "<select name='Current Residence'>";
while($nt=mysql_fetch_array($result2)){
echo "<option value='$nt[strres]'";
if($nt[strDivision] == "--- Select Current Residence ---") { echo " selected"; }
echo ">$nt[strres]</option>";
}

 

Quoting does not work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.