当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to eliminate a combo box from a form

问题描述:

I have the following form with a " select " tag :

 <form action="upload.php" enctype="multipart/form-data" method="post">

<br>Filename:&nbsp;<input name="userfile" type="file">

<p>Customer:&nbsp;

<select name="customer">

<?php

include 'data.dat';

include 'error.inc';

// Connect to the database

if (!($connection = @ mysql_connect ($hostName,

$username,

$password)))

die ("Could not connect to database");

// Select the database

if (!mysql_select_db ($databaseName, $connection))

showerror();

// Display all the available customers in order

$query = "SELECT customer FROM custlist order by id";

if (!($result = @ mysql_query ($query, $connection)))

showerror();

$i = 0;

// Display each customer in a drop down menu.

while ($row = @ mysql_fetch_array ($result))

{

$i++;

if ($i == 1)

echo "<option value=" . $i . " selected>" . $row['customer'] . "\n";

else

echo "<option value=" . $i . ">" . $row['customer'] . "\n";

//

}

//

?>

</select>

<input type="submit" value="Upload file">

</form>

As the database has only 1 row with only one customer i wish to eliminate the combo box and select automatically the only one customer. How can i do this? I'm really a newbie and after a lot of modifies i still not found the solution.

Thanks for your help.

Mat

UPDATES

Thanks to some suggestion i'm working on this modifies... still doesn't work but i think is the right way, isn't it?

Here is the code:

 <select name="customer">

<?php

include 'data.dat';

include 'error.inc';

// Connect to the database

if (!($connection = @ mysql_connect ($hostName,

$username,

$password)))

die ("Could not connect to database");

// Select the log database

if (!mysql_select_db ($databaseName, $connection))

showerror();

// Display all the available customers in order

$query = "SELECT customer FROM curlist order by id";

$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result) or die(mysql_error());

echo $row['customer'];

?>

</select>

网友答案:

As per suggestion, am posting my stab at it as an answer:

$count = mysql_num_rows($result);

if($count == 1) {
    $row = mysql_fetch_assoc($result);
    echo '<input type="text" value="' . htmlentities($row['customer']) . '" readonly />';
    echo '<input type="hidden" name="customer" value="..." />';
} else {
    echo '<select name="customer">';
    while($row = mysql_fetch_assoc($result)) {
        echo '<option value="...">' . htmlentities($row['customer']) . '</option>';
    }
    echo '</select>';
}

The ... in the value would ideally be some kind of primary key in the table.

网友答案:

After the call to mysql_query, you should call mysql_num_rows() to find out how many rows were returned from your SELECT statement, and if 1, proceed without displaying the select.

网友答案:

You can't eliminate a combo box that is hard coded into the HTML structure.

Incorporate this HTML into your code using something like:

// Get number of rows returned by query
$count = mysql_num_rows($result);

// Start iteration counter
$i = 1; # start at one to marry up with $count

// Iterate through array of mySQL rows
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {

    if ($count == 1) {  # Evals true if 1 customer is returned
        $html = "<input type=\"textbox\" value=\"". $row['customer'] . "\" />";

    // If multiple customers are returned, 
    // but it's the first iteration through the loop. Create Select box.
    } elseif ($i == 1 && $count != 1) { 

        $html .= "<select name=\"customer\">\n";
        $html .= "<option value=\"" . $row['id'] . "\">" . $row['customer'] . "</option>\n";

    // If this is the last row, close the select box.
    } elseif ($i == $count) {
        // 
        $html .= "<option value=\"" . $row['id'] . "\">" . $row['customer'] . "</option>\n";
        $html .= "</select>\n";

    // If none of the above, create new option
    } else {
        $html .= "<option value=\"" . $row['id'] . "\">" . $row['customer'] . "</option>\n";
    }

    $i++;
}

echo $html;

EDIT: From comments below, changed the <option> value attribute to be the customers row ID.

分享给朋友:
您可能感兴趣的文章:
随机阅读: