-2

I'm doing a campaign project in which the admin can reserve some seats using admin panel. When he wants to edit it,previously selected seats should highlight. This is what i've done for achieving this

if ($mode == 'EDIT')
        {
            $k=1;
            for($i=1;$i<=10;$i++)
            {
            for($j=1;$j<=10;$j++)
            { ?>
                <div id='<?php echo $k ;?>' class="seat_selection"
                <?php foreach($seat_data as $seats)
                {
                if($seats->iSeatNumber == $k)
                {?>
                    style='min-height:25px;min-width:25px;background-color:#12B22F;float:left;margin:0 0 5px 10px;cursor:pointer;'
                <?php
                }
                else
                {?>
                    style='min-height:25px;min-width:25px;background-color:#969696;float:left;margin:0 0 5px 10px;cursor:pointer;'
                <?php
                }
                }
                ?>
                >
                <?php echo $k;?>
                </div>
            <?php $k++;
            } ?>
            <br /><br />
            <?php
            }
        }

But only the first seat among the selected seats is being highlighted(like 5th seat as shown in screenshot). Rest remains unchanged.

Screenshot Seats Selection

I think the problem is with the looping of foreach. Am i doing it in the wrong way? Or is there any other method? Any help would be greatly appreciated.

I'm using datamapper(db operations) for codeigniter(php framework).

tereško
  • 58,060
  • 25
  • 98
  • 150
Bhuvan Rikka
  • 2,683
  • 1
  • 17
  • 27

3 Answers3

4

If I understand your task, I see this as a really bad way to achieve it.

Try making an array with all the highlighted seats first:

$highlighted = array();
foreach($seat_data as $seat) {
    $highlighted[] = $seat->iSeatNumber;
}

$k = 1;
for($i=1;$i<=10;$i++)
{
    for($j=1;$j<=10;$j++)
    {
        if(in_array($k, $highlighted))
        {
             echo '<div id="'.$k.'" class="seat_selection" style="min-height:25px;min-width:25px;background-color:#12B22F;float:left;margin:0 0 5px 10px;cursor:pointer;">'.$k.'</div>';
        } else {
             echo '<div id="'.$k.'" class="seat_selection" style="min-height:25px;min-width:25px;background-color:#969696;float:left;margin:0 0 5px 10px;cursor:pointer;">'.$k.'</div>';
        }
        $k++;
    }
    echo "<br/><br/>";
}

It's much cleaner. Though I didn't test this it should work.

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
  • Thanks mate.. working great :) Thanks for the hint. I was equating a single value to an array :O. `in_array()` is the right way of doing it – Bhuvan Rikka Aug 22 '12 at 07:05
2

This is an example similar to Vlakarados' but only uses one loop. The idea is that you build an array of the highlighted seat IDs, then check if $i is in this array when looping, and if so, add the highlighted class.

Instead of having all that inline CSS you could create a class for highlighted/not highlighted, like in the example below.

$highlightedSeats = array(2, 6, 12, 21, 44);
for($i=1; $i<=100; $i++) { 
    $class = (in_array($i, $highlightedSeats)) ? 'highlighted' : 'not-highlighted';
    echo '<div id="' . $i . '" class="seat_selection ' . $class . '">' . $i . '</div>';
    if($i % 10 == 0) {
        echo '<br /><br />';
    } 
} 
billyonecan
  • 20,090
  • 8
  • 42
  • 64
  • Yes, even a better solution for this task, but I think using mod to put line breaks is a bit less multipurpose, anyway, I would use your way myself. – Sergey Telshevsky Aug 22 '12 at 08:11
0

Try change to:

if ($mode == 'EDIT'){
$k=1;
for($i=1;$i<=10;$i++){
for($j=1;$j<=10;$j++){ ?>
<?php foreach($seat_data as $seats){
if($seats->iSeatNumber == $k){
$style="min-height:25px;min-width:25px;background-color:#12B22F;float:left;margin:0 0 5px 10px;cursor:pointer;";
}
else{
$style='min-height:25px;min-width:25px;background-color:#969696;float:left;margin:0 0 5px 10px;cursor:pointer;';
} 
}
?>
<div id='<?php echo $k ;?>' class="seat_selection" style="<?php echo $style; ?>">
<?php echo $k;?>
</div>
<?php $k++;
} ?>
<br /><br />
<?php
}
}
icecream
  • 11
  • 1
  • 2