Reputation: 31
Having a bit of trouble with some nested PHP inside HTML inside PHP.
Have the following code that works fine on it's own.
echo "<select>";
foreach($all_rooms as $val) {
echo "<option> $val </option>";
}
echo "</select>";
However when i try to place it inside HTML which is nested within the PHP block it doesn't seem to work:
<select id = "room_change'.$booking_id["{$id}"].'" hidden>
'.
foreach($all_rooms as $val) {
"<option>$val </option>";
}
.'
</select>
I have also tried the following code which doesn't seem to work :
<select id = "room_change'.$booking_id["{$id}"].'" hidden>
'. foreach($all_rooms as $val) {
.'<option>'.$val .'</option>'.;
}
.'
</select>
Worth noting that this code is nested inside PHP tags. Full , somewhat irrelevant code :
<?php
echo '<!-- BEGIN content -->
<div id="booking_Requests">
<p>
Name : '.$fname["{$id}"] . ' '.$lname["{$id}"].' </br>
Phone Number : '.$mobile["{$id}"].' </br>
Date : '.$newdate.' </br>
Time : '.$fstart_Time["$id"].' to '.$fend_Time["{$id}"].' </br>
<!-- Displays the requested room -->
Requested room : <label id = "requested_room'.$booking_id["{$id}"].'" visible>'.$room["{$id}"].' </label>
<!-- Hidden Select box that is displayed when user clicks the alter button -->
<select id = "room_change'.$booking_id["{$id}"].'" hidden>
'.
foreach($all_rooms as $val) {
"<option>$val </option>";
}
.'
</select>
<!-- Label that dispays whether or not the room is available -->
<label id = "room_available'.$booking_id["{$id}"].'"> |||| '.$room_blah.'</label>
<!-- Button that allows the user to alter the selected room, calls the function
alterRoomFunc -->
<button type ="button" id = "alter_room'.$booking_id["{$id}"].'"
onclick="alterRoomFunc('.$booking_id["{$id}"].')"> Alter </button>
</br>
Booking ID: '.$booking_id["{$id}"].' </br>
<button type="button" id="'.$booking_id["{$id}"].'"
onclick="accFunc('.$booking_id["{$id}"].')">Accept</button>
<button type="button" id="'.$booking_id["{$id}"].'"
onclick="rejFunc('.$booking_id["{$id}"].')">Reject</button>
</p>
</br>
</div>
'
;
}
?>
Upvotes: 0
Views: 163
Reputation: 13
<?php
echo "
<select id=\"room_change",$booking_id[$id],"\">";
foreach($all_rooms as $val)
{
echo "<option>$val</option>";
}";
echo "</select>";
<?
Upvotes: 0
Reputation: 12084
This should work :
<?php
echo "<select id = \"room_change " . $booking_id[$id] . "\" hidden> ";
foreach($all_rooms as $val) {
echo "<option>". $val ."</option>";
}
echo "</select>";
?>
Upvotes: 0
Reputation: 4218
Here is another way to do it:
<?php
echo '<select id = "room_change'.$booking_id[$id].'" hidden>';
foreach($all_rooms as $val) {
echo '<option>'.$val .'</option>';
}
echo '</select>';
?>
Upvotes: 0
Reputation: 2709
Here is your code working. ** /!\ This isn't the best way to do your code BUT you seem not to know well how PHP work /!\ **
Try to understand my code and the mistakes you did. I repeat, this isn't the best way to do it, but as a beginer this is what you should be able to do. Ask me anything you don't understnad. Here you'll learn how to escape quotes and that you have to echo everything but the loop things.
The best way to write sentence is to use <p></p>
instead of using <br/>
everytime. Open <p>
, put your name then close it. Because you are using many rows but only one <p>
<?php
/* BEGIN content */
echo '<div id=\'booking_Requests\'>';
echo '<p>';
echo 'Name : '.$fname[$id].' '.$lname[$id].' </br>';
echo 'Phone Number : '.$mobile[$id].' </br>';
echo 'Date : '.$newdate.' </br>';
echo 'Time : '.$fstart_Time[$id].' to '.$fend_Time[$id].' </br>';
/* Displays the requested room */
echo 'Requested room : <label id = \'requested_room'.$booking_id[$id].'\' visible>'.$room[$id].' </label>';
/* Hidden Select box that is displayed when user clicks the alter button */
echo '<select id = \'room_change'.$booking_id[$id].'\' hidden>';
foreach($all_rooms as $val) {
echo "<option>$val</option>";
}
echo ' </select>';
/* Label that dispays whether or not the room is available */
echo '<label id = \'room_available'.$booking_id[$id].'\'> |||| '.$room_blah.'</label>';
/* Button that allows the user to alter the selected room, calls the function
alterRoomFunc */
echo '<button type =\'button\' id = \'alter_room'.$booking_id[$id].'\'
onclick=\'alterRoomFunc('.$booking_id[$id].')\'> Alter </button>';
echo '</br> ';
echo 'Booking ID: '.$booking_id[$id].' </br>';
echo '<button type=\'button\' id=\''.$booking_id[$id].'\'
onclick=\'accFunc('.$booking_id[$id].')\'>Accept</button>';
echo '<button type=\'button\' id=\''.$booking_id[$id].'\'
onclick=\'rejFunc('.$booking_id["{$id}"].')\'>Reject</button>';
echo '</p>';
echo '</br>';
echo '</div>';
}
?>
Upvotes: 0
Reputation: 2709
There are many problems in your code. When you are using php code you must open php tab. If you want to print something on screen you have to echo
it
<?php
echo "<select id = 'room_change".$booking_id[$id]."' hidden>";
foreach($all_rooms as $val) {
echo "<option>$val</option>"; // OR
// echo '<option>'.$val.'</option>';
}
?>
</select>
Upvotes: 1