duanhui9840 2013-05-15 06:40
浏览 67
已采纳

在php mysql应用程序中处理以下用户功能

Problem Statement: I am working on a follow user feature in my application and need some at a point. I have a list of offers(posted by user) to show on my page on which there is a "follow vendor" button.Now any logged in user can click on the follow button and follow the person who posted the offer.

Query: Now if a user is already following that vendor,next time the vendor posts any offer the follow button should be replaced by "following" or "already followed" as he has already followed him.
Now I can get the results for the records of who the logged in user is following in an array.This is the structure of the offer div in which the offers are displayed in a loop.

   <div class="box col2">
    <div class="innerBox"> <img src="http://codex.abc.com/upload_deals/files/221x208/<?php echo $resDeal['productImg'];?>">
      <p><?php echo $resDeal['productName']; ?></p>
      <p class="styleTxt">Added <?php echo $timer; ?> ago</p>
      <div class="clear"></div>
        <div class="over">
            <div class="twitter">
                <a href="https://twitter.com/share" class="twitter-share-button" data-lang="en" data-url="http://www.abc.com/salesDetails.php?dealId=<?php echo $resDeal['saleId']; ?>">Tweet</a>
                <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
            </div>
            <!-- Facebook share button Start -->
            <div class="facebook">
                <fb:share-button type="button_count" href="http://www.abc.com/biggisalesDetails.php?dealId=<?php echo $resDeal['saleId']; ?>"></fb:share-button>

            </div>
            <!-- Facebook share button End -->
            <div class="pintrest">
                <!-- Pinterest button Start -->
                    <a href="javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());"><img alt="Pin It!" style='border: none;' src="http://assets.pinterest.com/images/pidgets/pin_it_button.png"/></a>
                    <!-- Pinterest button End -->
            </div>
            <div class="clear"></div>
            <a href="javascript:void(0)" class="red follow" saleId="<?php echo $resDeal['saleId'];?>">Follow Vendor</a>
        </div>
    </div>
    <div class="bottom">
      <h3><?php echo $resDeal['discount']; ?> Off</h3>
      <a href="biggisalesDetails.php?dealId=<?php echo $resDeal['saleId']; ?>">MORE</a>
    </div>
</div>

There can be more than 100 offers at a time,so what would be the most efficient way to check if the user is following the vendor by whom the offer is posted?
I though of getting the values in an array and check at each offer if the vendor id exists in the array of vendors followed by user?Like this: select followedUserId from follow where userId =34.Store it in an array and check if the offer vendor matches it
Would that be a good approach or I can optimize it further in any way?
Suggestions appreciated

  • 写回答

1条回答 默认 最新

  • dongxie3681 2013-05-15 06:54
    关注

    The design you have suggested would work fine. There are other options, such as:

    1. Get the list of vendor IDs that will be displayed on the page
    2. Get the list of followed vendor IDs by the user
    3. Take the intersection of the two lists, giving a list of all the vendor IDs that will be displayed on the page and are followed

    But really these are basically fancy ways to do what you have described. For reasons of: (a) it works, (b) it's simple, (c) it is easy to understand, (d) it is easy to maintain; I would stick with the option you have described, ie.

    1. SELECT followuserid FROM follow WHERE userId=[insert user id here]
    2. Store in an array
    3. Every time you output an item, check if the vendor ID is in the array
    4. Using a PHP if-else block, conditionally display different content to the user.

    If you really want to take it to the max, you can sort the array and pick a nice algorithm to search it, or even build a new data structure such as a tree, but really this is taking a simple problem and making it very complex.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示