Hi all,
Wow - what a change - long time since I've been on here!
Just a quicky, and I think I'm missing something blindingly obvious, as usual.
I'm storing a list of ISBNs in a single db field as a comma separated list.
When a user clicks on a book title, it calls a function, passing that book's ISBN. Because they've clicked on it, I want to add it to their 'recently viewed' list.
The function code is below.
The problem is that the in_array() function is doing something weird...
When I click a title that's already viewed it correctly states "[the isbn] already in list"
When I click a title that's NOT already been viewed, it INcorrectly states "[the isbn] already in list".
BUT...
When I click a title that's already viewed it DOESN'T re-add the isbn to the array to put it back in the db, so on one level it IS correctly picking up the fact that the ISBN is already in the list.
I'm baffled!
Can someone please point out what I'm missing here?
function logISBN($theIsbn, $user) {
require_once("../includes/bid_conn.php");
//get current list of ISBNs
$logISBNQuery = "SELECT user_titles FROM bid_users WHERE user_email = '$user'";
$logISBN = mysql_query($logISBNQuery) or die(mysql_error());
$log = mysql_fetch_array($logISBN);
extract($log); //gives $user_titles - the current list
//
//turn current comma separated list into array
if(trim($user_titles) !==''){
$list = explode(',', $user_titles);
} else {
$list = array();
}
if (in_array($theIsbn, $list, true)) {
echo $theIsbn.' already in list';
} else {
echo $theIsbn.' not in list, adding now...';
//check number of values in array
$isbnCount = count($list);
if ($isbnCount >= 20) {
$remove = array_shift($list);
}
//add current ISBN to the array
array_push($list, $theIsbn);
}
//make it a comma separated list again
$newList = implode(',', $list);
//add it back to the db
$updateISBNQuery = "UPDATE bid_users SET user_titles = '$newList' WHERE user_email = '$user'";
$updateISBN = mysql_query($updateISBNQuery) or die(mysql_error());
unset($list);
unset($theIsbn);
One other small question - ideally, if the isbn exists, I'd like to move it to the end of the array, but I've no idea how to do that - any pointers would be most gratefully received.
Thanks in advance for your help!