Topic: IDs being displayed instead of names |
|||
|---|---|---|---|
Posted: 2012-02-02 |
|||
|
User |
I am very new to coding in PyroCMS and CodeIgnitor so I apologize for my lack of knowledge. I am working on a module that allows me to display a project on each page and lists them on a listing page. At the moment I have 5 tables in my database: work (projects) clients agency roles project types The idea is that when creating a project a series of drop downs are pulling in the information from the other tables. I have these projects listed on the fronted as well as displaying info on the project pages which pulls it in from 'work' table in the database. The issue I am facing is that the columns in the project table that represent the other four tables have values stored as numbers, the ID for each row, rather than the name of the item in the table. Instead of the names appearing for each element on the page it is only displaying the numbers. I need to find a way to pull the names from the other tables. I have played around with the code and can display an array of the items in each table but I don't know how to display just one item specific to the project that is visible. I found this thread on the forum and tried to integrate it without much success. http://www.pyrocms.com/forums/topics/view/3422 Controller Code class Work extends Public_Controller
{
public function __construct()
{
parent::__construct();
// Load the required classes
$this->load->model('projects_m');
$this->load->model('clients_m');
$this->load->model('projtypes_m');
$this->load->model('roles_m');
$this->load->model('agency_m');
$this->lang->load('work');
$this->template->append_metadata(css('work.css', 'work'))
->append_metadata(js('work.js', 'work'));
}
public function _remap($method)
{
if($method == 'index')
{
$this->index();
}
else
{
$this->project($method);
}
}
/**
* index method
*
* @access public
* @return void
*/
public function index()
{
$projects = $this->projects_m->get_all();
$this->template->set('projects', $projects)
->build('index', $this->data);
}
public function project($slug = FALSE)
{
if(!$slug)
{
redirect('work');
}
else
{
$projects = $this->projects_m->get_by('slug', $slug);
$this->data->projects =& $projects;
$this->template->set('projects', $projects)
->build('project', $this->data);
}
}
}View Code <ul id="proj-list">
<?php foreach($projects as $project): ?>
<?php ?>
<li id="proj-list-top">
<a href="/work/strongbow-gold/"><img src="http://localhost/repos/paulflood/addons/default/themes/paul/img/inline/projects/sbg/sbg.jpg" alt="Strongbow Gold"/></a>
<div class="proj-details">
<img class="proj-logo" src="http://localhost/repos/paulflood/addons/default/themes/paul/img/inline/projects/sbg/logo.jpg" alt="Strongbow Logo"/>
<span class="proj-type"><?php echo $project->projtype; ?></span>
<h3><?php echo $project->name; ?></h3>
<p><?php echo $project->short; ?></p>
<a href="<?php echo rtrim(site_url(), '/').'/work/'; ?><?php echo $project->slug; ?>">View Project</a></div>
</li>
<?php endforeach; ?>
</ul>There is nothing special happening in the Modal file, everything is being pulled in. If anybody could throw some light on this I would be really grateful as I am really struggling at the moment to figure this out. Cheers |
||
| Quote | |||
Posted: 2012-02-03 |
# 1 | ||
|
User |
The model is actually where you should try to solve your problem. See the query on the bottom of jerels post, in the thread you linked? That's the important part: return $this->db
->select('*')
->from('jobs')
->join('categories, jobs.category_id = categories.id')
->where('categories.slug', $slug)
->get('jobs')
->result();a join is what you need: w3schools SQL join so in your case that would be something like return $this->db
->select('*')
->join('clients, projects.client_id = clients.id') //join the clients table to get the data
->join('projecttypes, projects.type_id = projecttypes.id') //join the projecttypes table to get the data
->where('projects.slug', $slug)
->get('projects')
->result();regards Marco |
||
| Quote | |||
Posted: 2012-02-03 |
# 2 | ||
|
User |
Thanks Marco. That helped a lot, but I am have one more little issue to tie this away. You can seen below I added to the code you supplied. Initially after I added your code it was displaying the names but was using the name tag for the project name. I added the additions in select line below to combat this. public function get_index_info()
{
return $this->db
->select('work.*, work_projtypes.name AS projtype_name')
->join('work_projtypes', 'work.projtype = work_projtypes.id') //join the projecttypes table to get the data
->get('work')
->result();
}So the listings page is showing all the data correctly now as its breaking that array data up using 'foreach' there is a list of items but the issue I am having now is that the data being pulled into the project page won't display cause I don't know how to break up the data that is the array, there is no need for foreach loop as its just showing the data from one table row. Here is the code for project page that is in the Modal. public function get_project_info()
{
return $this->db
->select('work.*, work_projtypes.name AS projtype_name, work_clients.name AS client_name, work_agency.name AS agency_name')
->join('work_clients', 'work.client = work_clients.id') //join the clients table to get the data
->join('work_agency', 'work.agency = work_agency.id') //join the agency table to get the data
->join('work_projtypes', 'work.projtype = work_projtypes.id') //join the projecttypes table to get the data
->where('work.slug', $slug)
->get('work')
->result();
}All the elements being pulled in on the project page are using this code, <?php echo $projects->item-name; ?>
Its projects instead of project and this gives the following error for all the id based elements.
A PHP Error has occurred
Severity: Notice
Message: Undefined property: stdClass::$client_name
Filename: views/project.php
Line Number: 24
Any extra help is hugely appreciated.
|
||
| Quote | |||
Posted: 2012-02-03 |
# 3 | ||
|
User |
ok, i think that's codeigniter stuff then have a look at CI Active Record if you only want a single row of data, you can use $this->db->where(something)->limit(1)->get(from_somewhere)->row()
that returns an object, and to access the data you just use it as any other object, e.g. $object->db_field_name
if you get "undefined property" on anything, you should always check if there actually is a property
and if not, why
but in this case it probably was because you used result() to get the result from the query, which returns an array of objects
you should really check out the codeigniter manual, it's actually really easy and convenient once you get to know the functions
|
||
| Quote | |||
Posted: 2012-02-03 |
# 4 | ||
|
User |
Cheers Marco, hugely appreciated your input. Have it all working now and will next time not start a project in such a backwards approach. For anybody that has the same problem in the future here is the code I used to get elements on the project page using the slug. Controller public function project($slug = FALSE)
{
if(!$slug)
{
redirect('work');
}
else
{
$data->projects = $this->projects_m->get_by('slug', $slug);
$data->other = $this->projects_m->get($slug);
$this->data->projects =& $projects;
$this->data->other =& $other;
$this->template->set('projects', $projects)
->set('other', $other)
->build('project', $data);
}
}Modal public function get($slug)
{
return $this->db
->select('*, work_projtypes.name AS projtype_name, work_clients.name AS client_name, work_agency.name AS agency_name')
->join('work_clients', 'work.client = work_clients.id') //join the clients table to get the data
->join('work_agency', 'work.agency = work_agency.id') //join the agency table to get the data
->join('work_projtypes', 'work.projtype = work_projtypes.id') //join the projecttypes table to get the data
->where('slug', $slug)
->get('work')
->row();
}View (You can se the id elements are using $other instead of $project for the items in the work(project) table) <div id="project">
<div id="proj-col-l">
<div id="proj-top-img">
<img src="{{settings:base_url}}uploads/default/work/main/<?php echo $projects->mimg; ?>" alt="<?php echo $projects->name; ?>"/></div>
<ul>
<li id="gal-1">
<a href="{{settings:base_url}}uploads/default/work/gallery/<?php echo $projects->gimg1; ?>" rel="gallery" title="Age Gate"><img src="{{settings:base_url}}uploads/default/work/thumbs/<?php echo $projects->gimg1; ?>" alt=""/></a></li>
<li>
<a href="{{settings:base_url}}uploads/default/work/gallery/<?php echo $projects->gimg2; ?>" rel="gallery" title="Home"><img src="{{settings:base_url}}uploads/default/work/thumbs/<?php echo $projects->gimg2; ?>" alt=""/></a></li>
<li>
<a href="{{settings:base_url}}uploads/default/work/gallery/<?php echo $projects->gimg3; ?>" rel="gallery" title="Strongbow Legend"><img src="{{settings:base_url}}uploads/default/work/thumbs/<?php echo $projects->gimg3; ?>" alt=""/></a></li>
<li>
<a href="{{settings:base_url}}uploads/default/work/gallery/<?php echo $projects->gimg4; ?>" rel="gallery" title="Brewed with Nature"><img src="{{settings:base_url}}uploads/default/work/thumbs/<?php echo $projects->gimg4; ?>" alt=""/></a></li>
</ul>
<h2 id="top">
Project Details</h2>
<p><?php echo $projects->details; ?></p>
</div>
<div id="proj-col-r">
<h3 id="pro_info">
Project Information</h3>
<p>
<span>Client:</span><a href="#" target="_blank"><?php echo $other->client_name; ?></a></p>
<p>
<span>Agency / Company:</span><a href="#" target="_blank"><?php echo $other->agency_name; ?></a></p>
<p>
<span>Project Type:</span><?php echo $other->projtype_name; ?></p>
<p>
<span>Project URL:</span><a href="<?php echo $projects->projurl; ?>" target="_blank"><?php echo $projects->projurl; ?></a></p>
<p>
<span>My Roles:</span></p>
<ul>
<li>
<?php echo $projects->role; ?></li>
</ul>
<h3>
Tags</h3>
<ul>
<?php foreach( $str as $st ): ?>
<li><?php echo $st; ?></li>
<?php endforeach; ?>
</ul>
</div>
</div>
<div class="clearer">
</div> |
||
| Quote | |||
