1

Hi CI developers I am new to CI framework so request you guys to help me in splitting array values in my view page. I am able to print the query values in my view page in this way, for which I wrote the following code:

echo "<pre>";
print_r($showdata); //I am getting o/p in this way
Array
(
   [results] => Array
        (
            [0] => Array
                (
                    [id] => 205
                    [uid] => mh47
                    [profile_for] => self
                    [gender] => female 
               )

        )

    [count] => 1
    [pages] => 1
)

Now I want to split these array values and count values and pages values so request you to help me. I tried it this way but I am not getting any output:

    if (isset($data['showdata'])){
      foreach ($showdata->result() as $key) {
    <?php echo ($key->gender); echo $key['gender']; ?>
    <?php
      }
    }
    ?>  

below is my model page

$data['showdata']   =   $this->searchresultss->login($per_page,$look,$age);

$this->load->view('searchresult',$data);

below is my model page

function login($per_page=3,$look,$age,$age_to,$age_from,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)
{

$query="SELECT * FROM users";

$data=array();
$query=$this->db->query($query);
$data['results']=$query->result_array();
$data['count']=$query->num_rows();
$data['pages']=ceil($data['count']/3);

return $data; 

when this works

echo "<pre>";
print_r($showdata);

Why am I not able to echo the array value. I am in a lot of confusion, so guys please help me. I am new to CI framework so I am unable to sort out the problem.

4
  • no error output...not getting any o/p for for each value Commented Jul 29, 2014 at 14:34
  • enable error in config.php and see output erros. debug $this->searchresultss->login and see result return from function Commented Jul 29, 2014 at 14:36
  • You are printing out $showdata with print_r, but are iterating through the array that is returned from $showdata->result(). Try dumping that first and seeing if there is anything in there. Commented Jul 29, 2014 at 14:37
  • Iam unable to get you jamil Commented Jul 29, 2014 at 14:40

1 Answer 1

2

$data['showdata']only exists in the controller, in the view you should access it like this:

if (isset($showdata)){ //...

Your foreach should probably look like this:

foreach($showdata['results'] as $k => $v){
    echo $v['gender'];
}
Sign up to request clarification or add additional context in comments.

7 Comments

@vicky You're welcome. If it helped you, please mark my answer as having solved your problem :-)
$showdata['count'] and $showdata['pages'], respectively.
Weird, because your print_rat the top suggests otherwise. both count and pages are toplevel elements of the $showdata array...
thanks man solved it forgot to write echo..can explain where was i wrong
$showdatais an array that already contains all your data. In your foreach loop, you were treating it as an object. I'd suggest you read up a bit on arrays and/versus objects... :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.