--- j0hncage <[EMAIL PROTECTED]> wrote:
> I have a table with a column named 'vendname'. This column isn't
> completely populated so as I query the column within the table to
> populate a pull down on a query page, there are many white spaces. My
> intent is to eventually have all of those empty table cells populated
> but until then, would really like to eliminate the white spaces and
> hoped I might get some ideas here. As it currently stands, I have
> approximately 50 rows with 15 'vendname' rows populated and a whole
> bunch of white space that shows up on the pull down query.
>
> My code is:
>
> <?php
> $hostName = "localhost";
> $userName = "####";
> $password = "########";
> $dbName = "approvals";
> $table = "approvals_tbl";
>
> mysql_connect($hostName, $userName, $password) or die("Unable to
> connect to host $hostName");
>
> mysql_select_db($dbName) or die("Unable to select database $dbName");
>
> $query = "SELECT DISTINCT vendname
> FROM $table";
> $result = mysql_query($query);
>
> $number = mysql_numrows($result);
>
> for ($i=0; $i<$number; $i++) {
> $vendname = mysql_result($result,$i,"vendname");
> print "<option value=\"$vendname\">$vendname</option>";
> }
>
> mysql_close();
> ?>
>
> any help is greatly appreciated.
>
> John
Instead of iterating through each number from 1 to the maximum number of rows,
why not use a while() loop.
while ($row_data = mysql_fetch_assoc($result))
{
$vendname = trim($row_data['vendname']);
# check to see if the value is not empty
if ($vendname)
{
printf("<option value=\"%s\">%s</option>", $vendname, $vendname);
}
}
Also, if you want your vendor names to be unique and alphabetical (perhaps you
don't) you can change your query to:
SELECT vendname FROM $table GROUP BY vendname
This will select unique values from the vendname column and then sort them
alphabetically.
James