Forum Moderators: coopster

Message Too Old, No Replies

drop down list populate + select correct intial selected from query

         

brodie_r

11:57 am on Feb 22, 2006 (gmt 0)

10+ Year Member



I have a add jobs form as cooper is by now well aware ;). This form has a drop down list which is populated from a machine table. So it gets all the machines from a table, then populates the table. Everything works fine, and it adds fine to the database.

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>";
?>

omoutop

1:34 pm on Feb 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try to send the job's id as hidden field in form or pass it in url.

Later in drop down list, if ($send_id==$current_id) {echo "selected";}

Hope u get the point :)

coopster

5:27 pm on Feb 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think what brodie_r is saying here is that this is the initial display of the edit page and he wants the job_id's machine to show up as the selected machine in the drop down list.

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>.

brodie_r

1:28 pm on Feb 23, 2006 (gmt 0)

10+ Year Member



coopster mate, ive tried so hard in trying to code this, but have dug myself a hole. Would you be able to help me get started?

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>";
?>

coopster

5:29 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure, but it depends on what you want the initial selection to be by default. I'll show you how to make it the first option. You could use a counter like so:
<?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>";
?>

We start off by initializing our counter to zero. Then, on each iteration of the loop we are adding 1 to counter and comparing it to the integer value of 1 to see if it was the first iteration of the loop. If so, mark that <option> as "selected". Otherwise, just print it out as another option, unselected.

jatar_k

6:27 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



i think there are some thml problems in that code, unclosed quotes

<?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>";
?>

;)

coopster

6:45 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



LOL. Thanks, jk. Yeah, I just threw it down out of my head, which is full of not only missing quotes, but useless quotes ;)

brodie_r

5:18 am on Feb 24, 2006 (gmt 0)

10+ Year Member



=== COOP WROTE ===
I think what brodie_r is saying here is that this is the initial display of the edit page and he wants the job_id's machine to show up as the selected machine in the drop down list.

====

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*

brodie_r

5:18 am on Feb 24, 2006 (gmt 0)

10+ Year Member



"when i add" should be "when i edit" sorry

brodie_r

1:14 pm on Feb 24, 2006 (gmt 0)

10+ Year Member



Ok so i have tried the above, but still it defaults/selects the first one the the drop list, not the correct entry.

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

brodie_r

1:41 pm on Feb 24, 2006 (gmt 0)

10+ Year Member



Ive added in all php, to help solve the problem.
P.S The job_id comes from the previous page, where i actually click the "edit job" button.

== 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>
===========================================

coopster

4:48 pm on Feb 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, now where you are extracting the data from the first query there is a column in that result set that contains the "initial value to select" -- since you are "SELECT * " we cannot see what that column name is, but you know what it is because you have it defined in your table. Your next step would be to use that value as opposed to a counter to do your comparison. Make sense?

jatar_k

6:05 pm on Feb 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the problem with your code there, as you can see by the output, is that it is a little confused.

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

brodie_r

3:29 am on Feb 25, 2006 (gmt 0)

10+ Year Member



jatar_k, tried with that still is selecting the first one in the list, not the correct one.

Coopster, how would i go about doing what you said above, and scrapping the count?

jatar_k

6:46 am on Feb 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well at least it will give proper html and actually post values now ;)

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?

brodie_r

7:30 am on Feb 25, 2006 (gmt 0)

10+ Year Member



jatar_k, even with that being wrong it still posts html that works fine. I have made teh change you selected and it doesnt it works in the sense of the drop down list, but is not selecting the correct one.

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.

jatar_k

3:04 pm on Feb 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



then you need something like

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.

brodie_r

2:02 pm on Feb 26, 2006 (gmt 0)

10+ Year Member



Ok got it all working, just for future refference and other users looking at something similar i have posted my code:

<?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>";
?>