Page 1 of 1

User Profiles - Favourites.

Posted: Tue Oct 16, 2012 9:38 am
by rabatos
I wanted to have members to be able to favourite another members profile. Similar to 'following' I guess except to just have a 'favourites' list showing your favourite users.

So I had a go at this, and added a 'favourites' column to the users table.

In profile.php I added:
<?php

//Add to favourites function.
if (isset($_POST['fav'])){
	$id = $_GET['id'];
	$email = $_SESSION['email'];
	add_favourite($id, $email);
}

if ($_GET['added'] == "true"){
													?>
													
													<h2 style="font-size:15px;color:#999;text-align:center;"><?php echo $user_info['name']; ?> has been added to your <a href="favourites.php">favourites.</a></p></h2>
													
													<?php
													}
													?>
													
													<form action="" method="post"><h2> <?php echo $user_info['name']; ?> 	
													<input style="display:inline;float:right;" type="submit" value="Add to Favourites" name="fav"/></h2>
													</form>
So thats just a submit button on the profile saying "Add to favourites" which calls a function in the back end file.

Then I created a file "favourites.php"
<?php

//remove favourite from database.
if (isset($_POST['remove'])){
	$email 	= $_SESSION['email'];
	$id     = $_POST['profile'];
	
	remove_fav($id, $email);
}

$req = (explode(',', $user_info['favourites']));

foreach ($req as $fav){
								$fav = (array($fav));
								foreach ($fav as $list){
								
								if (empty($list)){echo'<div style="display:none;">';}
							?>
									<a href="profile.php?id=<?php echo $list; ?>">
							<?php 	
									$name = fetch_user_info($list);
									
									echo $name['company_name'] ?>
									
									</a>
									<div style="margin:-5px 0 0 0;float:right;">
										<form action="" method="post">
											<input type="hidden" name="profile" value="<?php echo $list; ?>" name="delete"/>
											<input type="submit" value="✖ Remove" name="remove" style="	cursor:pointer;
																												cursor:hand;
																												font-weight:bold;
																												color:#c00;
																												background:none;
																												border:none;"/>
										</form>
									</div>
This page displays the list of favourited members, and has a remove button which too calls a funtion in the back end file.

Then in the back end file:
<?php

//add to favourites.
function add_favourite($id, $email){
	$id     = $id;
	$email 	= $email;
	
	$value 	= "{$id},"; 

	$result = mysql_query("UPDATE `users` SET
											`favourites` = CONCAT(favourites,'{$value}')

											WHERE `email` = '{$email}'");
	if($result){header("Location: profile.php?id={$id}&added=true");}
}

//delete favourites.
function remove_fav($id, $email){
        $id     = $id;
        $email  = $email;

        $check  = mysql_query("SELECT * FROM `users` WHERE `email` = '{$email}'");

        $get    = mysql_fetch_assoc($check);

        $return = explode("{$id},", $get['favourites']);

        $value  = implode("", $return);

        $result = mysql_query("UPDATE `users` SET `favourites` = '{$value}' WHERE `email` = '{$email}'");

        header("Location: favourites.php");
}
So this just added the members id to the favourites column and uses "," to separate, then explode() is used when displaying the id's.

But when removing a favourite, first the total value eg. 1,32,6,14,22; is selected from the favourites column and then exploded to remove the id of what is to be removed then the new value is re-entered into the database. This just seems a little be unnecessary, but is all that I could work out.


Is there a more efficient and easier way to do this, the whole script I have done. And if this is decent, is there anything I can improve.
I'm just trying to learn new things.

Re: User Profiles - Favourites.

Posted: Tue Oct 16, 2012 11:54 am
by Temor
I would create a new table for favorites with two columns, one for each user.

Sender-id - Reciever-id

This would make it easier to remove a particular relation.

The queries would be a lot easier too, seeing as you just need a basic Insert query for every new relation, and one Delete query for every removal.