Welcome Guest, Not a member yet? Register   Sign In
How to pass a value as id that is being fetched by another query.
#1

[eluser]Unknown[/eluser]
Hi Friends. I am having problem with a part of program. I have just started learning CI so guys pls help. I had fetched data from a table and can show that n a view but i want another query to run that need a value as an id from the before fetched row. My question is how to pass that value as an id in a function that would perform a fetch query.In short how can i pass a value from view to model[probably through controller].
For example in procedural approach it would be like this
Code:
$sql1="select * from table1";
   $result1=mysql_query($sql1);
   while($row1=mysql_fetch_array($result1))
   {
     $sql2="select * from table2 where id='$row1[id]'";
     // So how can I get this $row1[id] in code igniter in MCV pattern so
     // that i can fetch from another table with that id
     .
     .
   }
#2

[eluser]aquary[/eluser]
Do everything you need in the controller before send them to the view.

Your current way is not MVC. Try to use everything CI offer, at least the database class .-.

btw, MVC is Model<=>Controller=>View. Originally, You cannot call the Controller function directly from the view. Either put those part in a library (if needed), or prepare everthing related to data before loading the view.
#3

[eluser]InsiteFX[/eluser]
Are you retriving just one record or all records from the database?

Do you want to just retrive one record from each table?
Because above you are looping through the whole record set.
#4

[eluser]aquary[/eluser]
^---------------

He was trying to loop throgh the record set, then run a query per row in the record set, using their ID.
#5

[eluser]vincej[/eluser]
I have read your question 4 times and I am struggling to understand it clearly. I think you are struggling with the MVC pattern. 6 months ago I started CI. The Best thing you can do is buy a book called 'professional Codeigniter' by Tom Meyer. It is invaluable for getting started. Then you also need to really study the user guide. It is also excellent.

Essentially the pattern goes like this:

1 - Your controller calls a model requesting data - check the user guide for exact syntax.
2 - Your model grabs data and does a return in a variable.
3 - Your controller passes the data to the view you have featured in the controller without any specific extra syntax.
4 - If you need the data to go somewhere else then you must call that controller who in turn will pass it over to their view.

Here is an example taken from my own code :

Controller:

Code:
Controller:

function index(){
  $data['title'] = "Welcome to Country Wide";
$data['navlist'] = $this->MCats->getCategoriesNav();
$data['main'] = 'home';
$this->load->vars($data);
$this->load->view('template');
  }



Model:
function getCategoriesNav(){
     $data = array();
     $this->db->select('id,name,parentid');
     $this->db->where('status', 'active');
     $this->db->order_by('parentid','asc');
     $this->db->order_by('name','asc');
     $this->db->group_by('parentid,id');
     $Q = $this->db->get('categories');
     if ($Q->num_rows() > 0){
       foreach ($Q->result() as $row){
   if ($row->parentid > 0){
    $data[0][$row->parentid]['children'][$row->id] = $row->name;
  
   }else{
    $data[0][$row->id]['name'] = $row->name;
   }
  }
    }
    $Q->free_result();
    return $data;
}









Theme © iAndrew 2016 - Forum software by © MyBB