Welcome Guest, Not a member yet? Register   Sign In
Passing variable with header redirection in mvc format of php
#1

[eluser]rash[/eluser]
Hi All,

can anyone suggest me that how can i pass variables from controller to view page with redirection code..
my controller code is like this:

\[code\]

public function show_rooms()
{
$stud_id=$_POST['stud_id'];
$hostel_id=$_POST['hostel_id'];
$this->view->roomlist=$this->model->hostelroomList($hostel_id);
$this->view->render('hostel/room_allocate');
}

\[/code\]

I want to navigate room_allocate page with hostel_id and stud_id..
Thanks for any help
#2

[eluser]hungdp[/eluser]
Example:
Code:
$data['stud_id'] = $_POST['stud_id'];
$data['hostel_id'] = $_POST['hostel_id'];
$this->load->view('hostel/room_allocate', $data);

In view file
Code:
<div class="stud_id">
      &lt;?=$stud_id?&gt;
  </div>
  <div class="hostel_id">
      &lt;?=$hostel_id?&gt;
  </div>
Hope so help u !!
#3

[eluser]rash[/eluser]
Hi hungdp

Thanks for youe reply.
I tried what u said but it didn't working..Check my code..

in Controller.php
\[code\]

public function room_allocation()
{
$data = array();
$data['student_id'] = $_POST['student_id'];
$data['hostel_id'] = $_POST['hostel_id'];
$hostel_id=$_POST['hostel_id'];
$this->view->hostellist=$this->model->hostelList();
$this->view->studentlist=$this->model->studentList();
$this->view->hostelroomlist=$this->model->hostelroomList($hostel_id);
$this->view->render('hostel/room_allocation', $data);
}

\[/code\]

in view.php here is my code

\[code\]

<table width="100%" border="0" cellspacing="3" cellpadding="2">
<tr>
<td>Student Name :</td>
<td><select name="student_id" id="student_id">
<option value="0">-- Select --</option>
&lt;?php
foreach($this->studentlist as $key=> $value){?&gt;
<option value="&lt;?php echo $value['student_id']; ?&gt;"&lt;?php if($student_id==$value['student_id']){ echo " selected"; }?&gt;>&lt;?php echo $value['student_name']; ?&gt;</option>
&lt;?php } ?&gt;
</select></td>
</tr>
<tr>
<td width="29%">Hostel Name :</td>
<td width="71%"><select name="hostel_id" id="hostel_id">
<option value="0">-- Select --</option>
&lt;?php
foreach($this->hostellist as $key1=> $value1){?&gt;
<option value="&lt;?php echo $value1['hostel_id']; ?&gt;"&lt;?php if($hostel_id==$value1['hostel_id']){ echo " selected"; }?&gt;>&lt;?php echo $value1['hostel_name']; ?&gt;</option>
&lt;?php } ?&gt;
</select></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&lt;input type="submit" name="submit" id="submit" value="Show Rooms" /&gt;&lt;/td>
</tr>
</table>

<table width="100%" border="0" cellspacing="3" cellpadding="2">
<tr>
<td><strong>SL No.</strong></td>
<td><strong>Room No.</strong></td>
<td><strong>Students Per Room</strong></td>
<td><strong>Availability</strong></td>
<td><strong>Rent</strong></td>
<td><strong>Status</strong></td>
</tr>
&lt;?php
$slno=0;
//print_r($this->hostelroomlist);
foreach($this->hostelroomlist as $key2 => $value2) {
$slno++;
?&gt;
<tr>
<td>&lt;?php echo $slno;?&gt;</td>
<td>&lt;?php echo $value2['room_no']; ?&gt;</td>
<td>&lt;?php echo $value2['stud_proom']; ?&gt;</td>
<td>&lt;?php echo $value2['availability']; ?&gt;</td>
<td>&lt;?php echo $value2['rent']; ?&gt;</td>
<td>&lt;?php $avstatus=$value2['availability']; if($avstatus=="0"){echo "No Availability";}else{?&gt;<a href="&lt;?php echo URL;?&gt;hostel/stud_allocate/&lt;?php echo $value2['room_id'];?&gt;?stud_id=&lt;?php //echo $stud_id;?&gt;">Allocate</a>&lt;?php } ?&gt;</td>
</tr>
&lt;?php } ?&gt;
</table>

\[/code\]

but in dropdown it didn't showing the selected data which i selected last time..

thanks
#4

[eluser]TheFuzzy0ne[/eluser]
Is it really too much to ask that you use [code][/code] tags? If you want us to help you, help us!
#5

[eluser]rash[/eluser]
oh!!! Sorry, i got it...

Now please Check my code..

in Controller.php

Code:
public function room_allocation()
{
  $data = array();
  $data[‘student_id’] = $_POST[‘student_id’];
  $data[‘hostel_id’] = $_POST[‘hostel_id’];
  $hostel_id=$_POST[‘hostel_id’];
  $this->view->hostellist=$this->model->hostelList();
  $this->view->studentlist=$this->model->studentList();
  $this->view->hostelroomlist=$this->model->hostelroomList($hostel_id);
  $this->view->render(‘hostel/room_allocation’, $data);
}

in view.php here is my code

