Forum Moderators: coopster
I am having a problem with searching custom fields in deltascripts php classifieds.
When field type is defined as a drop-down menu, I can get the options, which are stored in a .txt file to display on the search page, but the search always turns up empty.
Here is the code for the search field:
==============
if ($varname<>"custom_field_1")
{
print("<tr>");
print("<td width=\"50%\" valign=\"top\"> $ad_caption </td>");
print("<td width=\"50%\" valign=\"top\">");
print("<input type=\"text\" name=\"$varname\" size=\"$fieldsize\" class='txt' value=\"$value\" />");
print("</td>");
print("</tr>");
}
if ($varname == "custom_field_1")
{
$options = file("options/locations.txt");
$num_options = count($options);
print("<tr>");
print("<td width=\"50%\" valign=\"top\"> $ad_caption </td>");
print("<td width=\"50%\" valign=\"top\">");
print "<select size='1' name='$varname'>";
print "<option selected>$options[$i]";
for ($i=0; $i<$num_options; $i++)
{
print "<option value='$options[$i]' ";
print ">$options[$i]</option>";
}
print "</select> ";
==============
I did a view source on the search page output and it looks like this:
==============
<select size='1' name='custom_field_1'><option selected><option value='az
' >az
</option><option value='ar
' >ar
</option><option value='ak
' >ak
</option><option value='tx
' >tx
</option></select>
===============
So I figured that since the txt file uses line breaks between options, that was tripping up the search, so I tried to trim the break:
===============
$options1 = trim($options, " \n. " );
for ($i=0; $i<$num_options; $i++)
{
print "<option value='$options1[$i]' ";
print ">$options1[$i]</option>";
}
===============
but then the options in the dropdown just vertically spelled ARRAY.
Any thoughts? Am I even on the right track? I am a php newbie so please be gentle w/me :)
Should I post the whole contents of search.php (400 lines)?
Thanks for your help!
The problem could stem from these two lines:
print "<option value='$options[$i]' ";
print ">$options[$i]</option>";
There are a few ways to print to a web page, but I suggest you stick with one method, just to keep it organized.
Here is a nice method, called heredoc [php.net], where you don't need to escape quotes:
if ($varname<>"custom_field_1")
{
print <<<HTML
<tr>
<td width="50%" valign="top"> $ad_caption </td>
<td width="50%" valign="top">
<input type="text" name="$varname" size="$fieldsize" class="txt" value="$value" />
</td>
</tr>
HTML;
}
if ($varname == "custom_field_1")
{
$options = file("options/locations.txt");
$num_options = count($options);
print <<<HTML
<tr>
<td width="50%" valign="top"> $ad_caption </td>
<td width="50%" valign="top">
<select size="1" name="$varname">
<option selected>$options[$i]</option>
HTML;
for ($i=0; $i<$num_options; $i++)
{
print "<option value=\"$options[$i]\">$options[$i]</option>";
}
print "</select> ";
Also, you never closed the first <option selected> tag in your original.
Hopefully, that will get you going in the right direction.
Birdman