Forum Moderators: coopster

Message Too Old, No Replies

radiobutton if loop problem

         

gbcamb

2:18 pm on Mar 15, 2004 (gmt 0)

10+ Year Member



Hi, I've got a problem implimenting something in PHP, and I'm hoping you guys can help.

I've got a set of radio buttons, and I want to create an IF loop that will execute a function if the value of the radio button is set to the correct value.

The radio buttons can be set to either "Y" or "N", and have variable names (they are called (string)n, where n is an integer relating to an incremented value ($count[FacilityID]).

The closest theory I have is something along the lines of:
IF( $(string)$count[FacilityID] == Y) {statement}
But this creates a parse error (I assume because it uses two $ signs in a single expression.

Does anyone have any idea what I could do?

lorax

3:09 pm on Mar 15, 2004 (gmt 0)

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



Greetings gbcamb, welcome to WebmasterWorld.

Let me get this straight. You have a series of radio buttons built from what sounds like a loop.

(some loop mechanism) {
<input type="radio" name=$foo.$count[FacilityID] value="Y">
<input type="radio" name=$foo.$count[FacilityID] value="N">
}

And you want to initiate a function if some condition is true (a certain radio button has been selected).

Just looking at your if statement

IF($(string)$count[FacilityID] == Y) {statement}

Should be written as

IF( $(string).$count[FacilityID] == Y) {statement}

Note the dot (for concantenation) between the count and the string - also note that I used it in building the radio button's name.

gbcamb

3:25 pm on Mar 15, 2004 (gmt 0)

10+ Year Member



You've hit the problem right on the head! Cheers.

Do I need to declare the radio button as:

<input type="radio" name=$foo.$count[FacilityID] value="Y">

or would it be:

<input type="radio" name=(string).$count[FacilityID] value="Y">

lorax

4:48 pm on Mar 15, 2004 (gmt 0)

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



Where does (string) come from? The only reason I'm using $foo is as a placeholder. It simply represents a variable. If the name is supplied by the query results then output the name there instead.

gbcamb

11:47 am on Mar 16, 2004 (gmt 0)

10+ Year Member



The (string) element is just a part of the name of the radio button, a left over idea from one my my attempts to solve the problem before I came on here looking for help, and it now helps me follow my program :D

I'm afraid I've got a problem still. I believe I've followed your advice, but the script refuses to work.

My radio button area is declared as below:

echo "<table border=0>";
while ($row = mysql_fetch_array($result2)) {
echo "<tr>";
echo "<td>$row[FacilityName]</td>";
echo "<td>Yes <input type=radio name=$foo.$row[FacilityID] value=Y checked></td>";
echo "<td>No <input type=radio name=$foo.$row[FacilityID] value=N></td>";
echo "</tr>\n";
}
echo "</table>";

with $foo declared as (string) elsewhere. The IF statement is declare thus:

while ($count = mysql_fetch_array($result7)) {
if ($(string).$count[FacilityID] == Y){
$sql4 = "INSERT INTO roomfacilities (RoomID, FacilityID) values ($check[RoomID], $count[FacilityID])";
$result4 = mysql_query($sql4);
}
}

But when I try to run it, I encounter this error:

Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /disks/diskh/zco/cogeb/public_html/add_room.php on line 26

(line 26 being the line containing the IF statement)

Sorry to bother you again, but can you see the problem?