Forum Moderators: coopster
I have an edit page for these jobs, and i can easily echo textinput fields and update the data. I can also re-populate the drop down list like i do on the add_job form, but i want it to select the initially selected. Otherwize everytime i edit a job, it defaults back to the first selection in the list, and i must choose the correct one before editing otherwize it will overwrite my correct selection.
Any ideas?
=========== CURRENT CODE USING : START ===========
$query = "SELECT * FROM jobs
WHERE job_id='$id'";
$result = mysql_query($query);
while ($r = mysql_fetch_assoc($result)) {
extract($r);?>
<?php
require_once ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM machine");
echo "<select name=machine> ";
while ($row = mysql_fetch_array($sql))
{
$sub = $row["list"];
$sub_id = $row["list"];
echo "<option value=$sub_id>$sub</option>";
}
echo "</select>";
?>
brodie_r, you need to simply make a comparison in your second loop. During each iteration of the loop you need to compare the 'machine id' variable value extracted from the first query with each 'sub_id' value being made into an <option>. If they match, then you added the ' selected="selected"' attribute to the <option>.
This is all i have so far (which just outputs whats in the folder, still unsure how to include the initial select.
<?php
require_once ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM operator");
echo "<select name=operator> ";
while ($row = mysql_fetch_array($sql))
{
$sub = $row["list"];
$sub_id = $row["list"];
echo "<option value=$sub_id>$sub</option>";
}
echo "</select>";
?>
<?php
$sql = mysql_query("SELECT * FROM operator");
echo '<select name="operator">';
$count = 0;
while ($row = mysql_fetch_array($sql)) {
$sub = $row["list"];
$sub_id = $row["list"];
echo '<option value="' . htmlentities($sub_id);
if (++$count == 1) {
echo ' selected="selected"';
}
echo '>' . htmlentities($sub) . '</option>';
}
echo "</select>";
?>
<?php
$sql = mysql_query("SELECT * FROM operator");
echo '<select name="operator">';
$count = 0;
while ($row = mysql_fetch_array($sql)) {
$sub = $row["list"];
$sub_id = $row["list"];
echo '<option value="' . htmlentities($sub_id) . '"';
if (++$count == 1) {
echo ' selected="selected"';
}
echo '>' . htmlentities($sub) . '</option>';
}
echo "</select>";
?>
;)
====
That's exactly what i need mate, cause at the moment when i add a job, i want it to see that the machine is say machineB, and choose machineB as the initially selected, but still have machineA, machineC etc in the drop down list. So the code that you have provided above is just for choosing an initially selected, if its in that spot, not for dynamic? *confused*
So the code im using is:
<?php
$sql = mysql_query("SELECT * FROM operator");
echo '<select name="operator">';
$count = 0;
while ($row = mysql_fetch_array($sql)) { $sub = $row["list"];
$sub_id = $row["list"];
echo '<option value="' . htmlentities($sub_id) . '"';
if (++$count == 1) { echo '<option selected="selected"';
} echo '>' . htmlentities($sub) . '</option>';
} echo "</select>";
?>
I was testing the output html and noticed the '<' missing from your example, so added it in, but still no luck. No errors or anything it just doesnt select the correct one. So my table is called operator, have a column called list, which all the operators are in.
list
======
brodie
------
john
------
rhys
== EDIT_JOB.PHP PAGE ============================
<?php
require_once ('mysql_connect.php');
$query = "SELECT * FROM jobs
WHERE job_id='$id'";
$result = mysql_query($query);
while ($r = mysql_fetch_assoc($result)) {
extract($r);?>
<form name="edit_job" action="edit_job_post.php" method="post" class="style1">
<?php
$sql = mysql_query("SELECT * FROM operator");
echo '<select name="operator">';
$count = 0;
while ($row = mysql_fetch_array($sql)) {
$sub = $row["list"];
$sub_id = $row["list"];
echo '<option value="' . htmlentities($sub_id) . '"';
if (++$count == 1) { echo '><option selected="selected"';
} echo '>' . htmlentities($sub) . '</option>';
} echo "</select>";
?>
input type="submit" name="submit" value="Edit Job">
</form>
<?php
++$i;
}
?>
================================================
====== HTML OUTPUT FROM ABOVE PHP PAGE=======
<select name="operator">
<option value="brodie">
<option selected="selected">brodie</option>
<option value="rhys">rhys</option>
<option value="john">john</option>
</select>
===========================================
this line is causing your confusion
if (++$count == 1) { echo '><option selected="selected"';
what is happening is that when $count is equal to 1 it is closing the above option and starting a new one. The line should look like this
if (++$count == 1) { echo ' selected="selected"';
all that does is add to the option tag if your criteria is met, not start a whole new as we have already opened the option tag for this iteration of the loop.
give that a try
this line
if (++$count == 1) { echo ' selected="selected"';
is the comparison line, that is what makes a certain option selected, the part you need to change is the bold part
if (++$count == 1) { echo ' selected="selected"';
change that to what you want to compare to choose the selected option.
what is the comparison you want to make?
If you read up you can see full details of my pages, for now if what your asking. What i require is:
At the moment when i edit a job, i want it to see that the machine is machineB, and choose machineB as the initially selected, but still have machineA, machineC etc in the drop down list.
if ($row['columnname'] == 'machineB') { echo ' selected="selected"';
as coopster already mentioned we don't know the columnname so you will have to add that
and just so we're clear
<option value="brodie">
<option selected="selected">brodie</option>
that's broken html and actually shouldn't post properly as the selected one doesn't actually have a value. If it does post the "brodie" value then that is just luck.
<?php
$sql = mysql_query("SELECT * FROM operator");
$operator_query = mysql_query("SELECT operator FROM jobs WHERE job_id='$id'");
$operator_result = mysql_query($operator_query);
echo '<select name="operator">';
while ($row = mysql_fetch_array($sql)) {
$sub = $row["list"];
$sub_id = $row["list"];
echo '<option value="' . htmlentities($sub_id) . '"';
if ($row['list'] == $operator ) { echo ' selected="selected"';
} echo '>' . htmlentities($sub) . '</option>';
} echo "</select>";
?>