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

[eluser]exodus7[/eluser]
YAY :cheese:

I ended up using the implode method. This way I can still use the data that is stored to run simple queries. This is what I ended up with in the end:

Controller:
Code:
<?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');

    }
    
    function index()
        {
            $data['css'] = $this->css;
            $data['base'] = $this->base;
            $this->load->view('survey/survey01');
        }
        
    function processed ()
    {
            $data['css'] = $this->css;
            $data['base'] = $this->base;
                $this->surveymodel->insert_entry($_POST);
            $this->load->view('survey/processed', $data);
    }
    
    function survey_results()
        {
        $this->surveymodel->q15($_POST);
        $data['q15'] = $this->surveymodel->q15('survey_results');    
                $this->load->view('survey/results', $data);
                 }

}
?>

Model:
Code:
<?
class Surveymodel extends Model {


    function Surveymodel()
    {
        // call the model constructor
        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' => implode("-", $_POST['q15_ARRAY']),
            'q16' => implode("-", $_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;?php foreach ($q15 as $row)
      {
      $res = $row->q15;
      $list = explode("-", $res);
       echo "<li>";
        foreach ($list as $item)
        {
        echo " $item -";
        }
        unset($item);
        unset($res);
        unset($list);
        echo "</li>";
      }
?&gt; </ol>
#12

[eluser]eggshape[/eluser]
hey exodus - great job!

have you tried replacing your implode/explode with serialize/unserialize like you had originally (keep everything else the same)?

also, for future reference, here are some things I would comment:

1. use $data['q1'] = $this->input->post('q1') etc instead of the naked $_POST['q1']. CI has a good Input class that helps to protect your database and data.
2. with a little work, you can get your model class to automatically get your keys (i.e. q1, q2, q3,...) and values from the $_POST superglobal without having to delineate each key and value.
3. and some minor things: you don't need to unset $list, $item, $res. Those variables are "local" to the loop, and at the end of each iteration (closing '}'), they just get cease (garbage-collected). every time you call a function it adds a little overhead to your script (but this is not so important in this case, but imagine your array was a million lines long...you would have 3 million unnecessary function calls.)
#13

[eluser]exodus7[/eluser]
eggshape,

Thanks for the great feedback.

Quote:have you tried replacing your implode/explode with serialize/unserialize like you had originally (keep everything else the same)?

I didn't try replacing it with serialize/unserialize yet, but I'll be sure to let you know once I do.

Quote:2. with a little work, you can get your model class to automatically get your keys (i.e. q1, q2, q3,...) and values from the $_POST superglobal without having to delineate each key and value.

I was wondering if there was an easier way, but with my limited programming knowledge, I did it the only way I knew how. Can you point me in the right direction like a tutorial or a book?

Quote:3. and some minor things: you don’t need to unset $list, $item, $res. Those variables are “local” to the loop, and at the end of each iteration (closing ‘}’), they just get cease (garbage-collected). every time you call a function it adds a little overhead to your script (but this is not so important in this case, but imagine your array was a million lines long...you would have 3 million unnecessary function calls.)


So these local variables get destroyed at the end of the function, how could I declare a variable that can be used anytime? Would that be set in the config file?




Theme © iAndrew 2016 - Forum software by © MyBB