Page 1 of 1

PHP combo

Posted: Mon Jul 25, 2016 2:53 am
by asai
Hi
I have a combo that I want to fill with data from a table.

Heres my code:

Code: Select all

<html>
<?php

include './config/config.php';
include './config/opendb.php';

$query_disp="SELECT DISTINCT 
            Responsible as respons
            FROM RegTable";
$result_disp = mysql_query($query_disp, $conn);

include './config/closedb.php';

?>
<h2>Responsible:</h2>
<form action="test.php" method="POST">
  <select name="responsible">
    <?php foreach ($result_disp as $key => $value) : ?>
    <?php $selected = ($key == $_POST['respons']) ? 'selected="selected"' : ''; ?>
    <option value="<?php echo $key ?>" <?php echo $selected ?>>
    <?php echo $value ?>
    </option>
<?php endforeach; ?>
  </select>
  
  
<input type="submit" value="Vis liste">
</form>

</html>
My test.php looks like this:

Code: Select all

<?php

$respons = $_POST['responsible'];

include './config/config.php';
include './config/opendb.php';

$result = mysql_query("SELECT * FROM RegTable WHERE Responsible = '$respons' ORDER BY Date DESC") 
or die(mysql_error());  
echo "<table border='0'>";
echo "<tr><th>Dato</th><th>Registrert av</th><th>Telefon</th><th>Mobil</th><th>Kundenavn</th><th>Kontaktperson</th><th>Endring</th></tr>";
while($row = mysql_fetch_array( $result )) {
	echo "<tr><td>";
	echo date('d.m.Y', strtotime($row['Date']));
	echo "</td><td>";
	echo $row['Responsible'];	
	echo "</td><td>";
	echo $row['Phone'];
	echo "</td><td>";
	echo $row['Mobile'];
	echo "</td><td>";
	echo $row['CustName'];
	echo "</td><td>";
	echo $row['ContactName'];
	echo "</td><td>";
	?>

However the combobox is not filled. I have tried the mysql query, and that works. The problem has to be with the list in the combo. Any suggestions?

Re: PHP combo

Posted: Mon Jul 25, 2016 7:57 am
by asai
I have tried a change in my code:

Code: Select all

<TD WIDTH=50%>
<select name="user">
<?php

include './config/config.php';
include './config/opendb.php';

$sql= mysql_query("SELECT UserName FROM UserTable ORDER BY UserName");
while ($row = mysql_fetch_array($sql)) {
echo "<option value=\"user1\">" . $row['UserName'] . "</option>";
}
?>

</select>
</TD>
But theres still something wrong.

Re: PHP combo

Posted: Mon Jul 25, 2016 8:10 am
by Celauran
asai wrote:But theres still something wrong.
You'll need to be a lot more specific than that. What is the expected outcome? What is the actual outcome? What errors are being reported? Is error reporting turned on? Have you checked your error logs?

Re: PHP combo

Posted: Tue Jul 26, 2016 3:57 am
by asai
Sorry...
First of all I had wrong filetype. So now the combo is filled correctly with this code:

Code: Select all

<TR VALIGN=TOP>
<TD WIDTH=50%>
<p>Registrert av: </p></TD>

<TD WIDTH=50%>
<select name="user">
<?php

include './config/config.php';
include './config/opendb.php';

$sql = mysql_query("SELECT UserName FROM Users ORDER BY UserName");
while ($row = mysql_fetch_array($sql)) {
echo "<option value=\"user1\">" . $row['UserName'] . "</option>";
}
?>

</select>
</TD>
</TR>
However in my output I get user1, not the user selected from the combo. What am I doing wrong here?

Re: PHP combo

Posted: Tue Jul 26, 2016 6:29 am
by Celauran
value is always set to user1 rather than something dynamic

Re: PHP combo

Posted: Tue Jul 26, 2016 6:57 am
by asai
Celauran wrote:value is always set to user1 rather than something dynamic
How can I set it to the value selected in the combobox?

Solved it:

Code: Select all

<TD WIDTH=50%>
<select name="user">
<option value="">Username</option>
<?php

include './config/config.php';
include './config/opendb.php';

$sql = mysql_query("SELECT UserName FROM Users ORDER BY UserName");
while ($row = mysql_fetch_array($sql)) { ?>
<option value="<?php echo $row['UserName']; ?>"<?php echo 'selected="selected"'; ?>><?php echo $row['UserName']; ?></option>
<?php } ?>
</select>
</TD>

Re: PHP combo

Posted: Tue Jul 26, 2016 8:30 am
by Celauran
The value part is right, but that's going to mark every entry as selected, which isn't what you want. If there's a previously selected value, you need to check for that before setting selected.

Re: PHP combo

Posted: Tue Jul 26, 2016 6:49 pm
by Christopher
This is a good example of not following the most basic rule programming -- separating your Domain from your Presentation -- and how it causes confusion that results in problems. It is better to organize and encapsulate, that way you can test each independently to confirm that it works.

Here is your Domain code that provides database data to be displayed. It only depends on the Data Source code to work. It has no dependencies on the Presentation code above it, so can be reused on any page that needs the User Names.

Code: Select all

include './config/config.php';
include './config/opendb.php';

function findUserNames()
{
    $rows = array();
    $sql = "SELECT UserName FROM Users ORDER BY UserName";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result)) {
        $rows[] = $row;
    }
    return $rows
} 

$value = 'Where does the default value come from?';
$rows = findUserNames();
?>
This is your Presentation code. In this case it is just a template that could even be in a separate file. It depends on $rows and $value containing data.

Code: Select all

<?php
function buildSelectOptions($data, $value, $value_field, $label_field)
{
    $selected = ($value == $data[$value_field]) ? ' selected="selected"' : '';
    $str = "<option value=\"{$data[$value_field]}\"$selected>{$row[$label_field]}</option>\n";
    return $str;
}

<TD WIDTH=50%>
<select name="user">
<option value="">Username</option>
<?php
    echo buildSelectOptions($rows, $value, 'UserName', 'UserName');
?>
</select>
</TD>
I want to point on that the code above only uses Structured Programming techniques defined by Dijkstra almost 50 years ago. These basic ideas are still important, but a lot of new ideas have been developed since then (e.g. OOP) that better organize and encapsulate your code -- to further reduce confusion and problems.