shrestha
shrestha

Reputation: 243

blank value in first row of dropdown box

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

Answers (3)

Presha
Presha

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

Krish R
Krish R

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

Petros Mastrantonas
Petros Mastrantonas

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

Related Questions