Welcome Guest, Not a member yet? Register   Sign In
get different tables data in different array index in a single method in a model
#1

[eluser]ranjitbd[/eluser]
Code:
// this is a controller name holiday.php

function search_holiday()
    {
        $data = array();
        $data['view_pack'] = $this->holiday->view_package();
        
        echo "<pre>";
        print_r($data['view_pack']);

        $data['mainContent'] = $this->load->view('holiday/search_result', $data, TRUE);
        $this->load->view('index', $data);
    }


// this is the model name holiday_model.php

function view_package()
    {    

        $sql = "select * from theme_name";
        $query['theme'] = $this->db->query($sql);
        
        $sql2 = "select * from destination";
        $query['destination'] = $this->db->query($sql2);

//        return $query->result_array();// this line produce the following errors
//Fatal error: Call to a member function result_array() on a non-object

        return $query; // this line does not shows any error. but when this $query array goes back
// to controller and print the array through this line print_r($data['view_pack']); it does not print the
// table value.it prints.

/*
    Array
(
    [theme] => CI_DB_mysql_result Object
        (
            [conn_id] => Resource id #36
            [result_id] => Resource id #60
            [result_array] => Array
                (
                )

            [result_object] => Array
                (
                )

            [current_row] => 0
            [num_rows] => 0
        )

    [destination] => CI_DB_mysql_result Object
        (
            [conn_id] => Resource id #36
            [result_id] => Resource id #63
            [result_array] => Array
                (
                )

            [result_object] => Array
                (
                )

            [current_row] => 0
            [num_rows] => 0
        )

)
// i need here the table data. how?

*/

    }


// i want to send two table data to controller via view_package() method in the holiday_model.php
// i cant join two tables....so i have to send indivisually like what my coding shows


//practice 2
// instead of two queries if i set two array in the same model and return the array to the controller
// and print the array through this line print_r($data['view_pack']); in the controller..it shows proper
// value

function view_package()
    {    

        $a['small'] = array(array(1,2,3), array(4,5,6));
        $a['big']  = array(array(7,8,9), array(10,11,12));
        
        return $a;
    }
// it shows
/*
    
Array
(
    [small] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )

            [1] => Array
                (
                    [0] => 4
                    [1] => 5
                    [2] => 6
                )

        )

    [big] => Array
        (
            [0] => Array
                (
                    [0] => 7
                    [1] => 8
                    [2] => 9
                )

            [1] => Array
                (
                    [0] => 10
                    [1] => 11
                    [2] => 12
                )

        )

)

*/
#2

[eluser]Krzemo[/eluser]
Men... what a mess! I don't get at all what you want to achieve.
I guess you should rewrite your question if you want to have answers that make sense...
#3

[eluser]ranjitbd[/eluser]
[quote author="ranjitbd" date="1256996435"][code]
// this is a controller name holiday.php

function search_holiday()
{
$data = array();
$data['view_pack'] = $this->holiday->view_package();

echo "<pre>";
print_r($data['view_pack']);

// here i want to get the array value? which is sent by holiday_model.
}


// this is the model name holiday_model.php

function view_package()
{

$sql = "select * from theme_name";
$query['theme'] = $this->db->query($sql);

$sql2 = "select * from destination";
$query['destination'] = $this->db->query($sql2);

return $query->result_array();
// why this line not working ? produce the following errors
//Fatal error: Call to a member function result_array() on a non-object
// so how can i sent $query to controller....

// return $query; // this line does not shows any error.but when i print
// from the controller through this line print_r($data['view_pack']); it does not print // the table value.it prints.

/*
Array
(
[theme] => CI_DB_mysql_result Object
(
[conn_id] => Resource id #36
[result_id] => Resource id #60
[result_array] => Array
(
)

[result_object] => Array
(
)

[current_row] => 0
[num_rows] => 0
)

[destination] => CI_DB_mysql_result Object
(
[conn_id] => Resource id #36
[result_id] => Resource id #63
[result_array] => Array
(
)

[result_object] => Array
(
)

[current_row] => 0
[num_rows] => 0
)

)

*/

}
#4

