Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Associate Array to Pass Database Information with PHP and CodeIgniter
    primarykey
    data
    text
    <p><strong>Issue/Question</strong>: 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 <code>&lt;ul&gt;</code>. This isn't ideal, and I would like to show the users' first and last names in the <code>&lt;ul&gt;</code> 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.</p> <p><strong>Funky link generated in view</strong>:</p> <pre><code>Array Array </code></pre> <p><strong>Funky URL that is linked to "Array Array"</strong>:</p> <pre><code>http://example.com/user/shared/view/Array </code></pre> <p><strong>Modified Controller</strong>:</p> <pre><code>&lt;?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-&gt;end_user = $this-&gt;ion_auth-&gt;user()-&gt;row(); $data['end_user'] = $this-&gt;end_user; $this-&gt;load-&gt;vars($data); $this-&gt;load-&gt;model('events_model', 'events'); } public function index() { $title['title'] = 'Shared'; $this-&gt;load-&gt;model('shared_model','shared'); $data['sharers'][] = array( 'shared_owner_id' =&gt; $this-&gt;shared-&gt;get($this-&gt;end_user-&gt;id), 'owner_first_name' =&gt; $this-&gt;shared-&gt;get($this-&gt;end_user-&gt;first_name), 'owner_last_name' =&gt; $this-&gt;shared-&gt;get($this-&gt;end_user-&gt;last_name), ); $this-&gt;load-&gt;view('public/head_view', $title); $this-&gt;load-&gt;view('user/header_view'); $this-&gt;load-&gt;view('user/shared_view', $data); $this-&gt;load-&gt;view('user/footer_view'); } </code></pre> <p><strong>Modified View</strong>:</p> <pre><code>&lt;div class="hide-on-phones"&gt; &lt;ul&gt; &lt;?php foreach($sharers as $key =&gt; $value): ?&gt; &lt;li&gt;&lt;a href="&lt;?php echo base_url('user/shared/view/'.$value['shared_owner_id']) ?&gt;"&gt;&lt;?php echo $value['owner_first_name']." ".$value['owner_last_name'] ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endforeach; ?&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p><strong>Model</strong>:</p> <pre><code>class Shared_model extends crud_model { public function __construct() { parent::__construct(); $this-&gt;pk = 'id'; $this-&gt;table_name = 'shared'; } public function get($shared_to_user_id) { $this-&gt;db-&gt;where('shared_to_id', $shared_to_user_id); $ids = parent::get_all(); $users = array(); foreach ($ids as $id) { $users[] = $id-&gt;owner_id; } return $users; } } </code></pre> <p>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.</p> <p><strong>Original Controller</strong>:</p> <pre><code>public function index() { $title['title'] = 'Shared'; $this-&gt;load-&gt;model('shared_model','shared'); $data['sharers'] = $this-&gt;shared-&gt;get($this-&gt;end_user-&gt;id); $this-&gt;load-&gt;view('user/head_view', $title); $this-&gt;load-&gt;view('user/header_view'); $this-&gt;load-&gt;view('user/navigation_view'); $this-&gt;load-&gt;view('user/shared_view', $data); $this-&gt;load-&gt;view('user/footer_view'); } </code></pre> <p><strong>Original View</strong>:</p> <pre><code>&lt;?php foreach($sharers as $s): ?&gt; &lt;li&gt;&lt;a href="&lt;?php echo base_url('user/shared/mobile/'.$s) ?&gt;"&gt;&lt;?php echo $s ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endforeach; ?&gt; </code></pre> <p><strong>Disclaimer</strong>: I'm new to web development, and I suck at associative arrays (apparently).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload