Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having trouble with my table as I run it through the for loop. Currently I am getting the following output:

Room(s)   Check-In    Check-Out   No. of Nights   Rate

Room1 04-09-2014  05-09-2014  1   $0

Room2 04-09-2014  05-09-2014  1   $0

Room3 04-09-2014  05-09-2014  1   $100.00

Room4 04-09-2014  05-09-2014  1   $100.00

What I want it to look like is this:

Room(s)   Check-In    Check-Out   No. of Nights   Rate

Room3 04-09-2014  05-09-2014  1   $100.00

Room4 04-09-2014  05-09-2014  1   $100.00

The array looks like this:

Array
(
    [1] => 0
    [2] => 0
    [3] => 100.00
    [4] => 100.00
)

I want anything with an array value of 0 to be omitted but if I change the value of 'X' I get one too few or one too many rows.

The 0 values are generated from checkbox posts <input type="hidden" name="chk" value="0"> and not from the SQL statement.

I have tried: if ($_SESSION['r_id'][$x] != 0) it gives:

Room(s)   Check-In    Check-Out   No. of Nights   Rate

Room1 04-09-2014  05-09-2014  1   $100.00  <-- Should be Room3

Room2 04-09-2014  05-09-2014  1   $100.00  <-- Should be Room4

HTML/SQL/PHP:

   <table width="500" border="0" align="center">
              <tr>
                <td>Room(s)</td>
                <td>Check-In</td>
                <td>Check-Out</td>
                <td>No. of Nights</td>
                <td>Rate</td>
              </tr>
              <tr>
                <?

                $sth = $dbh->prepare("SELECT * FROM room");
                $sth->execute();
                $result = $sth->fetchAll();

                $y = 0;
                for ($x=1; $x<=count($_SESSION['r_id']); $x++) { 

                ?>
                <tr>
                <td>
                    <!-- Modal Content -->
                    <div id="r_id_<?=$y?>" class="reveal-modal">
                         <h1><?=$result[$y]['r_type'];?></h1> 
                         <p><?=$result[$y]['r_desc'];?></p>
                         <h2>$<?=$result[$y]['r_rate'];?></h2> 
                         <a class="close-reveal-modal">&#215;</a>
                    </div><!-- Modal Content End -->

                    <!-- Modal Link -->
                    <a href="#" data-reveal-id="r_id_<?=$y?>"><?=$result[$y]['r_type'];?></a>
                    <!-- Modal Link End --></td>

                <td><?=$_SESSION['checkin_date'];?></td>
                <td><?=$_SESSION['checkout_date'];?></td>
                <td><?=$_SESSION['no_nights'];?></td>
                <td>$<?=$_SESSION['r_id'][$x];?></td>
              </tr>
              <?
              $y++;
              }       
              ?>
            </table>
share|improve this question

3 Answers 3

up vote 1 down vote accepted

If you are asking how to remove an element in the array having a specific value you can do like,

Having this array:

$arr = array('0', '0', '100.00', '100.00');

You can do:

$arr = array_diff($arr, array('0'));

And the value of $arr will be:

array('100.00', '100.00')


Edit:

As per the result, your $y is not getting incremented. Close your if before $y++. Check if below works.

<?php
  $y = 0;
  for ($x=1; $x<=count($_SESSION['r_id']); $x++) {

  if($_SESSION['r_id'][$x] != 0)
  {
?>

<tr>
    <td>
       <!-- Modal Content -->
       <div id="r_id_<?=$y?>" class="reveal-modal">
           <h1><?=$result[$y]['r_type'];?></h1> 
           <p><?=$result[$y]['r_desc'];?></p>
           <h2>$<?=$result[$y]['r_rate'];?></h2> 
           <a class="close-reveal-modal">&#215;</a>
       </div>
       <!-- Modal Content End -->

       <!-- Modal Link -->
       <a href="#" data-reveal-id="r_id_<?=$y?>"><?=$result[$y]['r_type'];?></a>
       <!-- Modal Link End -->
    </td>

    <td><?=$_SESSION['checkin_date'];?></td>
    <td><?=$_SESSION['checkout_date'];?></td>
    <td><?=$_SESSION['no_nights'];?></td>
    <td>$<?=$_SESSION['r_id'][$x];?></td>
</tr>

<?php
    }
    $y++;
  }       
?>
share|improve this answer
    
I have tried different variations of subtracting 0's from the array. When I remove the 0 values, all columns except the first column become correct (see example above). Room1 and Room2 are shown instead of Room3 and Room4. –  Accolade 2 days ago
    
can you show me the dump of $_SESSION? –  Parag Tyagi 2 days ago
    
array(4) { [1]=> string(1) "0" [2]=> string(1) "0" [3]=> string(6) "100.00" [4]=> string(6) "100.00" } –  Accolade 2 days ago
1  
Try edits and see if it works. –  Parag Tyagi 2 days ago
1  
Awesome, thank you dude! –  Accolade 2 days ago

You could update your SQL statement to only select results where the price is not 0. Something like this:

SELECT * FROM room WHERE rate NOT IN (0)

Or as @andrew suggested, just choose greater than:

SELECT * FROM room WHERE rate > 0

share|improve this answer
    
wouldn't be faster if you did rate>0? Since the items are hotel rates, i don't think it will be possible to have rate<0. –  andrew 2 days ago
    
@andrew true! Updated answer –  iswinky 2 days ago
    
Sorry, I should have said, the zeros are generated from checkboxes <input type="hidden" name="chk" value="0"> not from the SQL statement. –  Accolade 2 days ago
$RoomAvail=array("0","0","0","100","100");
$zeroValue="0";
$_SESSION['r_id']=array_diff($messages, [$del_val]); //Its avoid zero value

then u do each instead loop

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.