0

I've tried to rack my brain but got stucked.. but i believe this is possible, because there are gurus out there. There is this affiliation websites. If a user is the first to register on the app, he wont be under anyone but the next six users that registered would be under the first user,when it is more than six the seventh user wont be under anyone, any other six users that registered would be under the seventh users, and so on.. I created two tables, the users table and the table to keep track of users under users..

 if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $pre = $db->prepare("SELECT * FROM affi");
     $pre->execute();
    $fetch = $pre->fetchAll();
    // print_r($fetch);die;
    if(count($fetch) < 1) {
        //checking if this is the first user 
        $create_user = $db->prepare("INSERT INTO users (name, email, password )
        VALUES('$name', '$email', '$password')");
        $stmt = $create_user->execute();
        if($stmt)  {
            $refer = $db->prepare("INSERT INTO refer_customer (email)
            VALUES('$email')");
            $stmt = $refer->execute();
            header("Location: Index.php");
        }
    } else if(count($fetch) <= 7) {
        //trying to attach other six users that would register under the first one
        $get_user = $db->prepare("SELECT FIRST(id) from users ");
         $get_user->execute();
         $result = $create_user->fetch(PDO::FETCH_OBJ);
        if($result) {
        $create_user = $db->prepare("INSERT INTO users (name, email, password )
        VALUES('$name', '$email', '$password')");
        $stmt = $create_user->execute();
        if($stmt)  {
            $refer = $db->prepare("INSERT INTO refer_customer (refere_id ,email)
            VALUES('$result->id','$email')");
            $stmt = $refer->execute();
            header("Location: Index.php");
        }
    }  
    
    }
}

Got stucked dont even know how this gonna end because there will be unlimited users if you know what i mean.. Please any help is appreciated

1
  • really you should fix the sql injection issues before going any further (ill do later will never happen), see: How can I prevent SQL injection in PHP? also never store passwords as plaintext Commented Sep 22, 2021 at 23:25

1 Answer 1

1

This sounds like quite a simple problem.

  1. You could number your users when they register: user 1, 2, 3, etc. The best way to do this would be with an auto-increment index in your database. Let's call this column userId.

  2. Next is a little trick: You can use the modulo operator in PHP, like this: $position = $userId % 7; This will return a number between 0 and 6. $position == 0 is for the leader of the next 6 users.

And that's it. MySQL also has the modulo operator.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for replying, how do i implement this with the code ? the variable userId is coming from where? The auto increment id normally skips a number if a user was deleted. I see awesome reasoning here, but dont know how this helps, any further explanation is appreciated
I don't know what you explain about the implementation? The ID of an user dictates the position. That's all. There's really not that much to implement. Yes, an auto increment id does not automatically adapt to deleted users. You never mentioned anything about this in your question, so I didn't address it. Did you think about what to do with deleted users? Do they get replaced with other users?
@DipoDeen If the userId column is not an auto increment column then you can deal with deleted users. Let's rename the column to userNo. You can still use the solution I mentioned in my answer, and when a new user registers you have to look for the lowest available number.
Okay, i understand now.. It's a great idea how come i never think like this ? lol. Thanks so much
@DipoDeen It's simply a question of experience. If you reduce complex things to their simplest form on a daily basis for 30 years you will come up with things like this as well. It's already a big step that you actually understand this, many people wouldn't. So there is hope! 😀

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.