0

I'm trying to find the "users" current subscription on the database and display it's name in a view. Primary is the ID, so I'm using the User ID from session to find the appropriate row in the "subscriptions". This works as it should (I believe anyways). My problem is sending the selected data to the view. The only text that shows up in the view is "array".

What am I doing wrong?

Here's my code:

    // displays current subscription in view
    $userid = $this -> session -> userdata('user_id');

    $this -> db -> select('subscription');
    $this -> db -> where ('id', $userid);
    $query = $this -> db -> get('subscriptions');

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

Here's what I'm echoing in my view:

    <? echo $packagename; ?>

EDIT: I looked into the FOREACH function in the CI manual. I plugged this in and feel a bit closer. I don't want anything overly complex, code wise like some of you have posted, as I'm just learning. So now when the page is rendered, the following text is displayed: string(9) "mrpopular"

    // displays current subscription in view
    $userid = $this -> session -> userdata('user_id');

    $this -> db -> select('subscription');
    $this -> db -> where ('id', $userid);
    $query = $this -> db -> get('subscriptions');

    foreach ($query->result() as $row)
    {
        $data['packagename'] = $row->subscription;
    }   

3 Answers 3

1

That's because you are setting an array value to $data['package_name']. result_array() returns an array.

You will likely need to echo your value as$packagename['some_key'], where some_key is the field you are interested in.

Sign up to request clarification or add additional context in comments.

5 Comments

Okay, so something like this? <? echo $packagename['subscription']; ?>. If so, it doesn't work...getting error
What do you get when you var_dump $packagename?
array(1) { [0]=> array(1) { ["subscription"]=> string(9) "mrpopular" } }
OK, so you found your solution. It looks like originally you were getting nested arrays returned, so you would have needed to do $packagename[0]['subscription']. I think your revised approach looks better.
You helped buddy! Thanks! +1
1

What you are getting from the database is an array and you are setting it to $data['packagename']

for a quick check to see if you have the right data simply use:

<? print_r($data['packagename']); ?>

this should write all the data on the page

but the best way is to use loops:

<?
foreach ($data['packagename'] as &$value) 
{
    echo $value;
}
?>

2 Comments

add this in the view? Or controller/model? Code example please. Not overly familiar with the foreach function.
replace <? echo $packagename; ?> with the above
0

try this

foreach($yourarray as $key=>$val)
{
    echo "Key:" . $key. " Value: " . $val. "<br>";
}

you will get all keys and values from array

change $yourarray with your array name

Comments

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.