Welcome Guest, Not a member yet? Register   Sign In
save data from a view
#1

[eluser]Unknown[/eluser]
I'm using codeigniter to develop a website.

The query result is successfully returned from database, I use a controller to pass the result to a view to display them. and then I want to save the result into files. but I don't how to save the data. I tried to send the data back to a controller, but the variable(the returned result) couldn't be found.
In file query_view.php I set some criteria to let user choose

Code:
<? echo form_open('query/select') ;?>
                    <select name='name'>
                        <option value='food' selected="selected">food</option>
                    </select>
       &lt;input type="submit" submit&gt;;
    &lt;?php echo form_close(); ?&gt;;

In the controller 'query', I have the 'select' function:

Code:
function select() {
        global $data;
        $NAME = $this->input->post('name');
        $quer = "SELECT * FROM {$TABLENAME} WHERE NAME={$NAME}";
        $this->load->model('Mquery');
        $data['query'] = $this->Mquery->select($quer);
        $this->load->view('query_result', $data);
     }

In the "query_result" view, I can display the returned result, but after that I want to save the data, how to do that?

Q1: Should I send the data back to controller? how?
Q2: Should I use javascript to do this on client? How?


Any suggestions would be sincerely appreciated.
#2

[eluser]timpiele[/eluser]
Is the data being changed when it is in the view?

If not, just save it from the controller before you load the view:

Code:
// Use the file helper
$this->load->helper('file');

// write data to the file
write_file('./path/to/file.php', $data);

// then load the view
$this->load->view("query_view"< $data);
#3

[eluser]Unknown[/eluser]
[quote author="chemiks" date="1331003241"]Is the data being changed when it is in the view?

If not, just save it from the controller before you load the view:

Code:
// Use the file helper
$this->load->helper('file');

// write data to the file
write_file('./path/to/file.php', $data);

// then load the view
$this->load->view("query_view"< $data);
[/quote]

Thank you Chemiks, your method looks interesting. Yes, currently, I don't need to modify the data in the view. In your method, Is it true that no matter the $data is object or string, it will both work?

I'm using a method in the below:
Code:
&lt;?$query_array=$query->result_array();?&gt;
        &lt;? echo form_open($action = 'query/save');?&gt;
        &lt;input type="hidden" name ="save_result" value="&lt;?php echo htmlentities(serialize($query_array)) ?&gt;"&gt;
        <p align="center">&lt;input type="submit" value="save"&gt;&lt;/p>
        &lt;? echo form_close(); ?&gt;

In this way, I can transfer the data to the controller and then load them in other views.

But as you mentioned, if I want to modify the data in a view, which way do you think is better? do I need some javascript? Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB