Forum Moderators: coopster

Message Too Old, No Replies

if ..else

no php error, no MySQL errors but not delivering...

         

henry0

7:05 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I try to obtain from from my select box to display option previously chosen when in edit mode

my "if else" does not echo the previously selcted value that is correctly entered in the DB

I can change category and get correct new value in the DB
but cannot echo the selected value
thanks
<<<
Choose a Category:<br>
<select name="category" Wrap=virtual>
<option value="ag">
<?
$query= "select category from templates where username= '$username' and page= '$page' ";
$result=mysql_query($query);
while($query_data=mysql_fetch_array($result) ){
$category=($query_data["category"]);
}
if ($category=="ag") echo "Selected: AGRICULTURE ";
else echo "AGRICULTURE";
?> </option>
>>>

wonderbread

7:56 pm on Jan 23, 2004 (gmt 0)

10+ Year Member



Is the problem that it is not automatically selecting the items on the dropdown? If it is, I think this code may help..can anyone confirm? I think you're missing brackets too.

Choose a Category:<br>
<select name="category" Wrap=virtual>
<?
$query= "select category from templates where username= '$username' and page= '$page' ";
$result=mysql_query($query);
while($query_data=mysql_fetch_array($result) ){
$category=($query_data["category"]);
}
if ($category=="ag") {
echo "<option value=\"ag\" selected>Selected: AGRICULTURE</option>";
} else {
echo "<option value=\"ag\">AGRICULTURE</option>";
}
?>

henry0

8:29 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks
but it does not change anything

still no effects from the "if..Else.."

as mentioned I can change the values
but it is still not showing the selected category
and still defaulting back to "agriculture"
so if an Attorney edits its content
and do not select again "law" its category will become "agriculture" :)

<edit>
I am not sure that in the option double quotes need to be escaped?
</edit>

<edit_2>
My mistake - need to be escaped - </edit>

justageek

8:40 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your while loop is overwriting your $category var. Are you absolutely sure the last value from the db is lowercase 'ag'? If not the 'if' will not be true.

JAG

henry0

8:45 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks JAG
could you develop the overwriting
I used the same while loop at list 200 times in the rest of the other pertaining scripts and do not have a problem
so if overwriting happens it happens at the
"if" level
but I do not see the fix

Yes 'ag' is lower case
Henry

wonderbread

8:54 pm on Jan 23, 2004 (gmt 0)

10+ Year Member



Is the SQL query returning data? You can check by adding:

$num = mysql_num_rows($result);

Put this in and then echo the var $num. If it returns 0 then you're not getting any results back. This means something is wrong with the query or there is no data in the table that matches.

Also, what is following after the php bit of code?

justageek

9:17 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



while($query_data=mysql_fetch_array($result) ){
$category=($query_data["category"]);
}

If you only have one record in the db that matches the query then the while would be OK. But if you had more than one you will keep changing the value to the last record returned.

That's why it's important to know what the last matching record value is. As wonderbread pointed out....it may also be null which would fail. So if you have two records in the db and the natural order has the category of 'ag' but the next one has a value of 'bh' or null it will fail.

JAG

henry0

9:34 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



wonderbread I tried before even in php my admin
the query is OK

JAG
to verify your last post I (on my test server) deleted all records to have only one vaue left
and it does not change anything

I am afraid that I have to rethink that all select box solution
Henry

I have a solution which consists in separating the category choice from the editable content
which can be done but it will not be elegant
and I will not learn anything!

justageek

9:58 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just for grins and giggles:

echo out the following just before the if...

echo "query data: ".$query_data['category']."<br>";
echo "category var: ".$category."<br>";

It will show up before the select box but just make sure the values are what you think they should be.

JAG

henry0

10:13 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK
last report:
it does not echo anything
I need to review the whole query
however it works if I isolate it and run it separately
JAG thanks again
I will try to fix it and report tomorow AM US time
sorry got to go, called for a meeting off my premises!

Henry