Welcome Guest, Not a member yet? Register   Sign In
Variable passed but no read
#1

[eluser]gbar[/eluser]
Hello. Sorry but I have a problem really mysterious, I can not explain.
I have a very simple case, I'm trying to create a search field in my crud.

From the view file, I click the button of my search, i have one select and one input, with this code:

Code:
<?php echo form_open('admin/sections/authors'); ?>
                    <?php
                    $options = array(
                      'id' => 'Id',
                      'author' => 'Author',
                      'biography' => 'Biography',
                      'site' => 'Site Url'
                    );
                    ?>
                    <?php echo form_dropdown('field', $options, 'author'); ?>
                    <?php echo form_input('search', $search); ?>
                    <?php echo form_submit('submit', 'Search'); ?>
                <?php echo form_close(); ?>

In the controller, I have this code that receives data from the above view file and calls the function in the model:

Code:
function authors()
        {
            $limit = '5';
            $offset = '0';
            $sortBy = 'id';
            $sortOrder = 'desc';
            $search = $this->input->post('search');
            $field = $this->input->post('field');
            
            $results = $this->authors_model->_retrieveAuthors($sortBy, $sortOrder, $limit, $offset, $search, $field);
-----
-----
-----
}

In the model file, the function is:

Code:
function _retrieveAuthors($sortBy, $sortOrder, $limit, $offset, $search, $field)
        {
            $field = 'author';
            $query = $this->db->select('id, author, biography, site, status')
                ->from('authors')
                ->like($field, $search)
                ->order_by($sortBy, $sortOrder)
                ->limit($limit, $offset);
            
            $data['rows'] = $query->get()->result();
}

You may have noticed that this:

Code:
$field = 'author';

is a stretch, it should work perfectly without it.

Unfortunately, if I remove that line, I get this damn error:

Fatal error: Call to a member function result() on a non-object in /.../.../.../.../models/authors_model.php on line 47

For me it is a mystery, I can not explain. I'm sure that $field variable is passed correctly, because I've re-sending from this model function to the controller and then passed back to the view file, and is read correctly. Only $field not work.

Can anyone help me? Thanks
#2

[eluser]InsiteFX[/eluser]
Thats because of this line, the field is being used:
Code:
->like($field, $search)

InsiteFX
#3

[eluser]Josh Holloway[/eluser]
I think the $field variable should be passed from the controller to the model
Code:
$field = $this->input->post('field');          
$results = $this->authors_model->_retrieveAuthors($sortBy, $sortOrder, $limit, $offset, $search, $field);

gbar what happens if you echo the $field variable in the controller to see if the
Code:
$this->input->post('field');
is being picked up?
#4

[eluser]gbar[/eluser]
hello, thanks for the replies.

For Josh Holloway:

What you suggest me, if I well understand it, I do already:

Code:
$field = $this->input->post('field');
$results = $this->authors_model->_retrieveAuthors($sortBy, $sortOrder, $limit, $offset, $search, $field);

I am sure that the variable $field is passed well, because I tried to return it to the controller and from the controller to the view, it is displayed perfectly.

For InsiteFX:

Honestly, I did not understand very well what you tell me ...

The fact is that all the variables from the controller to step model, $sortBy, $sortOrder, $limit, $offset and $search, all work perfectly.

One might object to the data type .... $field is a post data, just like $search ....

But it comes from the same form of $search, is initialized in the same way as $search, passed in the same way of $search, why $search is working and $field not working in like() active records?

I also tried to change the name thinking it was a reserved word, then some problems of conflict, but it's the same thing ...

Thanks again for your attention
#5

[eluser]Josh Holloway[/eluser]
Logically, it can only really be that the $field var isn't being passed to the model.

If you echo $field from the model, do you actually see what you've posted?
#6

[eluser]gbar[/eluser]
Hi, see this code:

Code:
function _retrieveAuthors($sortBy, $sortOrder, $limit, $offset, $search, $field)
{
   echo $field;
   $query = $this->db->select('id, author, biography, site, status')
                ->from('authors')
                ->like('author', $search)
                ->order_by($sortBy, $sortOrder)
                ->limit($limit, $offset);
            
   $data['rows'] = $query->get()->result();
}

This is the code that I use to check if the variable is passed.

Of course, in clause like(), I've put the real name of the table, for not to cause the error, otherwise I would not see the variable passed.

But the passage of $field remains, and the echo taht you see as the second line produces the name of the variable actually passed.

I'm breaking my head ...
#7

[eluser]Josh Holloway[/eluser]
OK, so I'd say modify it like this:
Code:
function _retrieveAuthors($sortBy, $sortOrder, $limit, $offset, $search, $field)
{
   $query = $this->db->select('id, author, biography, site, status')
                ->like($field, $search)
                ->order_by($sortBy, $sortOrder)
                ->limit($limit, $offset)
                ->get('authors');

   if ( $query->num_rows() < 0 )
   {
        echo 'error';
   }

   $data['rows'] = $query->result();
}

if error is echo'd no results have been returned and you need to check the query to ensure that it's a valid query which will return results.
#8

[eluser]toopay[/eluser]
What if you do the variable validation, like
Code:
function _retrieveAuthors($sortBy, $sortOrder, $limit, $offset, $search, $field = '')
{
   $key_field = $field == '' ? 'some_default_field' : $field;
   $query = $this->db->select('id, author, biography, site, status')
                ->from('authors')
                ->like($key_field, $search)
                ->order_by($sortBy, $sortOrder)
                ->limit($limit, $offset);
            
   $data['rows'] = $query->get()->result();
}
#9

[eluser]gbar[/eluser]
I copy and paste your code, and i receive this error:

Fatal error: Call to a member function num_rows() on a non-object in /..././.../.../models/authors_model.php on line 46

now rewrite the query from scratch and then come back here.

Thank You
#10

[eluser]gbar[/eluser]
I had not had time to read your post ...
I do not know who you are and what planet you come from ... damn is working!

Can you explain why this? Why two variables of the same type ($search and $field) initialized and passed in the same way, need to have two different treatments?
what do you think is the difference?

Thanks to all you guys!




Theme © iAndrew 2016 - Forum software by © MyBB