Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

0'1 and 1's in drop-down list 1

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
Hi

This is a further query regarding this thread I asked about earlier - thread434-1505213

Although the query answers my question by printing the result, I need this result in an edit form with a drop-down list (form select).
So I have a mySQL Db with values in it for 'activated' and 'authorised' as an integer - 0 or 1. Obviously 0 is no and 1 is yes.
So I want to edit the users details and set these flags to Yes (if they are set to no). The problem I have is that I extract the data as 0's or 1's.

When I load the users details into the edit form, I want to set the form select value to either No or Yes depending on the value from the database. I also want to list the cahracter values for the numerical ones, so in the list I want a No and yes with the values set to 0 and 1.

Possible?
Thanks.
 
Hi

I will continue with my old suggestion :
Code:
$val=array([i]'No'[/i],[i]'Yes'[/i]);

$row=fetch_assoc($statement); [gray]// assuming all other required operations[/gray]

[red][b]echo[/b] [i]"Activated <select name=\"Activated\">\n"[/i];

[b]foreach[/b] ($val as $number=>$word)
  [b]echo[/b] [i]"<option value=\"$number\""[/i].($number==$row[[i]'Activated'[/i]]?[i]' selected="selected"'[/i]:[i]''[/i]).[i]">$word</option>\n"[/i];

[b]echo[/b] [i]"</select>\n"[/i];[/red]

Feherke.
 
I need a little more help with this query...

I think there are a couple of syntax errors in the code which I think I've sorted out like the { and } and also the mysql_fetch_assoc, but how this script know what the value if the database value is so the form defaults to that value?
 
Hi

Huh ? Now I am not sure that I understood you correctly.

I understood that you want to display a [tt]select[/tt] in a [tt]form[/tt] where the value of Activated can be set. And you want it user friendly, so the user will see words, but behind the scenes will still work with numbers. And when the [tt]select[/tt] is displayed, the item corresponding to Activated's current value should be selected by default. So the HTML should be generated as follows :
Code:
[gray]// if Activated is 0[/gray]
<select name="Activated">
<option value="0" selected="selected">No</option>
<option value="1">Yes</option>
</select>

[gray]// if Activated is 1[/gray]
<select name="Activated">
<option value="0">No</option>
<option value="1" selected="selected">Yes</option>
</select>
Am I ( very ) wrong ?

Feherke.
 
Yes, that's exactly what I need.
 
Hi

Ok, that's exactly what the code does.

( Of course, I tested it only partially, because I have no suitable database accessible now. )

If you have problem including it in your script, you will have to paste the relevant parts of your script, or the entire script if is not huge.

Feherke.
 
Thanks.
I'm having a little trouble understanding the script.
I already a an SQL query to pull all the info for this user using the ID set in the $_REQUEST.
So I have all the data for this user.

Your script mentions a
Code:
$row=fetch_assoc($statement);
which I don't understand. I have a variable already set with the value of the 'activated' field from the SQL.

And I also don't understand this
Code:
$val as $number=>$word
Can you explain that for me?
 
Hi

Or was the accent only on this question ?
d0nny said:
how this script know what the value if the database value is so the form defaults to that value?
Then let us rewrite it using [tt]if[/tt] instead of the ternary operator :
Code:
foreach ($val as [red]$number[/red]=>[blue]$word[/blue]) {
  echo "[highlight #fcc]<option value=\"[/highlight][COLOR=red #fcc]$number[/color][highlight #fcc]\"[/highlight]"
  if ([red]$number[/red]==$row['Activated']) echo '[highlight #cfc] selected="selected"[/highlight]';
  echo "[highlight #ccf]>[/highlight][COLOR=blue #ccf]$word[/color][highlight #ccf]</option>\n"[/highlight];
}
And that outputs the HTML code of the [tt]option[/tt] from 3 pieces :
Code:
[highlight #fcc]<option value="[/highlight][COLOR=red #fcc]0[/color][highlight #fcc]"[/highlight][highlight #cfc] selected="selected"[/highlight][highlight #ccf]>[/highlight][COLOR=blue #ccf]No[/color][highlight #ccf]</option>[/highlight]

Feherke.
 
OK, I understand that... well, I'm not sure how the $number=>$word works.

But which value/variable in this small script is the variable I have set with the value of the database field?
 
Hi

d0nny said:
Your script mentions a
Code:
$row=fetch_assoc($statement);
which I don't understand.
That is used more as a placeholder for "place your SQL query here". It is just to show that $row is supposed to be an associative array where the keys are the field names and the values the values.
d0nny said:
I have a variable already set with the value of the 'activated' field from the SQL.
Well, you not mentioned that. Neither here or in your previous thread. Neither its name or type. That is why I used my $row. Of course, you are free to replace it with yours.
d0nny said:
And I also don't understand this
Code:
$val as $number=>$word
Can you explain that for me?
That is the syntax of the [tt]foreach[/tt] instruction. loops over the items of array $number and for each item places the key in $number and the value in $word.


Feherke.
 
OK, I'm getting more and more confused now.
I don't have a variable by the name of $number or $word.

All I have is the variable $activated which is set to the value of either 0 or 1 from the database. This is set to whatever the value is set against that user.

So I have a variable of $activated set for this specific user.
I want a drop-down box with No and Yes as options, and those options to be equal to values 0 (No) and 1 (Yes).
I want the box to default to the value of the variable $activated, but in words.

Apologies, as it might be me that is confusing the whole process!
 
Hi

d0nny said:
I don't have a variable by the name of $number or $word.
You not need to have them. [tt]foreach[/tt] will create them and will set their values as it loops over the given array.
d0nny said:
So I have a variable of $activated set for this specific user.
Ok, then you will need it like this :
Code:
$val=array('No','Yes');

echo "Activated <select name=\"Activated\">\n";

foreach ($val as $number=>$word)
  echo "<option value=\"$number\"".($number==[red]$activated[/red]?' selected="selected"':'').">$word</option>\n";

echo "</select>\n";

Feherke.
 
Perfect!
Words a treat!

Many thanks and apologies again for confusing the matter somewhat.
I've never seen that 'foreach' used before... maybe that's what confused me with those variables too!!

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top