Reputation: 167
Hi I am having portfolio table where i am inserting all my portfolio list.While inserting the portfolio list i will inserting tags which are separated by commas.But while fetching data it should in this
Controller:
$data["records2"] = $this->portfolio_model->get_portfolio();
$data['mainpage'] = "portfolio";
$this->load->view('templates/template',$data);
Model:
function get_portfolio()
{
$this->db->Select('portfolio.*');
$this->db->From('portfolio');
$this->db->where(array('portfolio.status'=>1));
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<div class="materials">
<div class="class453">
<a href="#" class="read_more12">All</a>
</div>
<div class="class455">
<a href="#" class="read_more13">E-Commerce</a>
</div>
<div class="class459">
<a href="#" class="read_more14">Cms</a>
</div>
</div>
<?php
$cnt = 0;
if(isset($records2) && is_array($records2)):?>
<?php foreach ($records2 as $r):?>
<div class="portfolioimages">
<a href="<?php echo $r->website_url;?>" target="_blank"><img src="<?php echo base_url();?>admin/images/portfolio/thumbs/<?php echo $r->image_path;?>" /></a>
</div>
<?php if(($cnt%3) == 0) { echo "<br>"; }
$cnt++; endforeach; endif;?>
</div>
In my database it will be inserting the data in the format:
while fetching data it should get all the data but if we click on CMS only the data for that particular tag should be displayed. Ex: if we select cms then 1,3,4 id should be displayed because they are cms tags,If we select E-commerce then 1,2,4 id should be displayed.How can be these done.
Upvotes: 1
Views: 485
Reputation: 5507
Ok just to elaborate a bit more without turning this into a major tutorial, what is being alluded to here is the fact that if you have to store repeat strings separated by commas and you wish to perform a Search on those using sql, then you need to rethink your design.
What you need is a joining table And a Tags Table. So the Tags Table would look like...
tags table
==========
ids name
1 All
2 CMS
3 E-Commerce
Then your Joining/pivot portfolio_tag table would be
portfolio_tag
=============
portfolio_id tag_id
1 2
1 3
2 3
3 2
4 1
So then you could display all of your tags with their associated id's So if I selected CMS, then that results in the id of 2 from the tags table. Then referencing that from the pivot table I can get all the portfolios ids matching the tag_id = 2.
That is the gist of what you should be heading towards.
It wouldn't hurt to go and do some reading up on pivot tables and joins. I'm not trying to make some throw away comment in regard to this, but there's too much to explain to put in here.
Upvotes: 0
Reputation: 4829
It is a bad practice to store tags in a comma separated list. All Tags should always be stored in a different table which can be related to your main table via a pivot table.
In your case it seems like a portfolio can have many tags and a tag can belong to many portfolios so you should have a DB structure something like this :
portfolio
id
title
description
image
tags
id
tag
porfolio_tag (Pivot Table)
portfolio_id
tag_id
However, if you would like to proceed with your current design, you can simply select the records as it is and then use the php explode function
Upvotes: 1