Reputation: 7076
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
Upvotes: 0
Views: 109
Reputation: 1120
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>
Upvotes: 1
Reputation: 1448
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;
?>
Upvotes: 1
Reputation: 2059
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++;
}
?>
Upvotes: 0
Reputation: 699
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;
}
}
Upvotes: 0