Reputation: 243
I generated the data in dropdown box from postgresql database in php. The data is generated with empty first row in dropdown box and remaining row gives required data. I want to remove that empty first row. My code is like this
<select id="dzongkhag_id">
<option>Dzongkhag<option/>
<?php
$con_string= "host=localhost port=5432 dbname=bhutan_shp user=postgres password=rabi";
$db_con = pg_connect($con_string);
$result=pg_query($db_con,"SELECT * FROM dzongkhag");
while ($row = pg_fetch_array($result))
{echo"<option value={$row['dzgname']}>{$row['dzgname']}</option>";}
?>
Upvotes: 2
Views: 1095
Reputation: 104
You have an error defining html
In line Dzongkhag, you didn't close the option tag properly. That might be causing error.
Upvotes: 1
Reputation: 22741
You can filter your non-empty values in your query itself. Try this,
$result=pg_query($db_con,"SELECT * FROM dzongkhag where (`dzgname`!='' OR `dzgname` is not null ");
Upvotes: 0
Reputation: 1046
if I understand correctly, you dont want to show the first array item, or empty arrays in general, right? so change this code:
while ($row = pg_fetch_array($result))
{echo"<option value={$row['dzgname']}>{$row['dzgname']}</option>";}
to
while ($row = pg_fetch_array($result))
{
if (empty($row)) continue;
echo"<option value={$row['dzgname']}>{$row['dzgname']}</option>";
}
Upvotes: 0