Jump to content

Recommended Posts

HI guys,

Created a matchmaking form with 2 users,user 1 fills it out saves the information then user 2 fills it out and the result is calculated.

Problem is that user 1 fills it out and it won't recognize user 2 so I can't calculate anything.

I have tried many times to fix this but it dosn't work,

Please help me by telling me what's wrong with my code.

<?php
session_start();
$difference=0;
$match=0;
$nmatch=1;
$result=1;


if(isset($_POST['check']))
{

    if (!isset($_SESSION['userOneName']))
    {
        $_SESSION['userOneName'] = $_POST["username"];
        $_SESSION['userOneBirthYear'] = $_POST["birthyear"];
        $_SESSION['userOneGoal'] = $_POST["goal"];
        $_SESSION['userOneFavorite'] = $_POST["favorite"];
        $_SESSION['userOneCat'] = $_POST["cat"];
        $_SESSION['userOneDog'] = $_POST["dog"];   
        $_SESSION['userOneHamster'] = $_POST["hamster"];
        $_SESSION['userOneGoldfish'] = $_POST["goldfish"];
        $_SESSION['userOneHuman'] = $_POST["human"];
        // create ALL session variables for user1 according to what's in the input vars: $_SESSION["userOneSomething"]=$_POST["something"];
    }
    else
    {
      $_SESSION['userTwoName'] = $_POST["username"];
      $_SESSION['userTwoBirthYear'] = $_POST["birthyear"];
      $_SESSION['userTwoGoal'] = $_POST["goal"];
      $_SESSION['userTwoFavorite'] = $_POST["favorite"];
      $_SESSION['userTwoCat'] = $_POST["cat"];
      $_SESSION['userTwoDog'] = $_POST["dog"];   
      $_SESSION['userTwoHamster'] = $_POST["hamster"];
      $_SESSION['userTwoGoldfish'] = $_POST["goldfish"];
      $_SESSION['userTwoHuman'] = $_POST["human"];
    }
        
        // Now we're in user 2. Create ALL session vars for user 2- AND Calculate Match 
     if(isset($_SESSION["goal"]))
        if ($_SESSION['goal'] !== 'romance')
        {
            session_unset (); 
            echo "sorry,no match"; 
        }
             #calculate first result
        {
            $difference +=abs('$userOneBirthYear'-'$userTwoBirthYear');
        }
      if ($_SESSION['userOneFavorite'] == 'userTwoFavorite')    
        {  
            $result +=$nmatch +10;
        }
    
    {
    $_SESSION["check"]= $_POST["check"];
    $_SESSION["username"]= $_POST["username"]; 
    $_SESSION["birthyear"]= $_POST["birthyear"];
    $_SESSION["goal"]= $_POST["goal"];
    $_SESSION["favorite"]= $_POST["favorite"];
    }
   
    if(isset($_POST['cat']))
    {
          $_SESSION["cat"]= $_POST["cat"];
    }
    if(isset($_POST['dog']))
    {
        $_SESSION["dog"]= $_POST["dog"];
    }
    if(isset($_POST['hamster']))
    {
        $_SESSION["hamster"]= $_POST["hamster"];
    }
    if(isset($_POST['goldfish']))
    { 
        $_SESSION["goldfish"]= $_POST["goldfish"];
    }
    if(isset($_POST['human']))
    {
        $_SESSION["human"]= $_POST["human"];
    }
                
    
    if(isset($_SESSION["dog"]))
        if ($_SESSION['userOneDog'] == 'userTwoDog')    
        {
            $nmatch +=8+$match;
        }
        else if(isset($_SESSION["dog"]))
            if ($_SESSION['userOneDog'] !== 'userTwoDog')    
            {
                $nmatch +=8+$match;
            } 
   
    if(isset($_SESSION["cat"]))
        if ($_SESSION['userOneCat'] == 'userTwoCat')    
        {
            $nmatch +=8+$match;
        }
    else if(isset($_SESSION["cat"]))
        if ($_SESSION['userOneCat'] == 'userTwoCat')    
        {
            $nmatch +=8+$match;
        }
    if(isset($_SESSION["hamster"]))
        if ($_SESSION['userOneHamster'] == 'userTwoHamster')    
        {
            $nmatch +=8+$match;
        }
    else if(isset($_SESSION["hamster"]))
        if ($_SESSION['userOneHamster'] == 'userTwoHamster')    
        {
            $nmatch +=8+$match;
        }
    if(isset($_SESSION["goldfish"]))
        if ($_SESSION['userOneGoldfish']=='userTwoGoldfish')
        {    
            $nmatch +=8+$match;
        }
    else if(isset($_SESSION["goldfish"]))
        if ($_SESSION['userOneGoldfish'] !== 'userTwoGoldfish')    
        {
            $nmatch +=8+$match;
        }
    if(isset($_SESSION["human"]))
        if ($_SESSION['userOneHuman'] == 'userTwoHuman')    
        {
            $nmatch +=8+$match;
        }
        else if(isset($_SESSION["human"]))
            if ($_SESSION['userOneHuman'] !== 'userTwoHuman')    
            {
                $nmatch +=8+$match;
            }
    
    #final result
      
    if(isset($_SESSION['userTwoFavorite']))
        echo  "system calculated the result";
        echo " your compatabilaty grade " .$result;
    if(isset($_SESSION['check']))
    if ($result <= 50) 
    {
        session_unset ();echo " keep away";
    }
    
    if ($result <= 50-70)
    {
        session_unset ();
        echo "not great";
    }
    else if ($result >= 75) 
    {
        session_unset ();
        echo "  you are a match made in heaven ";
    } 
   //need to think where this goes:into what condition
    session_unset ();
    echo " ?another round";
}
    
 ?>

