imlouisrussell imlouisrussell -3 years ago 34
PHP Question

Using Associate Array to Pass Database Information with PHP and CodeIgniter

Issue/Question: I'm using CodeIgniter to build an event calendar, and I have included a sharing option. This option works at a base level, but only displays the users' ID (primary key) in an

<ul>
. This isn't ideal, and I would like to show the users' first and last names in the
<ul>
instead. I thought creating an associative array would work, but I'm receiving funky results. The first and last name echo out as "Array Array" when the page loads, and the URL id comes up as "Array" when you select the "Array Array" link. I'm wondering what is wrong in my logic.

Funky link generated in view:

Array Array


Funky URL that is linked to "Array Array":

http://example.com/user/shared/view/Array


Modified Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Shared extends Common_Auth_Controller {

private $end_user;

public function __construct()
{
parent::__construct();

$this->end_user = $this->ion_auth->user()->row();
$data['end_user'] = $this->end_user;
$this->load->vars($data);

$this->load->model('events_model', 'events');
}

public function index()
{
$title['title'] = 'Shared';

$this->load->model('shared_model','shared');

$data['sharers'][] = array(
'shared_owner_id' => $this->shared->get($this->end_user->id),
'owner_first_name' => $this->shared->get($this->end_user->first_name),
'owner_last_name' => $this->shared->get($this->end_user->last_name),
);

$this->load->view('public/head_view', $title);
$this->load->view('user/header_view');
$this->load->view('user/shared_view', $data);
$this->load->view('user/footer_view');
}


Modified View:

<div class="hide-on-phones">
<ul>
<?php foreach($sharers as $key => $value): ?>
<li><a href="<?php echo base_url('user/shared/view/'.$value['shared_owner_id']) ?>"><?php echo $value['owner_first_name']." ".$value['owner_last_name'] ?></a></li>
<?php endforeach; ?>
</ul>
</div>


Model:

class Shared_model extends crud_model {

public function __construct()
{
parent::__construct();

$this->pk = 'id';
$this->table_name = 'shared';
}

public function get($shared_to_user_id)
{
$this->db->where('shared_to_id', $shared_to_user_id);
$ids = parent::get_all();

$users = array();

foreach ($ids as $id)
{

$users[] = $id->owner_id;
}

return $users;
}
}


Thank you so much for your help, and let me know if there is any more information that may be required. Below are the original view and controllers that work, but are not preferable.

Original Controller:

public function index()
{

$title['title'] = 'Shared';

$this->load->model('shared_model','shared');

$data['sharers'] = $this->shared->get($this->end_user->id);

$this->load->view('user/head_view', $title);
$this->load->view('user/header_view');
$this->load->view('user/navigation_view');
$this->load->view('user/shared_view', $data);
$this->load->view('user/footer_view');
}


Original View:

<?php foreach($sharers as $s): ?>
<li><a href="<?php echo base_url('user/shared/mobile/'.$s) ?>"><?php echo $s ?></a></li>
<?php endforeach; ?>


Disclaimer: I'm new to web development, and I suck at associative arrays (apparently).

Answer Source

I've figured it out. I needed to create an associative array in my model; not my controller. Thanks to everyone for your help!

Model:

class Shares_model extends crud_model {

    public function __construct()
    {
        parent::__construct();

        $this->pk = 'id';
        $this->table_name = 'shares';
    }

    public function get($shared_to_user_id)
    {
        $this->db->where('shared_to_id', $shared_to_user_id);
        $ids = parent::get_all();

        $users = array();

        foreach ($ids as $id)
        {
            $users[$id->owner_id]['owner_id'] = $id->owner_id;
            $users[$id->owner_id]['owner_first_name'] = $id->owner_first_name;
            $users[$id->owner_id]['owner_last_name'] = $id->owner_last_name;
        }

        return $users;
    }   
}

View:

<ul>
    <?php foreach($sharers as $s): ?>
        <li><a href="<?php echo base_url('user/shared/view/'.$s['owner_id']) ?>"><?php echo $s['owner_first_name']." ".$s['owner_last_name'] ?></a></li>
    <?php endforeach; ?>
</ul>

Controller:

public function index()
    {
        $title['title'] = 'Shared';

        $this->load->model('shares_model','shares');

        $data['sharers'] = $this->shares->get($this->end_user->id);

        $this->load->view('public/head_view', $title);
        $this->load->view('user/header_view');
        $this->load->view('user/shared_view', $data);
        $this->load->view('user/footer_view');
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download