1

I am developing a movie ticket reservation system in codeigniter.The user can select his seat by checking the box on each available seat.When I try to store the checked valued in session data i get an error.Below is my code.

View select_seat

<!DOCTYPE html>
<html>
<head>

<title>Select Seats</title>
<link rel="stylesheet" type="text/css" href="http://localhost/cinema/assets/css/form2.css"/>
</head>
<body>
<form method="post" action="http://localhost/cinema/Movies/select_combo" name="selectshow" accept-charset="utf-8" class="username">
<?php

$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo "<table width='100%' border='0' cellpadding='0' cellspacing='2'>";
foreach($query as $key=>$result)
{
  $rowId = $result['rowId'];
  $status = $result['status'];
  $columnId = $result['columnId'];
  $typeID=$result['seat_type_id'];
if ($prevRowId != $rowId) {
 if ($rowId != 'A') {
  echo "</tr></table></td>";
  echo "\n</tr>";
}
 $prevRowId = $rowId;
 echo "\n<tr><td align='center'><table border='0' cellpadding='5' cellspacing='2'>      <tr>";
 } else {
$tableRow = false;
}
 if ($status == 0) {
 if($typeID==1)   
    $seatColor = "grey";
 else if($typeID==2)
  $seatColor="yellow";
else  
  $seatColor="white";
 } 
else {
 $seatColor = "red";
}

 echo "\n<td bgcolor='$seatColor' align='center'>";
 echo "$rowId$columnId";
 if ($status == 0)
{
 echo "<input type='checkbox' name='seats[]' value='$rowId$columnId'></checkbox>";
}
echo "</td>";
 if (($rowId == 'A' && $columnId == 6)
   || ($rowId == 'B' && $columnId == 7)
   || ($rowId == 'C' && $columnId == 7)
    || ($rowId == 'D' && $columnId == 7)
) {
// This fragment is for adding a blank cell which represent the "center aisle"
echo "<td> </td>";
}
 }
 echo "</tr></table></td>";
 echo "</tr>";
 echo "</table>";

mysql_close();
?>
<input type="submit" class="button-link" style="float:right; margin-top: 20px; margin-right:0px; margin-bottom: 10px;" value="Submit" />
    </form>
</body>
</html>

Model

 <?php 
 class Selectseat extends CI_Model {
   public function __construct()
   {
      $this->load->database();
   }

   function get() {
      return $this->db->query("SELECT * from seats order by rowId, columnId asc")
             ->result_array();
   }
 }
 ?>

Controller

 function select_combo(){
       foreach($this->input->post('seats[]') as $seat)
            $data['seats']=$seat;
    $this->session->set_userdata($data);

       $this->load->model('combo_model');
    $data['combos'] = $this->combo_model->get_combo();
    $this->load->view('header');
       $this->load->view('select_combo',$data);


   }

I'm getting this error

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/Movies.php

1 Answer 1

1

A quick finding:

You can't use as

$this->input->post('seats[]')

Instead

$seats = $this->input->post('seats');

Now seats having array of values.

You can change your code as below:

function select_combo(){
    $data['seats']=$this->input->post("seats");
    $this->session->set_userdata($data);

    $this->load->model('combo_model');
    $data['combos'] = $this->combo_model->get_combo();
    $this->load->view('header');
    $this->load->view('select_combo',$data);


   }
Sign up to request clarification or add additional context in comments.

4 Comments

yes. possible. check your html. And also </checkbox> strange. Is there any tag like this?
can u please tell me how can i use this session array to be stored in database in one column.
do you want to store multiple value in a single column?If so, you can implode the data with some separator like comma and store in db
I tried this in my model foreach($this->session->userdata('seats') as $seat) { $seats['seats_booked']=$seat; } .....then $data = array( 'Show_ID'=>$this->show_id($movie,$date), 'User_ID'=>$this->session->userdata['logged_in']['id'], 'seats_booked'=>$seats, 'combo_id'=>$combo_name); $insert = $this->db->insert('ticket', $data); return $insert; but i got a database error Message: Array to string conversion Filename: mysql/mysql_driver.php

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.