Code:
<table width=“100%” border=“0” cellspacing=“3” cellpadding=“2”>
  <tr>
  <td>Student Name :</td>
  <td><select name=“student_id” id=“student_id”>
    <option value=“0”>—Select—</option>
    &lt;?php
  foreach($this->studentlist as $key=> $value){?&gt;
    <option value=”&lt;?php echo $value[‘student_id’]; ?&gt;”&lt;?php if($student_id==$value[‘student_id’]){ echo " selected"; }?&gt;>&lt;?php echo $value[‘student_name’]; ?&gt;</option>
    &lt;?php } ?&gt;
  </select></td>
  </tr>
  <tr>
  <td width=“29%”>Hostel Name :</td>
  <td width=“71%”><select name=“hostel_id” id=“hostel_id”>
    <option value=“0”>—Select—</option>
    &lt;?php
  foreach($this->hostellist as $key1=> $value1){?&gt;
    <option value=”&lt;?php echo $value1[‘hostel_id’]; ?&gt;”&lt;?php if($hostel_id==$value1[‘hostel_id’]){ echo " selected"; }?&gt;>&lt;?php echo $value1[‘hostel_name’]; ?&gt;</option>
    &lt;?php } ?&gt;
  </select></td>
  </tr>
  <tr>
  <td> </td>
  <td>&lt;input type=“submit” name=“submit” id=“submit” value=“Show Rooms” /&gt;&lt;/td>
  </tr>
</table>

<table width=“100%” border=“0” cellspacing=“3” cellpadding=“2”>
  <tr>
  <td>SL No.</td>
  <td>Room No.</td>
  <td>Students Per Room</td>
  <td>Availability</td>
  <td>Rent</td>
  <td>Status</td>
  </tr>
  &lt;?php
    $slno=0;
  foreach($this->hostelroomlist as $key2 => $value2) {
  $slno++;
  ?&gt;
  <tr>
  <td>&lt;?php echo $slno;?&gt;</td>
  <td>&lt;?php echo $value2[‘room_no’]; ?&gt;</td>
  <td>&lt;?php echo $value2[‘stud_proom’]; ?&gt;</td>
  <td>&lt;?php echo $value2[‘availability’]; ?&gt;</td>
  <td>&lt;?php echo $value2[‘rent’]; ?&gt;</td>
  <td>&lt;?php $avstatus=$value2[‘availability’]; if($avstatus==“0”){echo “No Availability”;}else{?&gt;;?&gt;?stud_id=&lt;?php //echo $stud_id;?&gt;”]Allocate&lt;?php } ?&gt;</td>
  </tr>
  &lt;?php } ?&gt;
</table>

but in dropdown it didn’t showing the selected data which i selected last time..

thanks

#6

[eluser]TheFuzzy0ne[/eluser]
Is "selected" where you expect it to show in the source? If so, then you logic is correct. It's just that you have the syntax wrong.

You set an option as selected by using:
Code:
<option value="foo" selected="selected">bar</option>

Hope this helps.
#7

[eluser]rash[/eluser]
Hello,

i did it but, my problem is the variable which i send from controller to view page it did n't show in view page..
I did 't get that where thae problem is, can u help for this...

Code:
$this->view->render(‘hostel/room_allocation’, $data);

when i go to room_allocation page it shows blank page. i also on the error reporting but it shows like this.


Code:
( ! ) Notice: Undefined variable: student_id in D:\wamp\www\SDMS\views\hostel\room_allocation.php on line 18
Call Stack
# Time Memory Function Location
1 0.0006 371264 {main}( ) ..\index.php:0
2 0.0027 427360 Bootstrap->init( ) ..\index.php:21
3 0.0154 628232 Bootstrap->_callControllerMethod( ) ..\Bootstrap.php:31
4 0.0154 628264 Hostel->room_allocation( ) ..\Bootstrap.php:150
5 0.0185 655680 View->render( ) ..\hostel.php:131
6 0.0193 676048 require( 'D:\wamp\www\SDMS\views\hostel\room_allocation.php' ) ..\View.php:12

i didn't get it could u help me or give me another way to redirect a page with variable in mvc of php.

thanks
#8

[eluser]TheFuzzy0ne[/eluser]
I can't see where the problem is. I can only assume it has something to do with $this->view->render(). What library are you using to handle your views?

Are you sure it takes an array as the second parameter? What happens when you do this:

Code:
...
$this->view->student_id = $_POST['student_id'];
$this->view->hostel_id = $_POST['hostel_id'];
$this->view->render('hostel/room_allocation');
#9

[eluser]rash[/eluser]
hello

Yes,that problem was solved, as per your solution.
Thanks

But now my problem is how can i send more than one parameter from view to controller with
Code:
<a href=""></a>
link...like

Code:
<a href="&lt;?php echo URL;?&gt;hostel/stud_allocate/&lt;?php echo $value2['room_id'];?&gt;?stud_id=&lt;?php echo $student_id;?&gt;&hostelid;=&lt;?php echo $hostel_id;?&gt;"><strong>Allocate</strong></a>&lt;?php } ?&gt;

I want to send hostel_id, student_id and room_id to contoller page.

can u help me to solve this??

thanks
#10

[eluser]TheFuzzy0ne[/eluser]
I don't understand your problem. From the code you've posted, you have a pretty good idea of what to do.




Theme © iAndrew 2016 - Forum software by © MyBB