Here is the form in html code:

<!doctype html>


<html>
<head>
<title>matchmaker</title>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0">
        <style> body{text-align: center;
            border-style: solid;
            padding:50px;}
        </style>
</head>

   <body>
       <h1>MatchMaker-2017-Find Your Soulmate</h1>
       <h2>user 1</h2> 
        <form method="post">
            <br>
            <hr>
             <label>?Name?</label>
             <input type="text" name="username">
            <br>
            <hr>
            <label>Birthyear: </label>
            <input type="text" name="birthyear">
            <br>
            <br>
            <hr>
            <label>looking for soul mate</label><br>   
      <input type="radio" name="goal" value="romance">romantic reasons
      <input type="radio" name="goal" value="friendship">friendship reasons
      <input type="radio" name="goal" value="luck">potluck<br> 
            <hr>
            <label>Animals I love:</label> <br>
             <input type="checkbox" name="cat" value="cat">cats<br>
             <input type="checkbox" name="dog" value="dog">dogs<br> 
             <input type="checkbox" name="hamster" value="hamster">hamsters <br> 
             <input type="checkbox" name="goldfish" value="goldfish">goldfish <br> 
             <input type="checkbox" name="human" value="human">humans <br>
            <hr>
            <label>favourite thing:</label>
             <input type="text" name="favorite">
            <br>
            <br>
            <button type="sumbit" name="check" value="check">check compatibility</button>
        </form>
</body>
</html>


Thank You

 

 

Link to comment
https://forums.phpfreaks.com/topic/305351-have-2-users-it-only-recognises-user-1/
Share on other sites

Look at your code that processes the form. When the first user submits the form, the code sets up the userOne* values. Then what? What will the code do next? I don't mean what it should do, I mean what does it actually do? Try to step through the code in your head as if you were executing it to see what happens.

Try something like this to organise your users:

session_start();
$_SESSION['users'] = [];

function addUser($data) {
    $_SESSION['users'][] = [
        'name' => $data['username'],
        'birthYear' => $data['birthyear']
        // ...
    ];
}

if (isset($_POST['check'])) {
    addUser($_POST);
    
    $numberOfUsers = count($_SESSION['users'])
    if ($numberOfUsers == 1) {
        // do user 1 stuff
        // $user1 = $_SESSION['users'][0];
    } elseif($numberOfUsers == 2) {
        // do user 2 stuff
        // $user1 = $_SESSION['users'][0];
        // $user2 = $_SESSION['users'][1];
    }

}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.