Solar Posted October 5, 2010 Share Posted October 5, 2010 Since I am still new to php I am probably doing this the hardway. I'm coding a LIKE SYSTEM (Similar To Facebooks) I have a table that looks like this; Table Named; stats; idnamedescription 1Stevenupdated their status. 2Stevenuploaded a picture to their profile. Then I have a table like this; Table Named; `like`; idusernamelike 2Steven1 (Column "like" is a variable between 0 (Not Liked) and 1 (Liked) My SQL looks like this; $sql = "SELECT * FROM stats LEFT JOIN `like` ON `like`.id = stats.id ORDER BY stats.id DESC,stats.id LIMIT $offset, $rowsperpage"; Everything you see here is perfect. I will try to continue to explain the best I can. As you see above, the STATS TABLE has ID numbers auto-increcement while when someone hits the like button, that STATS ID number enters in the LIKE TABLE also inserting the sessions USERNAME and 1 Value for LIKE COLUMN. $sql="INSERT INTO `like` (id, username, `like`) VALUES ('$_POST[id]','$_POST[username]','$_POST[like]')"; Everything is once again perfect of what I have. Displaying it is my problem. echo $list[''] . "<a href=\"profile.php?user=" . $list['name'] . "\"> <b>" . $list['name'] . "</b></a> " . $list['description'] . "<br /><form action=\"profile_like_insert.php\" method=\"post\"> <input name=\"id\" type=\"hidden\" size=\"50\" value=\"" . $list['id'] . "\"/> <input name=\"username\" type=\"hidden\" size=\"50\" value=\""; ?> <?php echo $_SESSION['username']; ?> <?php echo $list[''] . "\"> <input name=\"like\" type=\"hidden\" size=\"50\" value=\"1\"/> <input type=\"submit\" value=\"Like\" /> </form>; The above is an echo list that echos perfectly. My only problem is where do I insert this into?: <a href=\"profile.php?user=" . $list['username'] . "\">" . $list['username'] . "</a> likes this.<br> When I enter this code in the ECHO LIST it duplicates posts and also has the text "Likes This" under each status update (Updated Profile). This is what I would like to have; Only show the word LIKES THIS if an USERNAME likes it. For example; Steven updated his profile. Steven uploaded a profile picture. John Doe likes this. What i've been getting is this; Steven updated his profile. likes this. Steven uploaded a profile picture. John Doe likes this. Steven uploaded a profile picture Bob likes this. (As you see it duplicates) I will keep trying very hard to figure this out, I would love someone to get me into the right path Thanks so much in advanced and your time! Link to comment https://forums.phpfreaks.com/topic/215198-is-this-possible/ Share on other sites More sharing options...
ignace Posted October 5, 2010 Share Posted October 5, 2010 Your table should look somewhat similar to: user (id, user_name, password)status (id, user_id, status, posted_at)like (status_id, user_id, liked) In mySQL this would be: CREATE TABLE user ( id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, user_name VARCHAR(32) NOT NULL CHARACTER SET utf8 COLLATE utf8_bin, password CHAR(40) NOT NULL CHARACTER SET ascii, INDEX idx_user_logon (user_name, password), UNIQUE idx_user_name (user_name)) ENGINE = MyISAM;CREATE TABLE status ( id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, user_id INT UNSIGNED NOT NULL, status VARCHAR(160) NOT NULL CHARACTER SET utf8 COLLATE utf8_bin, posted_at DATETIME NOT NULL, INDEX idx_status_user_id (user_id)) ENGINE = MyISAM;CREATE TABLE like ( status_id INT UNSIGNED NOT NULL, user_id INT UNSIGNED NOT NULL, liked TINYINT(1) NOT NULL DEFAULT TRUE, INDEX idx_like_status_id (status_id), PRIMARY KEY (status_id, user_id)) ENGINE = MyISAM; This design should cover most of your bases (user_name should only be registered once, user_name|password index for login, ..). Link to comment https://forums.phpfreaks.com/topic/215198-is-this-possible/#findComment-1119342 Share on other sites More sharing options...
Solar Posted October 5, 2010 Author Share Posted October 5, 2010 Thanks for your reply! As stated above (Which I have tables really similar); I am having troubles displaying this in PHP. As there is a news feed; there is a echo list (Posted above) here is an example of my output; Steven updated his profile. likes this. <- This is showing likes this. But nobody liked this post, how can I make that into an else statement using while and echo list? Steven uploaded a profile picture. John Doe likes this. Steven uploaded a profile picture. Bob likes this. <- This was allready liked above by John Doe. How can I put these two together in a while echo list? Thanks, hopefully this is clear enough to understand Link to comment https://forums.phpfreaks.com/topic/215198-is-this-possible/#findComment-1119452 Share on other sites More sharing options...
ignace Posted October 6, 2010 Share Posted October 6, 2010 As stated above (Which I have tables really similar); No, they are not. Your design isn't normalized or optimized, mine is. Link to comment https://forums.phpfreaks.com/topic/215198-is-this-possible/#findComment-1119550 Share on other sites More sharing options...
sasa Posted October 7, 2010 Share Posted October 7, 2010 pseudo code some code$description='':while loop for outputif($description != $list['description']){$description = $list['description'];//echo description}if($list['username']){echo "<a href=\"profile.php?user=" . $list['username'] . "\">" . $list['username'] . "</a> likes this.<br>";}//end while loop Link to comment https://forums.phpfreaks.com/topic/215198-is-this-possible/#findComment-1119640 Share on other sites More sharing options...
Solar Posted October 8, 2010 Author Share Posted October 8, 2010 pseudo code some code $description='': while loop for output if($description != $list['description']){ $description = $list['description']; //echo description } if($list['username']){ echo "<a href=\"profile.php?user=" . $list['username'] . "\">" . $list['username'] . "</a> likes this.<br>"; } //end while loop 100% Works! Thanks very much! Question (Doesn't Have to Be Answered): I have; Steven changed his profile picture. Steven likes this. Steven changed his profile picture. Bob likes this. Is there a way to combine the two so that it outputs: Steven changed his profile picture. Steven, Bob likes this. Thanks ignace for your help as well. If I want to clean up my databases/tables, I will follow your tables. They make more sense that way. Link to comment https://forums.phpfreaks.com/topic/215198-is-this-possible/#findComment-1120068 Share on other sites More sharing options...
sasa Posted October 8, 2010 Share Posted October 8, 2010 some code $description='': $tmp = array(); while loop for output if($description != $list['description']){ if(count($tmp)>0){ echo implode(', ', $tmp), " likes this.<br>"; $tmp = array(); } $description = $list['description']; //echo description } if($list['username']){ $tmp[] = "<a href=\"profile.php?user=" . $list['username'] . "\">" . $list['username'] . "</a>"; } //end while loop Link to comment https://forums.phpfreaks.com/topic/215198-is-this-possible/#findComment-1120108 Share on other sites More sharing options...
sasa Posted October 8, 2010 Share Posted October 8, 2010 i forget to add this if(count($tmp)>0){ echo implode(', ', $tmp), " likes this.<br>"; $tmp = array(); } afterr while loop Link to comment https://forums.phpfreaks.com/topic/215198-is-this-possible/#findComment-1120111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.