简体   繁体   English

在 CodeIgniter 框架中使用 Jquery 添加到收藏夹功能

[英]Add to favorites functionality using Jquery in CodeIgniter framework

I am currently working on a tour website, where the number of destinations is available for the user.我目前正在一个旅游网站上工作,其中的目的地数量可供用户使用。 A Login user can like any destination which will be added to his favorites destination section.登录用户可以喜欢将添加到他的收藏夹目的地部分的任何目的地。 I have done this task successfully.我已经成功地完成了这项任务。 But after liked the destination the like button appears multiple times or equal to the number of likes have the user done.但是在喜欢目的地之后,喜欢按钮会出现多次或等于用户完成的喜欢次数。 Maybe I am wrong in my logic here is my code.也许我的逻辑错了这是我的代码。 The first loop is fetching all the destination and the second loop fetching the favorites destination.第一个循环获取所有目的地,第二个循环获取收藏夹目的地。 Note:- Here I am showing only logic code not complete HTML or other PHP code注意:-这里我只显示不完整的逻辑代码 HTML 或其他 PHP 代码

<?php foreach($listing_data as $list){ ?>
<?php foreach($favorites as $fav){ if($fav['link_id']==$list['id']){?>
      <li class="pull-right"> <a  class="Theme" id="liked"><i class="fas fa-heart"></i> </a> </li>
      <?php } else {?>
      <li class="pull-right"> <a href=""  class="Theme" id="liked"><i class="far fa-heart"></i> </a> </li>
      <?php } } }?>



Just add a helper variable只需添加一个辅助变量
and take out the html in 2th foreach.并在第 2 个 foreach 中取出 html。
$like = false; $喜欢=假; in each Site在每个站点
Then in each site you loop $favorites, then walk in $favorites for check if TheSite is liked;然后在每个站点中循环 $favorites,然后进入 $favorites 以检查 TheSite 是否被喜欢;
after second foreach you should compare if $like is true for use class "fas" or false for use class "far"在第二次 foreach 之后,您应该比较 $like 对于使用 class "fas" 是否为真,对于使用 class "far" 是否为假

 <?php foreach($listing_data as $list){ $like = false; foreach($favorites as $fav){ if($fav['link_id']==$list['id']){ $like = true; } } if($like){?> <li class="pull-right"> <a class="Theme" id="liked"><i class="fas fa-heart"></i></a> </li> <?php } else {?> <li class="pull-right"> <a href="" class="Theme" id="liked"><i class="far fa-heart"></i> </a> </li> <?php } }?>

This is just a refactoring of the code to make it look a bit cleaner.这只是对代码的重构,使其看起来更简洁。 The links are not included as they are?链接不包括在内? and also use id's that will not be unique.并且还使用不会唯一的id。

So the code could become something like...所以代码可能会变成......

<?php foreach ($listing_data as $list): ?>
    <?php

    foreach ($favorites as $fav) {
        $like = ($fav['link_id'] == $list['id']);
    }
    ?>

    <li class="pull-right">
        <?php if ($like): ?>
            <span>More Sensible Link 1</span>
        <?php else: ?>
            <span>More Sensible Link 2</span>
        <?php endif; ?>
    </li>

<?php endforeach; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM