1

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:

enter image description here

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.

8
  • Here's an idea. DON'T insert tags separated by commas. Commented Oct 24, 2016 at 10:58
  • Then how to insert the data into database Commented Oct 24, 2016 at 11:05
  • select * from table_name where FIND_IN_SET ('cms',tags)>0 Commented Oct 24, 2016 at 11:06
  • image_id, tag_id. Commented Oct 24, 2016 at 11:07
  • @JYoThI if there are more than 5 then how can we write like that Commented Oct 24, 2016 at 11:09

2 Answers 2

1

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

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

4 Comments

but how can we display the data selecting on a particular tag
That's a whole different question.
@TimBrownlaw i have posted the question for that purpose only how to fetch the data for particular tag and display that data
I understand that but you are looking at a world of grief doing it the way you have put forward here, and all we are doing is trying to point you in the right direction. But if you really insist...
0

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.

2 Comments

if we are fetching tags from different table that to be shown in header and fetching portfolios from different page is not working because all the code is in single php page so i can fetch data from that tables because there will not be any connection between portfolio and tags table
Yes there is, that is what the pivot table is for! It is the pivot table that defines the relationship between the portfolio and the tags. This is called a Many to Many relationship with emphasis on the term "relationship"! You use a join in your SQL. So when you create/edit a portfolio you will display the tags and select the ones that are related to the portfolio. The fun part is in managing this... Like I suggested, it'd be a good idea to read up on this by googling...

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.