TeddyKiller Posted April 11, 2010 Share Posted April 11, 2010 This script, is one of the messiest I've probably done. Should be straight forward. $user is the currently logged in users array. (Holds all data for the user thats in the users table) When a user clicks accept, it changes the status of the request to 2 (meaning accepted) Then it inserts 2 rows into the friends database. With opposite values for user_id, and friend_id It then updates the users table where the logged in users, and takes away 1, from the friend_requests. After that, it inserts two rows into the activity table. One for each user, the user accepting the request, and the user who sent it. Is there any way to .. simplify it, it's very messy. if(isset($_POST['id'])){ $id = $_POST['id']; $query = mysql_query("UPDATE `friend_requests` SET `status`='2' WHERE `request_to`='$user->id' AND `request_by`='$id'"); $query = mysql_query("INSERT INTO `friends` (user_id, friend_id) VALUES ('$user->id', '$id')"); $query = mysql_query("INSERT INTO `friends` (user_id, friend_id) VALUES ('$id', '$user->id')"); $amount = $user->friend_requests - 1; $query = mysql_query("UPDATE `users` SET `friend_requests`='$amount' WHERE `id`='$user->id'"); $date = time(); $qry = mysql_query("SELECT * FROM `users` WHERE id = '$id'"); $row = mysql_fetch_row($qry); $activity = 'Just became friends with '.ucwords($row['username']). $query = mysql_query("INSERT INTO `activity` (user_id, activity, date) VALUES ('$user->id', '$activity', '$date')"); $activity = 'Just became friends with '.ucwords($user->username); $query = mysql_query("INSERT INTO `activity` (user_id, activity, date) VALUES ('$id', '$activity', '$date')"); echo 'You have successfully added '.ucwords($_POST['username']).' to your friends list!<br /><br />'; } Link to comment https://forums.phpfreaks.com/topic/198242-any-help-with-tidying-up-this-script-please/ Share on other sites More sharing options...
TeddyKiller Posted April 11, 2010 Author Share Posted April 11, 2010 Actually I could remove $qry = mysql_query("SELECT * FROM `users` WHERE id = '$id'"); $row = mysql_fetch_row($qry); replacing $row['username'] with $_POST['username'] Any other things I could do? Link to comment https://forums.phpfreaks.com/topic/198242-any-help-with-tidying-up-this-script-please/#findComment-1040124 Share on other sites More sharing options...
the182guy Posted April 11, 2010 Share Posted April 11, 2010 $qry = mysql_query("SELECT * FROM `users` WHERE id = '$id'"); $row = mysql_fetch_row($qry); $activity = 'Just became friends with '.ucwords($row['username']). FYI that wouldn't have worked anyway because you can't access the result returned from mysql_fetch_row() by the field name as the key like $row['username'] you would have to use the numerical key index $row[0], $row[1] etc, or use mysql_fetch_assoc() to get the result with the field name as keys. I notice you have used the same query twice here $query = mysql_query("INSERT INTO `friends` (user_id, friend_id) VALUES ('$user->id', '$id')"); $query = mysql_query("INSERT INTO `friends` (user_id, friend_id) VALUES ('$id', '$user->id')"); You can simplify that... MySQL allows you to put multiple rows in an INSERT INTO query, like $query = mysql_query("INSERT INTO `friends` (user_id, friend_id) VALUES ('$user->id', '$id'), ('$id', '$user->id')"); It looks like you have got a lot of different processes going on in that piece of code. I would split it all up by creating a class called something like Friend_Factory or Friend_Manager, and then have seperate functions for the individual processes. Then the logic of your code would be become very simple and easy to read. There are many other advantages too such as reusable code. Link to comment https://forums.phpfreaks.com/topic/198242-any-help-with-tidying-up-this-script-please/#findComment-1040152 Share on other sites More sharing options...
TeddyKiller Posted April 12, 2010 Author Share Posted April 12, 2010 $row = mysql_fetch_row($qry); I removed that, but thanks. That was just a typo. Was suppose to be Array $query = mysql_query("INSERT INTO `friends` (user_id, friend_id) VALUES ('$user->id', '$id'), ('$id', '$user->id')"); Didn't realise this was possible. Cheers! Link to comment https://forums.phpfreaks.com/topic/198242-any-help-with-tidying-up-this-script-please/#findComment-1040233 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.