1
\$\begingroup\$

I have an array setup which consists of 25 rings, names, stats, prices. Right now in its current form, I have a form setup with 25 radio buttons with a value of 1-25 on them, and I'm using If statements in my function to buy a ring. It works but I feel it's very inefficient because I have this code running 25 times based on $_POST['ring'] which sends the value 1-25.

    if ($rings[1][7] > $_SESSION['cash']) {
        echo "You do not have enough cash for this ring!<BR><tr>
        <td class=\"fa-border\" align=\"center\"><a href=\"ringshop.php\">GO Back!</td> 
    </tr>";
        return;
    }
    if ($_POST['ring'] == 1) {
        $ringcost = $_SESSION['cash'] - $rings[1][7];
        $stmt = $pdo->prepare('UPDATE cu_cash SET cash=:cash WHERE id=:id');
        $stmt->bindParam(":cash", $ringcost);
        $stmt->bindParam(":id", $_SESSION['id']);
        $stmt->execute();

        $stmt = $pdo->prepare('UPDATE cu_users SET ring_id=:ring_id WHERE id=:id');
        $stmt->bindParam(":ring_id", $_POST['ring']);
        $stmt->bindParam(":id", $_SESSION['id']);
        $stmt->execute();
        echo "You have Successfully Purchased " . $rings[1][0] . "<BR><tr>
        <td class=\"fa-border\" align=\"center\"><a href=\"ringshop.php\">GO Back!</td> 
    </tr>";
    }

Now I am wondering if there's a loop I could use to take the value from $_POST['ring'] and pull the price assign it to a variable so I can do my cash - price then update the DB with ring id & update my cash? been thinking about this for a while & I am having a hard time wrapping my head around it. Loops have always confused me, but I know they are useful!

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Well turns out I did not need a loop, I modified my code as follows and it does exactly as I intended.

if ($rings[$_POST['ring']][7] > $_SESSION['cash']) {
        echo "You do not have enough cash for this ring!<BR><tr>
        <td class=\"fa-border\" align=\"center\"><a href=\"ringshop.php\">GO Back!</td> 
    </tr>";
        return;
    } else {
        $ringcost = $_SESSION['cash'] - $rings[$_POST['ring']][7];
        $stmt = $pdo->prepare('UPDATE cu_cash SET cash=:cash WHERE id=:id');
        $stmt->bindParam(":cash", $ringcost);
        $stmt->bindParam(":id", $_SESSION['id']);
        $stmt->execute();

        $stmt = $pdo->prepare('UPDATE cu_users SET ring_id=:ring_id WHERE id=:id');
        $stmt->bindParam(":ring_id", $_POST['ring']);
        $stmt->bindParam(":id", $_SESSION['id']);
        $stmt->execute();
        echo "You have Successfully Purchased " . $rings[$_POST['ring']][0] . "<BR><tr>
        <td class=\"fa-border\" align=\"center\"><a href=\"ringshop.php\">GO Back!</td> 
    </tr>";
    }
    $_SESSION['ring_id'] = $_POST['ring'];
    $_SESSION['cash'] = $ringcost;
}

I also do not need 25 of these as I did before.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.