0

If variable $single = 1 and variable $double = 2

I want a result like this image : https://i.sstatic.net/1IivO.jpg

I try the code like this :

<?php
    $Sgl = 1;
    $Dbl = 2;
    $Trp = 0;
?>

<table border="1">
    <tr>
        <td>No</td>
        <td>Room Type</td>
        <td>Room Grade</td>
        <td>Gender</td>
        <td>Family Name</td>
        <td>First Name</td>
    </tr>
    <?php 
        $no = 1;
        for($i=0; $i< ($Sgl ? $Sgl : ($Dbl ? $Dbl : ($Trp ? $Trp : 0))); $i++) {
            if($Sgl)
                $room_type = 'Single';
            else if($Dbl)
                $room_type = 'Double';
            else if($Trp)
                $room_type = 'Triple';
    ?>
    <tr>
        <td><?php echo $no; ?></td>
        <td><?php echo $room_type; ?></td>
        <td>Standart Room</td>
        <td>
            <select>
              <option selected>Mr.</option>
              <option>Ms</option>
              <option>Mrs</option>
              <option>Mstr</option>
            </select>
        </td>
        <td>
            <input type="text" class="" placeholder="Family Name">
        </td>
        <td>
            <input type="text" class="" placeholder="First Name">
        </td>
    </tr>
    <?php 
            $no++;
        } 
    ?>

</table>

But it seems the code is still wrong

How to keep the results as the image above?

Any help much appreciated?

Cheers

2
  • what is your issue? and what are your requirement? Commented Jan 28, 2016 at 5:06
  • @Amit Rajput, what I want is : If variable $single = 1 and variable $double = 2, then the result like this image : imgur.com/U0zxCgI. I have tried like the above code, but it failed Commented Jan 28, 2016 at 5:13

4 Answers 4

1

Try this :

<?php
    $Sgl = 1;
    $Dbl = 2;
?>

<table border="1">
    <tr>
        <td>No</td>
        <td>Room Type</td>
        <td>Room Grade</td>
        <td>Gender</td>
        <td>Family Name</td>
        <td>First Name</td>
    </tr>
    <?php       
        $total = ($Sgl * $Sgl) + ($Dbl * $Dbl);
        for($i=1; $i <= $total; $i++) {
            $room = 'Standart Room';
            if($i == $Sgl && $Sgl == 1){
                $no = $i;
                $roomType = 'Single';
            }elseif ( ($i%$Dbl) == 0) {
                if($i == $Dbl){
                    $no = $i;
                    $roomType = 'Double';
                }else{
                    $no = $i - 1;
                    $roomType = 'Double';
                }
            }else{
                $no = $room = '';
                $roomType = '';
            }
    ?>
    <tr>
        <td><?php echo $no;?></td>
        <td><?php echo $roomType;?></td>
        <td><?php echo $room;?></td>
        <td>
            <select>
              <option selected>Mr.</option>
              <option>Ms</option>
              <option>Mrs</option>
              <option>Mstr</option>
            </select>
        </td>
        <td>
            <input type="text" class="" placeholder="Family Name">
        </td>
        <td>
            <input type="text" class="" placeholder="First Name">
        </td>
    </tr>
    <?php 
            $no++;
        } 
    ?>

</table>

OUTPUT :

It is same as showing in image.

Note :

This answer take variable $single = 1 and variable $double = 2.

Modified : This is fully dynamic code. Cheers...

<?php
   $counter = 0;
   $first =2; $second = 4; $third = 1; $four = 2; $five = 3;
   $common_fields = '<td>
            <select>
              <option selected>Mr.</option>
              <option>Ms</option>
              <option>Mrs</option>
              <option>Mstr</option>
            </select>
        </td>
        <td>
            <input type="text" class="" placeholder="Family Name">
        </td>
        <td>
            <input type="text" class="" placeholder="First Name">
        </td>';
?>

<table border="1">
    <tr>
        <td>No</td>
        <td>Room Type</td>
        <td>Room Grade</td>
        <td>Gender</td>
        <td>Family Name</td>
        <td>First Name</td>
    </tr>       
        <?php 
            if($first){
                for ($i=1; $i <= ($first*1); $i++) { 
                    $counter ++;                   
                    echo "<tr><td>$counter</td><td>First</td><td>First Standard</td>$common_fields</tr>";  
                }
            }

            if($second){
                for ($i=1; $i <= ($second*2); $i++) {
                    if($i%2) {$counter++;}
                    $roomtype = ($i%2) ? 'Second' : '';
                    $room = ($i%2) ? 'Second Standard' : '';
                    $no = (!empty($roomtype)) ? $counter : '';
                    echo "<tr><td>$no</td><td>$roomtype</td><td>$room</td>$common_fields</tr>";  
                }
            }

            if($third){
                for ($i=1; $i <= ($third*3); $i++) {
                    if(($i-1)%3 ==0) {$counter++;}
                    $roomtype = (($i-1)%3 ==0) ? 'Third' : '';
                    $room = (($i-1)%3 ==0) ? 'Third Standard' : ''; 
                    $no = (!empty($roomtype)) ? $counter : '';
                    echo "<tr><td>$no</td><td>$roomtype</td><td>$room</td>$common_fields</tr>";
                }
            }

            if($four){
                for ($i=1; $i <= ($four*4); $i++) {
                    if(($i-1)%4 ==0) {$counter++;}
                    $roomtype = (($i-1)%4 ==0) ? 'Four' : '';
                    $room = (($i-1)%4 ==0) ? 'Four Standard' : '';
                    $no = (!empty($roomtype)) ? $counter : '';
                    echo "<tr><td>$no</td><td>$roomtype</td><td>$room</td>$common_fields</tr>";  
                }
            }

            if($five){
                for ($i=1; $i <= ($five*5); $i++) {                    
                    if(($i-1)%5 ==0) {$counter++;}
                    $roomtype = (($i-1)%5 ==0) ? 'Five' : '';
                    $room = (($i-1)%5 ==0) ? 'Five Standard' : '';
                    $no = (!empty($roomtype)) ? $counter : '';
                    echo "<tr><td>$no</td><td>$roomtype</td><td>$room</td>$common_fields</tr>";    
                }
            }
        ?>
