Jump to content

Any help with tidying up this script please?


TeddyKiller

Recommended Posts

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 />';
      }

          $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.

          $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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.