Jump to content

Populate Select Options via MSSQL


FrankieGee
Go to solution Solved by 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
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.

Edited by Psycho
Link to comment
Share on other sites

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.

 
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.