</table>
Sign up to request clarification or add additional context in comments.

15 Comments

It's working. It is the same as the picture. Thank you very much.
I want ask again. For example, there are 5 input variables. they are single, double, twin, triple, quad. it looks like the above code should be changed
@samueltoh YES. Above code will change. If my answer solve your question or near to your answer, don;t forget to vote
Ok. Seems the above code is not perfect. When I change the variables, the results does not match. For example, the input variables: $Sgl = 0, $Dbl = 2, $Twn = 0, $Trp = 0, $Quad = 0;. The result: imgur.com/U0zxCgI
i know this is not perfect. but matches your result. do one thing. tell me exact what you want. how many variable can be include and all things.
|
1

Try this:

<tr>
<td>No</td>
<td>Room Type</td>
<td>Room Grade</td>
<td>Gender</td>
<td>Family Name</td>
<td>First Name</td>
</tr>

<?php
$row_count = 1;
foreach($array as $key => $value):

if($value == 'single'){
    $room_type = "Single";
    $room_grade = "Standard Room";
    $count = 1;
}else if($value == 'double'){
    $room_type = "Double";
    $room_grade = "Standard Room";
    $count = 2;
}

for($i=1;$i<=$count;$i++){
    if($i>1){
        $room_type = "";
        $room_grade = "";
    }
?>
<tr>
    <td><?php echo $row_count; ?></td>
    <td><?php echo $room_type; ?></td>
    <td><?php echo $room_grade;?></td>
    <td>
    <select>
      <option selected>Mr.</option>
      <option>Ms</option>
      <option>Mrs</option>
      <option>Mstr</option>
    </select>
    </td>
    <td>
        <input type="text" class="" placeholder="Family Name">
    </td>
    <td>
        <input type="text" class="" placeholder="First Name">
    </td>
</tr>
<?php

}

endforeach;
?>

1 Comment

may i see what is out
0

Limited information. If you can please tell us your current output that would help. The issue seems to be in your for loop and conditionals. Try something like this.

$Sgl = 1; $Dbl = 2; $Trp = 3;
for ($i=0; $i<3; $i++) {
    switch ($i) {
        case $Sgl:
            $room_type = 'Single';
            break;
        case $Dbl:
            $room_type = 'Double';
            break;
        case $Trp:
            $room_type = 'Triple';
            break;
    }
}

3 Comments

what I want is : If variable $single = 1 and variable $double = 2, then the result like this image : imgur.com/U0zxCgI. I have tried like the above code, but it failed
"it failed" is ambiguous. Is it a syntax error? Is it a 500? A segfault? Is the output just incorrect? Try tweaking the for loop and conditionals and the variables. Use "echo" to debug your variables.
the output just incorrect. The output does not match with the picture
0

You can achieve your requirement like this. Just do in this way. Below is your full code.

<table border="1">
<tr>
    <td>No</td>
    <td>Room Type</td>
    <td>Room Grade</td>
    <td>Gender</td>
    <td>Family Name</td>
    <td>First Name</td>
</tr>
<?php 
$no = 1;
foreach($array as $key=>$val){        
    for($i=0; $i< $val; $i++) {
        $room_type = $key;
?>
<tr>
    <td><?php echo $no; ?></td>
    <td><?php echo $room_type; ?></td>
    <td>Standart Room</td>
    <td>
        <select>
          <option selected>Mr.</option>
          <option>Ms</option>
          <option>Mrs</option>
          <option>Mstr</option>
        </select>
    </td>
    <td>
        <input type="text" class="" placeholder="Family Name">
    </td>
    <td>
        <input type="text" class="" placeholder="First Name">
    </td>
</tr>
<?php            
    }
    $no++;
} 
?>

5 Comments

Thank you for answer my question. But the numbers are not the same. It is not the same to that in the picture. Look here imgur.com/U0zxCgI. It's different
OK, i got your point. Just try my updated answer, this will solve your no. issue also. let me know if works.
You try to see the latest answer to the above. It is my point
ok..but in above answer you would need to change code if need arises for more variables.
I need you help. Look here : stackoverflow.com/questions/38517387/…

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.