Category: PHP

  • Convert JSON Array to PHP Array

    To convert a JSON array into a PHP array, use the following script: <?php $json = file_get_contents(‘json/yourscript.json’); // Get the JSON data $phpObj = json_decode($json,true); // Convert to PHP Object print_r($phpObj); // Convert to PHP Array ?> file_get_contents(‘json/yourscript.json’); Allows you to get the JSON data easily. json_decode($json,true); Is a PHP function that converts your JSON…

  • Get only the Domain Name from HTTP_REFERRER in PHP

    $_SERVER[‘HTTP_REFERER’] gives you the URL from where the user came, which is the user’s previous page (whatever page that would appear if the user clicked “Back” in their browser) But $_SERVER[‘HTTP_REFERER’]  gives you the complete URL, something like this : driverstest.info/test/quiz.php?quizgroup=7 If you just want get the domain name .i.e. driverstest.info from the full URL, then you can…

  • Convert Month Numbers to Month Names in PHP

    Here is a lovely bit of code to help you convert months in numbers to proper names! <?php $monthNum = 11; $monthName = date(“F”, mktime(0, 0, 0, $monthNum, 10)); echo $monthName; // Outputs: November ?>

  • Finding nested items in JSON object or associative array using PHP

    I had this problem recently when I used made a call to Yahoo Finance’s Stock quotes API for my Chinese website about US Stocks. Yahoo’s API returns the stock data in JSON form, but you can choose to handle it as a php object or associative array. First, this post by Michael Nitschinger really helped…

  • Dealing with apostrophes and quotation marks for forms and output with PHP

    I have run into the same annoying problem for a long time and finally found an easy solution. Whenever forms are submitted via PHP (by a user or myself) and apostrophes or quotation marks are added in forms, they are escaped using mysqli_real_escape_string and entered into the database without a hitch. However, when outputting the data…

  • Count elements in a database based on how many unique values there are in a field with MySQL

    I had this problem come up when I needed to know how many times a particular essay question had been answered with a written submission.   $query = “SELECT COUNT(id) FROM table WHERE essay_id = ‘$essay_id'”; $data = mysqli_query($dbc, $query); $row = mysqli_fetch_array($data); echo $row[‘0′].’ times’; // # times

  • How to keep the carriage return in textarea using PHP?

    I’ve created submission forms with the textarea box, but when I echoed out the submission, all the carriage returns disappeared! Of course you don’t want to force the user to type <br /> or <p> tags into the posts. Actually, I learned that the carriage returns did not disappear, you simply need to add a…

  • Find a needle in a haystack with PHP in_array

    Want to see if a single item is in an array in PHP? Use in_array. For example, you have an array of colors and you want to find if red exists. Try this… $array = array(red, blue, green); if (in_array(“red”, $array)) { echo “has red”; }