[eluser]ranjitbd[/eluser]
// this is a controller name holiday.php

function search_holiday()
{
$data = array();
$data[‘view_pack’] = $this->holiday->view_package();

echo “”;
print_r($data[‘view_pack’]);

// here i want to get the array value? which is sent by holiday_model.
}


// this is the model name holiday_model.php

function view_package()
{

$sql = “select * from theme_name”;
$query[‘theme’] = $this->db->query($sql);

$sql2 = “select * from destination”;
$query[‘destination’] = $this->db->query($sql2);

return $query->result_array();
// why this line not working ? produce the following errors
//Fatal error: Call to a member function result_array() on a non-object
// so how can i sent $query to controller….
// return $query; // this line does not shows any error.but when i print
// from the controller through this line print_r($data[‘view_pack’]); it does not print // the table value.it prints.

/*
Array
( [theme] => CI_DB_mysql_result Object
(
[conn_id] => Resource id #36
[result_id] => Resource id #60
[result_array] => Array
(
)

[result_object] => Array
(
)

[current_row] => 0
[num_rows] => 0
)

[destination] => CI_DB_mysql_result Object
(
[conn_id] => Resource id #36
[result_id] => Resource id #63
[result_array] => Array
(
)

[result_object] => Array
(
)

[current_row] => 0
[num_rows] => 0
)

)

*/
// i want to see here data from theme_name table and destination table...
}
#5

[eluser]ranjitbd[/eluser]
Code:
// this is a controller name holiday.php

function search_holiday()
  {
    $data = array();
    $data[‘view_pack’] = $this->holiday->view_package();
  
    echo “”;
    print_r($data[‘view_pack’]);
  
// here i want to get the array value? which is sent by holiday_model.
  }


// this is the model name holiday_model.php

function view_package()
  {

    $sql = “select * from theme_name”;
    $query[‘theme’] = $this->db->query($sql);
  
    $sql2 = “select * from destination”;
    $query[‘destination’] = $this->db->query($sql2);

    return $query->result_array();
// why this line not working ? produce the following errors
//Fatal error: Call to a member function result_array() on a non-object
// so how can i sent $query to controller….
//    return $query; // this line does not shows any error.but when i print
// from the controller through this line print_r($data[‘view_pack’]); it does not print // the table value.it prints.

/*
  Array
(  [theme] => CI_DB_mysql_result Object
    (
      [conn_id] => Resource id #36
      [result_id] => Resource id #60
      [result_array] => Array
      (
      )

      [result_object] => Array
      (
      )

      [current_row] => 0
      [num_rows] => 0
    )

  [destination] => CI_DB_mysql_result Object
    (
      [conn_id] => Resource id #36
      [result_id] => Resource id #63
      [result_array] => Array
      (
      )

      [result_object] => Array
      (
      )

      [current_row] => 0
      [num_rows] => 0
    )

)

*/
// i want to see here data from theme_name table and destination table…
  }
#6

[eluser]saidai jagan[/eluser]
You might use like this

$query = 'SELECT * FROM table1';
$result1 = $this->db->query($query);

$query = 'SELECT * FROM table2';
$result2 = $this->db->query($query);

return (array('result_1' => $result1->result_array(),'result_2' => $result2->result_array()));
#7

[eluser]Ben Edmunds[/eluser]
Try this model

Code:
$sql = “select theme, destination from theme_name”;
$query = $this->db->query($sql);
  
return $this->db->result_array();

then in your controller

Code:
$data = array();
$data[‘view_pack’] = $this->holiday->view_package();

/*
   $data['view_pack'] will now contain:
   array(theme       => 'whatever',
         destination => 'whatever');
*/

print_r($data[‘view_pack’]);


That model will select the two fields you need from the table and return them as an array. Then the controller is printing the array.
#8

[eluser]Pascal Kriete[/eluser]
Hi ranjitbd. I've merged these 3 threads as they all pertain to the same question.

Having it in one spot makes it easier on everyone. Please refrain from double posting in the future.

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB