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

Dipo Deen
  • 11
  • 3
  • 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?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) also never store passwords as plaintext – Lawrence Cherone Sep 22 '21 at 23:25

1 Answers1

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.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • 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 – Dipo Deen Sep 22 '21 at 23:18
  • 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? – KIKO Software Sep 22 '21 at 23:24
  • @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](https://forums.mysql.com/read.php?20,12839,83242). – KIKO Software Sep 22 '21 at 23:29
  • Okay, i understand now.. It's a great idea how come i never think like this ? lol. Thanks so much – Dipo Deen Sep 23 '21 at 07:50
  • @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! – KIKO Software Sep 23 '21 at 08:08