0

my site is written in php and heres the code I have got

//good words
        $body = implode(" ", $body_array);
        $niceWords = array("nice", "amazing", "fantastic", "great");
        $body = preg_replace('/\b('.implode('|',$niceWords).')\b/','You have used the speical ',$body);

        //Start the Points
        $_SESSION['points'];
        $_SESSION['points']++;
        echo $_SESSION['points'];
  • What triggers this? Do they submit a form or something? Or something like when they exit out of the text field which would require some javascript? – C Miller Dec 22 '17 at 03:35
  • yes it is like a submit form but the thing is there are certain words that if you type them your number will get incremented so its like a points system –  Dec 22 '17 at 04:54

1 Answers1

0
    <?php session_start(); ?>
    <form method="POST">
        <input type="text" size="60" name="myText" id="myText" />
        <input type="submit" name="submit" value="submit" />
    </form>      

    <?php

    if(isset($_POST["submit"])) {            
        //echo "$_POST[myText] <br>";            

        $niceWords = array("nice", "amazing", "fantastic", "great");            
        $textArray = explode(" ", $_POST["myText"]); // create an array of all the words from the textbox

        if(! isset($_SESSION["points"])) {
            //Start the Points
            $_SESSION["points"] = 0;
        }

        foreach($textArray as $word) {
            //echo "$word <br/>";
            // check if the word is in the array of niceWords
            if(in_array($word, $niceWords)) {
                //echo "Yeah!! nice word... <br/>";
                // if it is in the array increment points counter
                $_SESSION["points"]++;
            }
        }
        echo $_SESSION['points'];

    }
    ?>
T.Shah
  • 2,768
  • 1
  • 14
  • 13
  • your code works its just when i get out of the page and then go back in it starts from 1 again if you could help me that would be great –  Dec 26 '17 at 06:02
  • You could use local storage instead of session, if you want to maintain the points. – T.Shah Dec 26 '17 at 07:54
  • Hi T.Shah is there a link that has some good teaching about local storage and also is local storage in php or javascript –  Dec 27 '17 at 05:19
  • These may be starting points - https://www.w3schools.com/html/html5_webstorage.asp and https://www.tutorialrepublic.com/html-tutorial/html5-web-storage.php. – T.Shah Dec 27 '17 at 07:34
  • What if I store the points a database? Will that work because im not that good in javascript –  Dec 29 '17 at 08:49
  • To interact with database, I think it would be best if you use server side language like PHP. But I found this - https://stackoverflow.com/questions/19281312/how-to-insert-data-to-a-mysql-database-from-an-html-table - it may help. – T.Shah Dec 29 '17 at 10:56
  • Is there anyway of doing it in php because im not good at javascript –  Dec 30 '17 at 22:13
  • I also tried uploading it to phpmyadmin which is a php database and It did'nt work –  Jan 03 '18 at 06:41
  • Upload the code that did not work and then we can help you figure out why that did not work. – T.Shah Jan 03 '18 at 07:46
  • $points_query = mysqli_query($con, 'INSERT INTO user_points VALUES($_SESSION["points"])'); –  Jan 03 '18 at 22:58
  • and also my table is called user_points and it only has one row and its called points I want to store the session in to points so I can access it more easily –  Jan 03 '18 at 22:59
  • Try this - $points_query = mysqli_query($con, 'INSERT INTO user_points VALUES(' . $_SESSION["points"] . ')'); And then learn PDO. – T.Shah Jan 04 '18 at 11:04
  • Hello T.shah Inserting the code into the database works but I also want to display it here was my code that did not work @ $points_select_query = mysqli_query($con , 'SELECT * FROM user_points'); while($row = mysqli_fetch_array($points_select_query)) { echo $row["points"]; } –  Jan 06 '18 at 22:45
  • Hi... I can't find anything wrong with this code. Check whether the table does have points using phpmyadmin. Also you will have a number of rows in the table. Improve the logic for insertion as well. – T.Shah Jan 08 '18 at 06:01
  • I only have a row called points and also I dont know what is the problem I've tried looking up on google and youtube even stackoverflow But i STILL CANT FIND THE AWNSER –  Jan 09 '18 at 05:04
  • I rechecked the code, there is nothing wrong with it. Your problem lies some place else. Do check using phpmyadmin, whether there is anything inserted in the table. There must be number of rows as you keep checking, it will go on inserting new rows. – T.Shah Jan 09 '18 at 05:52
  • $points_query = mysqli_query($con, 'INSERT INTO user_points VALUES(' . $_SESSION["points"] . ')'); $points_query_select = mysqli_query($con, 'SELECT `points` FROM `user_points`'); while($row = mysqli_fetch_assoc($points_query_select)) { echo $row['points']; } –  Jan 09 '18 at 06:38
  • Thank you for helping me im only 10 years old so Its a big help thankyou –  Jan 14 '18 at 00:53