Jump to content

Populate Select Options via MSSQL


FrankieGee

Recommended Posts

I am trying to figure this out.

 

When I run the below code, I DO get spaces inside my <select><option></option></select> code, but I can not see anything but blank spaces.

echo "<table border ='1' width='90%'><tr><td>";
			echo "<select name=\"test\">";
			
				$sql="select csname from customer
				where sacode = $sacode";
				
				$rs=odbc_exec($conn,$sql);
				if (!$rs)
  					echo"Error in SQL";

				while($row=odbc_fetch_row($rs))
				{
				echo "<option value=.$row[csname].</option>";
				}
		echo "</select>";	
		echo "</td></tr>";
		odbc_close($conn);
		echo "</table>";

When I run the same code to output into a table, everything is fine.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/278865-populate-select-options-via-mssql/
Share on other sites

 

try

echo "<option value=\"{$row['csname']}\">{$row['csname']}</option>";

 

To elaborate on what Barand provided, the problem was that the code was creating invalid HTML code. If the value from the DB was 'FOO' the option tag would have been created like this

<option value=.FOO.</option>

Instead it should be like this:

<option value="FOO">FOO</option>

which is what Barand's snippet will resolve. But, I would add that I would add a \n at the end of every line of HTML code (within double quotes) so it will put line-breaks in the HTML output. Otherwise, it becomes very difficult to debug the HTML.

Thank you Barand and Phyco.

I have edited the script to reflect the proposed changes and I am still unable to actually see the results. Again, there is a 'placeholder' if you will, where the data should be.

I have counted 102 rows inside the database for this user ($sacode =26) and 102 rows for the Select function.

 

Looking at this with fresh eyes, I was able to gain the results I needed.

echo "<table border ='1' width='90%'><tr><td>";
echo "<select name=\"test\">";
echo "<option>Select Company</option>";
$sql="select csname from customer
where sacode = $sacode
order by csname";		
$rs=odbc_exec($conn,$sql);
if (!$rs)
echo"Error in SQL";

while(odbc_fetch_row($rs))
{
$csname=odbc_result($rs,"csname");
echo "<option value=\"$csname\">$csname</option>";
}
echo "</select>";
echo "</td></tr>";
odbc_close($conn);
echo "</table>";

Thanks everyone! I am sure to have more questions as this project progresses forward.

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.