Welcome Guest, Not a member yet? Register   Sign In
CI duplicate my results
#1

[eluser]a77icu5[/eluser]
Well, this is my example:
Code:
$data['first'] = $this->model->getAll();
$data['second'] = $this->model->getSingle($id);

echo '<pre>';
print_r($data);
echo '</pre>';

Array
(
    [first] => Array
        (
            [0] => stdClass Object
                (
                    [id_table1] => 1
                    [value_table1] => Some value here
                )
            [1] => stdClass Object
                (
                    [id_table1] => 2
                    [value_table1] => Other different value here
                )
    [second] => Array
        (
            [0] => stdClass Object
                (
                    [id_table1] => 1
                    [value_table1] => Some value here
                )
            [1] => stdClass Object
                (
                    [id_table1] => 2
                    [value_table1] => Other different value here
                )
            [2] => stdClass Object
                (
                    [id_table2] => 2
                    [value_table2] => Other different value here
                )
        )
)
CI puts the same result of $this->model->getAll() in the second index and then at the end put the single result of the $this->model->getSingle($id),this duplicate results in the second index, and when use foreach to display results of the second index in my view there's an error because in the positions 1 y 2 the property id_table2 doesn't exist, someone can tell why CI do this?
#2

[eluser]mddd[/eluser]
I think it is not CI doing this, but you are doing something wrong in your model. Maybe you are using some variable that you are not clearing up before doing the second query?
#3

[eluser]pickupman[/eluser]
[quote author="mddd" date="1275566198"]I think it is not CI doing this, but you are doing something wrong in your model. Maybe you are using some variable that you are not clearing up before doing the second query?[/quote]
I would say it probably something do with a where statement. How about posting the code from your model, so we can help you.

Please use the code button on the post page when posting code on the boards. It will format and syntax highlight it for us.
#4

[eluser]a77icu5[/eluser]
this is my model code

Code:
class Some_Model extends Model
{
    private $_query = NULL;
    private $_rows  = array();

    public function __construct() {
       parent::Model();
    }

    public function getAll() {
       $this->_query = $this->db->get('table1');
       foreach($this->_query->result() as $row) {
           $this->_rows[] = $row;
       }
       $this->_query->free_result();
       return $this->_rows;
    }

    public function getSingle($id) {
       $this->db->where('id_table2', $id);
       $this->_query = $this->db->get('table2');
       $this->_rows = $this->_query->row();
       $this->_query->free_result();
       return $this->_rows;
    }
}
#5

[eluser]pickupman[/eluser]
You may need to make sure you are check if $id is set. If not, you may be selecting all rows. Try this:
Code:
class Some_Model extends Model
{
  private $_query = NULL;
  private $_rows = array();

  public function __construct() {
    parent::Model();
  }

  public function getAll() {
    $this->_query = $this->db->get('table1');
    
    if($this->_query->num_rows() > 0)
        return $this->_query->result_array();
  
    return FALSE;
  }

  public function getSingle($id) {
    
    if(empty($id))
       return FALSE;

    $this->db->where('id_table2', (int)$id);
    $this->_query = $this->db->get('table2');
    
    if($this->_query->num_rows() > 0)
        return $this->_query->row();
        
    return FALSE;
  }
}

Again...please try to use the code button at the top of the editor when posting code.
#6

[eluser]a77icu5[/eluser]
The problem was that when I call getAll() $_rows is empty, but when I call getSingle() $_rows even contain values, in that case I just had to set $_rows again as an empty array, thanks for the answers =D and sorry I forgot use the highlight.

Code:
class Some_Model extends Model
{
  private $_query = NULL;
  private $_rows = array();

  public function __construct() {
    parent::Model();
  }

  public function getAll() {
    $this->_query = $this->db->get(‘table1’);
    foreach($this->_query->result() as $row) {
        $this->_rows[] = $row;
    }
    $this->_query->free_result();
    return $this->_rows;
  }

  public function getSingle($id) {
    $this->_rows = array();
    $this->db->where(‘id_table2’, $id);
    $this->_query = $this->db->get(‘table2’);
    $this->_rows = $this->_query->row();
    $this->_query->free_result();
    return $this->_rows;
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB