Array_Push not adding to end of array, it is replacing the whole array

517 Views Asked by At

I have tried to create a small 'bookmarking' feature for my website. Users are able to click on the ".bookmarkButton" which will execute the following script:

<!--Add To Bookmarks--> 
        $(".bookmarkButton").click(function() {
            var pid=$(this).closest('div').attr('id');
            $('#noBookmark').hide(); 
            $.post('bookmarks/addBookmark.php', 'rid=' + pid, function (addBookmark) {
              $("#bookmarkResults").add(addBookmark);
           });  
       });

Here is the code for "addBookmark.php":

    <?php

session_start();

if (isset($_SESSION['ridArray']) && count($_SESSION['ridArray'] > 0)){
    addBookmark();

} else if (isset($_POST['rid']) && !isset($_SESSION['ridArray'])) { 
    $_SESSION['ridArray'] = array(); 
    addBookmark(); 
}


function addBookmark() {  
    if (is_array($_SESSION['ridArray']) && isset($_SESSION['ridArray']) && isset( $_POST['rid']) ) { 
            array_push($_SESSION['ridArray'], $_POST['rid']); //push the id value from post to the session array
            //$_SESSION['ridArrayClean'] = array_unique($_SESSION['ridArray']); //remove duplicates
            print_r($_SESSION['ridArray']); 

            foreach($_SESSION['ridArray'] as $x) {
                // Get all the data from the "example" table
                $result = mysql_query("SELECT * FROM example WHERE id = $x") 
                or die(mysql_error()); 
                $row = mysql_fetch_array( $result );
                echo $row['productname'];
    }}}

    ?>

The variable $_SESSION['ridArray'] holds the array with all the id's that have been accumulated.

My problem is that this script works only when one item is bookmarked. When there is more than one product bookmarked, I only get the product name that was last bookmarked and not every thing that I've bookmarked.

So for example instead of getting multiple product id's after clicking the bookmarkButton class like this: 0,1,2,3 in the array. I only get the one that was clicked last i.e. 6.

I've been looking into this for a while now and I can't seem to see what I'm doing wrong.

2

There are 2 best solutions below

5
On

The script only echos the productnames, if you posted a "rid".

Also you could write the if like this:

if (isset($_SESSION['ridArray'], $_POST['rid']) && is_array($_SESSION['ridArray'])) { 

Checking isset() first. Also you could additionally check for

... && count($_SESSION['ridArray'] > 0)
0
On

I do not think that your session starts automatically (is it possible to set its autostart in php.ini, but it does not by default), so

<?php
session_start();

Other thoughts:

SELECT * FROM example WHERE id = $x

Have you ever heard about SQL Injection?

ps: no need in secondary check (they are checked before) and from the first condition follows the second one

is_array($_SESSION['ridArray']) && isset($_SESSION['ridArray'])

I would write it as

<?php
session_start();
if (isset($_POST['rid'])) { 
    addBookmark(intval($_POST['rid'])); 
}


function addBookmark($rid) {  
     $_SESSION['ridArray'][] = $rid; 
     $_SESSION['ridArray'] = array_unique($_SESSION['ridArray']);

     foreach($_SESSION['ridArray'] as $x) {
         $result = mysql_query("SELECT * FROM example WHERE id = '$x'") 
                   or die(mysql_error()); 
         $row = mysql_fetch_array( $result );
         echo $row['productname'];
     }
}
?>