I have a JSON string within a MySQL database which I could like to get 2 numbers from within WordPress.
My string is as follows:
a:1:{i:300;a:4:{s:7:"lessons";a:1:{i:302;i:1;}s:6:"topics";a:1:{i:302;a:2:{i:304;i:1;i:311;i:1;}}s:9:"completed";i:1;s:5:"total";i:4;}}
The i:300 is the course ID. Within the topics is both 302 and 311. These are the IDs of each page completed. I need to get the last one of these which in this case would be 311.
I basically need something that would do the following:
Get Page ID Find Page ID in JSON string Get the last ID from JSON string topics
How would I go about this?
Update - Getting the string from the DB but doesn't display.
$conn = new mysqli($servername, $username, $password, $dbname);
$user_id = get_current_user_id();
$sql1 = "SELECT meta_value FROM xrji_usermeta";
$sql3 = " WHERE user_id = ".$user_id." AND meta_key = '_sfwd-course_progress';";
$sql = $sql1 . "" . $sql3;
//echo "sql: ".$sql;
$a = $conn->query($sql);
$results = unserialize($a);
echo "<br>results: ".$results;
I have the above however nothing is shown for $results however if I run the query through sequel pro it works fine.
SELECT meta_value FROM xrji_usermeta WHERE user_id = 2 AND meta_key = '_sfwd-course_progress'
unserialize()it; that access it as ordinary array; demo.array_pop()to get last value of the needed array.