Welcome Guest, Not a member yet? Register   Sign In
unserialize query results not working
#1

[eluser]exodus7[/eluser]
I seem to be having some trouble using unserialize in my view. When I use the following code, im getting a blank list generated with no data :ahhh:

If I change unserialize to a simple echo statement, it displays the serialized data fine. Anyone have an idea?


Controller:
Code:
$this->surveymodel->q15($_POST);
$data['q15'] = $this->surveymodel->q15('survey_results');    
$this->load->view('survey/results', $data);
Model:
Code:
function q15()
{
$q15 = $this->db->query('SELECT q15 FROM survey_results');
return $q15->result();
}
View:
Code:
<ol>
&lt;?
foreach ($q15 as $row)
{
echo "<li>";
   unserialize ($row->q15);
echo "</li>";  
}
?&gt;  
              
</ol>

Thank you
#2

[eluser]eggshape[/eluser]
Hi - you don't need to use unserialize() because you haven't serialized the results.

Just echo the field:
Code:
echo '<li>' . $row->q15 . '</li>';

I think your confusion is that you think view() serializes your $data array? It does not.
#3

[eluser]exodus7[/eluser]
eggshape:

Thanks for your response.

What I did i was serialize an array (checkboxes) and stored the serialized data in my database using the "insert_entry" function in my model (SEE bottom). When viewing the results using the survey_results function in my controller is when im trying to unserialize the serialized data from the database. Here's a more detailed view:

Controller:

Code:
&lt;?php

class Survey extends Controller {

        var        $base;
        var        $css;

    function Survey()
    {
            parent::Controller();
            $this->base = $this->config->item('base_url');
            $this->css = $this->config->item('css');        
            $this->load->helper('form');
            $this->load->database('survey');
                    $this->load->model('surveymodel');

    }
    // index function loads the survey
    function index()
        {
            $data['css'] = $this->css;
            $data['base'] = $this->base;
            $this->load->view('survey/survey01');
        }
    // process_form function processes the form & stores the data in my db. user is then taken to a thank you page    
    function process_form ()
    {
            $data['css'] = $this->css;
            $data['base'] = $this->base;
                    $this->surveymodel->insert_entry($_POST);
            $this->load->view('survey/thankyou', $data);
    }
    // function survey_results runs a simple query to get the serialized data - then it gets passed to the view where I tried to unserialize the results.
    function survey_results()
        {
                        
        $this->surveymodel->q15($_POST);
        $data['q15'] = $this->surveymodel->q15('survey_results');    
            $this->load->view('survey/results', $data);
        }

Model:
Code:
&lt;?
class Surveymodel extends Model {

    function Surveymodel()
    {
        parent::Model();
    }
    
    function insert_entry()
    {
      $data = array(
               'gender' => $_POST['gender'] ,
               'age' => $_POST['age'] ,
               'zip' => $_POST['zip'] ,
               'email' => $_POST['email'],
            'q01' => $_POST['q01'],
            'q02' => $_POST['q02'],
            'q03' => $_POST['q03'],
            'q04' => $_POST['q04'],
            'q05' => $_POST['q05'],
            'q06' => $_POST['q06'],
            'q07' => $_POST['q07'],
            'q08' => $_POST['q08'],
            'q09' => $_POST['q09'],
            'q10' => $_POST['q10'],
            'q11' => $_POST['q11'],
            'q12' => $_POST['q12'],
            'q13' => $_POST['q13'],
            'q14' => $_POST['q14'],
            'q15' => serialize($_POST['q15_ARRAY']),
            'q16' => serialize($_POST['q16_ARRAY']),
                     'q17' => $_POST['q17'],
            'q18' => $_POST['q18'],
            'q19' => $_POST['q19'],
            'q20' => $_POST['q20'],
            'q21' => $_POST['q21']
            );

        $this->db->insert('survey_results', $data);

    }
    function q15()
    {
       $q15 = $this->db->query('SELECT q15 FROM survey_results');
       return $q15->result();
    }

}
View:
Code:
<ol>
&lt;?
foreach ($q15 as $row)
{
echo "<li>";
   unserialize ($row->q15);
echo "</li>";  
}            
?&gt;  
              
</ol>
#4

[eluser]eggshape[/eluser]
Hey exodus;

Sorry, I didn't properly read your code. I think if you read the manual concerning passing objects to the view function, it says the object gets converted to an array. So perhaps this will work:

Code:
&lt;?
foreach ($q15 as $row)
{
    $row = unserialize($row['q15']); // if this works, and you will get an array right?
    while ($q = array_unshift($row)) // so you need to loop thru the array
    {
       echo '<li>' . $q . '</li>';
    }
}            
?&gt;
#5

[eluser]exodus7[/eluser]
I gave that a shot and I'm getting a fatal error:

"Fatal error: Cannot use object of type stdClass as array" - line 331

line 331 is in the view file which shows:

Code:
unserialize($row['q15']);
#6

[eluser]eggshape[/eluser]
EDIT:

Hmm. That error seems to indicate that $row is still an object, which confuses me a bit in light of the manual. If $row is still an object and you want to keep your original q15() method:

Code:
&lt;?php foreach ($q15 as $row)
      {
         $row = unserialize($row->q15); // This should get you your original q15_ARRAY
         foreach ($row as $val) // use this if that array is numerical or use 'as $key => $val' if associative
         {
              echo '<li>' . $val . '</li>'; // you might need to change this
         }
      }
?&gt;

Oh man...am I totally confusing you or what?! =)
#7

[eluser]exodus7[/eluser]
I gave that a shot as well and got a new error:

Call to undefined function unshift()


So I figured you might of wanted to use array_unshift instead but when I use array_unshift

I'm getting this error:

Wrong parameter count for array_unshift()
#8

[eluser]eggshape[/eluser]
I tried using 'return $query->result()' and then '$row->id' in one of my apps and it works. I looked at the view() method code, and it does convert an object to an array. *BUT* (duh) $this->db->result() returns an ARRAY of objects...so the objects are still objects, and there is no conversion.

EDIT: CI is complicated, eh? Smile. you're right, sometimes i use unshift/pop by accident since i used to write in perl.
#9

[eluser]exodus7[/eluser]
considering I have less than 6 months of php experience and about 2 months of CI experience, its not easy :-)

In any case, I've been doing some searching online to see how others do it, and I found in some postings that others use implode to store data, which stores the values separated by a separator of your choice, and then use explode when you want to separate the values, is that right?

Thanks for the help eggshape
#10

[eluser]eggshape[/eluser]
yeah...that's pretty much what i do too, but i don't see a problem with your method. there's almost always more than one way to do it. perhaps in your original code when you wrote

[code]
echo '<li>';
unserialize($row->q15);
echo '<li>';
[code]

it didn't work because (1) you're dealing with an array after unserialization and (2) there's no 'echo' call, so you didn't even get the output 'array(#number of items)'.

sure, no problem. i hope i didn't confuse you that much. try to see if you can get your method to work.




Theme © iAndrew 2016 - Forum software by © MyBB