0

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>
            ' 
          ;
        }
      ?>
4
  • Because you need to open php tag when you are using php. After your select, open php tag then close it before </select> Commented Aug 19, 2015 at 8:20
  • Php is opened well before this, felt pointless posting my whole code... Commented Aug 19, 2015 at 8:22
  • Your first Select as no escaping strings... and no echo, this is why i said that Commented Aug 19, 2015 at 8:24
  • $id is a number, so if you use "$id" the program will look for a word named $id, so your program won't find anything Commented Aug 19, 2015 at 9:06

5 Answers 5

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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>';

    }
?>

2 Comments

Is it better practise to echo everything separately?
You choose. I echoed everything to show you that it's important to echo everything. You can make a single echo if you want. You choose how you want to code
0

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

Comments

0

This should work :

<?php
      echo "<select id = \"room_change " . $booking_id[$id] . "\" hidden>  ";
      foreach($all_rooms as $val) {
         echo "<option>". $val  ."</option>";
      }
      echo "</select>";
?>

Comments

0

<?php
   echo "
   <select id=\"room_change",$booking_id[$id],"\">";
      foreach($all_rooms as $val){
   echo "<option>$val</option>";
      }";
   echo "</select>";
<?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.