CodeIgniter Forums
on adding value in database last values is repeated as many times as rows are in table. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: on adding value in database last values is repeated as many times as rows are in table. (/showthread.php?tid=19101)



on adding value in database last values is repeated as many times as rows are in table. - El Forum - 05-28-2009

[eluser]alberta[/eluser]
Hi all,

I have some very strange problem. I am adding a valued in database by submitting a form. After form submisstion when go back to page where the result is shown, the last value added is repeated as many time as there are total no of rows in database.

this is code in controller part code.
Code:
$query = $this->db->get_where('candidates' , array('site_id' => $site_id));
        $data['candidates'] = $query->result_array();
on view page i've this code as em using template parser.

Code:
{candidates}
    <tr>
      <td><a href='{site_url}index.php/projects/rollout_details/{pid}/{id}'>{code}</a></td>
      <td>{latitude}</td>
      <td>{longitude}</td>
      <td>{isactive}</td>      
      <td><a href='../candidate_active/{id}'>Mark as Active</a></td>
    </tr>
{/candidates}

Before adding any value result is
value of {code}
A

B
But when i add one more code like E then output is
E

E

E

Why this is happening ??


on adding value in database last values is repeated as many times as rows are in table. - El Forum - 05-28-2009

[eluser]Colin Williams[/eluser]
1.) How is $site_id set? Could you be incorrectly setting it after submit?
2.) Have you examined the query result? Try print_r($query->result_array()) to see what is returned.


on adding value in database last values is repeated as many times as rows are in table. - El Forum - 05-28-2009

[eluser]alberta[/eluser]
when i use print_r($query->result_array()), then it shows result accurately. It means exact value is retrieved from database.
but on display page only last value is displayed equal to no of rows of result->array.

$data['candidates'] = $query->result_array();

on view page candidates value gives only last value. is there any way to reset value of candidates, why it displays just last value.


on adding value in database last values is repeated as many times as rows are in table. - El Forum - 05-28-2009

[eluser]Evil Wizard[/eluser]
how are you looping through the candidates array?
Code:
&lt;? foreach($candidates as $candidate):?&gt;
    <tr>
      <td>
           <a href="&lt;?= site_url(); ?&gt;index.php/projects/rollout_details/&lt;?=$candidate['pid']; ?&gt;/&lt;?= $candidate['id']; ?&gt;"> &lt;?= $candidate['code']; ?&gt;</a></td>
      <td>&lt;?=$candidate['latitude'];?&gt;</td>
      <td>&lt;?=$candidate['longitude'];?&gt;</td>
      <td>&lt;?=$candidate['isactive'];?&gt;</td>      
      <td><a href="../candidate_active/&lt;?=$candidate['id']">Mark as Active</a></td>
    </tr>
&lt;?endforeach;?&gt;
something like that should only produce rows for the elements withing the candidates array.


on adding value in database last values is repeated as many times as rows are in table. - El Forum - 05-28-2009

[eluser]Evil Wizard[/eluser]
[quote author="Evil Wizard" date="1243531325"]how are you looping through the candidates array?
Code:
&lt;? foreach($candidates as $candidate):?&gt;
    <tr>
      <td><a href="&lt;?=site_url();?&gt;index.php/projects/rollout_details/&lt;?=$candidate['pid'];?&gt;/&lt;?=$candidate['id'];?&gt;">&lt;?=$candidate['code'];?&gt;</a></td>
      <td>&lt;?=$candidate['latitude'];?&gt;</td>
      <td>&lt;?=$candidate['longitude'];?&gt;</td>
      <td>&lt;?=$candidate['isactive'];?&gt;</td>      
      <td><a href="../candidate_active/&lt;?=$candidate['id']">Mark as Active</a></td>
    </tr>
&lt;?endforeach;?&gt;
something like that should only produce rows for the elements withing the candidates array.[/quote]


on adding value in database last values is repeated as many times as rows are in table. - El Forum - 05-28-2009

[eluser]alberta[/eluser]
I am using template parser and for that i've no need of any loop.
the code written above works fine when i don't add any candidate and go to page. It only creates the problem when i add one more candidate and again go to that page.

and then value of $data[candidate] is not set properly.


on adding value in database last values is repeated as many times as rows are in table. - El Forum - 05-28-2009

[eluser]Evil Wizard[/eluser]
it sounds like the parser is expecting a multi dimensional array in candidates and on getting a single dimension array, e.g.
Code:
$candidates = array(
                    array(
                          'id'        =>    1,
                          'pid'       =>    12,
                          'code'      =>    'unique',
                          'latitude'  =>    '10.45',
                          'longitude' =>    '14.56',
                          'isactive'  =>    '0'
                          ),
                    array(
                          'id'        =>    2,
                          'pid'       =>    16,
                          'code'      =>    'unique',
                          'latitude'  =>    '16.47',
                          'longitude' =>    '7.98',
                          'isactive'  =>    '1'
                          )
                    );
$candidates = array(
                    'id'        =>    1,
                    'pid'       =>    12,
                    'code'      =>    'unique',
                    'latitude'  =>    '10.45',
                    'longitude' =>    '14.56',
                    'isactive'  =>    '0'
                    );
could it be that when you add the record, it only returns the last inserted record and not the full collection, but you said that when you do a print_r($candidates) its the right array contents, have you done the print_r immediately after the insert?
sorry about the double post, the code snippet gave me trouble with unescaped quotes.


on adding value in database last values is repeated as many times as rows are in table. - El Forum - 05-28-2009

[eluser]alberta[/eluser]
It returns full collection but inspite of this showing last value. That's really strange for me. No i didn't do it right after insert. After inserting i call another function and in that function i set the value of $data[candidates]. I dont know what's wrong with it Sad


on adding value in database last values is repeated as many times as rows are in table. - El Forum - 05-28-2009

[eluser]TWP Marketing[/eluser]
I have the same problem, not yet solved.

But I think the list array created from the query->result array is not resetting it's pointer.

The inserted record is the last one (perhaps the only one?) in the query, and the array pointer may be set for the final array entry. Look for a prior loop which iterated through the array and left the pointer at the last entry.

Possible fix?
Check your code and perhaps unset() the query array and get() it again before loading the view?

If you find a cure, please post it here.