Reputation: 1414
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
Upvotes: 0
Views: 393
Reputation: 4075
The design you have suggested would work fine. There are other options, such as:
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.
SELECT followuserid FROM follow WHERE userId=[insert user id here]
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.
Upvotes: 1