user7047368 user7047368 - 13 days ago 5
HTML Question

Displaying images based on the selected ids using codeigniter

enter image description hereHow to display images based on selecting ids.While adding portfolio images i am inserting data into two tables as portfolio table and portfolio_tags table.

I am having three tables portfolio,tags and portfolio_tags.

portfolio
=============
portfolio_id image_path
1 image.png
2 imag.png
3 images.png
4 img.png
5 imagess.png

Tags table:
==========
tag_id tag_name
1 All
2 CMS
3 DESIGN

portfolio_tag
=============
portfolio_id tag_id portfolio_tag_id
1 1 1
1 2 2
2 3 3
3 1 4


I will be fetching all the tags data as well as the portfolio data.While opening the page it will display all the data related to all the tags.But when we select particular only the information related to that tag to be displayed.
Ex:If i select CMS it should display only that information relation to CMS and if i select DESIGN only the information related to that tag should be displayed.

Can any one suggest me how to do that.

Here is my code.

Controller:

public function index()
{
$this->load->model('portfolio_model');
$data["records2"] = $this->portfolio_model->get_portfolio();
$data["records3"] = $this->portfolio_model->get_tags();
$data['mainpage'] = "portfolio";
$this->load->view('templates/template',$data);
}


Model:

function get_portfolio($limit, $start)
{
$this->db->limit($limit, $start);
$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;
}
}
function get_tags()
{
$this->db->Select('tags.*');
$this->db->From('tags');
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}


View:

<?php $this->load->view('tagss');?>
<?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;?>


Tags

<?php if(isset($records3) && is_array($records3)):?>
<?php foreach ($records3 as $r):?>
<div class="materials">
<div class="class453">
<a href="#" class="read_more12"><?php echo $r->tag_name;?></a>
</div>
</div>
<?php endforeach ;endif;?>
<script type="text/javascript">
$('.materials a').not('.materials a:first').addClass('opacty');
$('.materials a').click(function(e){
$('.materials a').not(this).addClass('opacty');
$(this).removeClass('opacty');
});
</script>

Answer

For showing filtered images on clicking different tagNames, we can use ajax. So at first we need to create a new function in the Controller class which would display the fetched images url for the tag_id as the json object.

Add the function below to you controller.

public function tag_data($id){
    $data = array();
    $tagged_result = $this->portfolio_model->get_tag_images($id); // call to model function
    $tagged_images = array();

    foreach($tagged_result as $tag){
        $tagged_images[] = $tag->image_path;
    }
    echo json_encode($tagged_images);
}

In the code above I've called the function get_tag_images($id) which fetches all the images url from the database which are related to the tag_id.

Append the code below to the model class

public function get_tag_images($id){
    $query = $this->db->select('image_path')->from('portfolio_tag')->join('portfolio',"portfolio_tag.portfolio_id = portfolio.portfolio_id")->where("tag_id", $id)->group_by('portfolio.portfolio_id')->get();
    if($query->num_rows() > 0)     
        return $query->result();
    else
        return false;
}

Now we have to make some changes in the tags view.

View:

<?php 
$cnt = 0;
if(isset($records2) && is_array($records2)):?>
    <div id="portfolio">
        <?php foreach ($records2 as $r):?>  
            <div class="portfolioimages">                   
              <a href="<?php echo base_url('portfolio'); ?>" target="_blank"><img src="<?php echo base_url('assets/images/'). $r->image_path; ?>" /></a>
            </div>
            <?php 

            if(($cnt%3) == 0) { echo "<br>"; }          
            $cnt++; 
        endforeach; ?> 
    </div>
<?php endif;?>

Edit Tags view:

<?php if(isset($records3) && is_array($records3)):?>
    <?php foreach ($records3 as $r):?> 
        <div class="materials">         
            <div class="class453">
                <a href="javascript:void(0)"  class="read_more12">
                    <span style="display:none"><?php echo $r->tag_id; ?></span> // this contains the tag_id
                    <?php echo $r->tag_name;?>
                </a> 
            </div>
        </div>
<?php endforeach ;endif;?>  

Ajax -

<script type="text/javascript">

    $('.materials div a').click(function(e){
        e.preventDefault();
        var tagId = $(this).find('span').html();
        var url   = '<?php echo base_url('index.php/Portfolio/tag_data/'); ?>'+ tagId;
        var $this = $(this);
        $.ajax({
          type: 'POST',
          url:  url,
          data: {'tagid': tagId},
          success: function(data){
            var taggedImgs = $.parseJSON(data);
            var inc = 0;
            var unTag = [];
            var portfolioImages =  $('.portfolioimages a img').map(function(){
                    var url = $(this).attr('src').split('/');
                    return url[url.length-1];
                });

            $('.portfolioimages a img').each(function(e){
                    imgUrl      = $(this).attr('src').split('/');
                    var imgPath = imgUrl[imgUrl.length-1];
                    // compare the tagged image with portfolio images url
                    if($.inArray(imgPath, taggedImgs) == -1){
                        // image doesn't matched
                        $(this).remove();
                    } 

                });
            jQuery.each(taggedImgs, function(idx, tagImg){
                var flag = false;
                if($.inArray(tagImg, portfolioImages) == -1){
                    // image doesn't exist
                    $('#portfolio').append("<div class='portfolioimages'><a href='<?php echo base_url('index.php/portfolio'); ?>' target='_blank'><img src='<?php echo base_url('admin/images/portfolio/thumbs/'); ?>"+tagImg+"'></a></div>");
                }

            });

          },
          error: function(err){
            alert("Some error occured! "+ err);
          }
        })
    })
</script>