0

I have the following function:

function addToShortlist($propref) {

    global $wpdb;

    if (isset($_SESSION['shortlist'])) {

        $shortlist = new Namespace\Shortlist($_SESSION['shortlist']);
        $result = $shortlist->addProperty($propref);
    }
    elseif (empty($_SESSION['shortlist'])) {

        $list_id = $wpdb->get_var("SELECT MAX(list_id) FROM " . $wpdb->prefix . "ch_shrtlst");
        $new_list_id = ++$list_id;
        // Insert into database
        if ($wpdb->insert( 
            $wpdb->prefix . 'ch_shrtlst', 
            array( 
                'list_id' => $new_list_id,
                'property_id' => $propref,
                'time' => current_time('mysql')
            )
        )
        ) {
            $_SESSION['shortlist'] = $new_list_id;
            $result = new Namespace\Shortlist($_SESSION['shortlist']);
        }
    }
    return $result;
}

In the class, I have the following method

public function addProperty($propref) {

    global $wpdb;

    if ($wpdb->insert( 
            $wpdb->prefix . 'ch_shrtlst', 
            array( 
                'list_id' => $this->list_id, 
                'property_id' => $propref,
                'time' => current_time('mysql')
            )
        )
    ) {
        return $this;
    }
}

When the session is set and the function calls the method on the object, the database query works perfectly, but for some reason, the query won't work directly from the function. I'm not getting any errors or anything, just nothing happening. Any ideas?

  • Probably the code is skipping because the elseif condition is not fulfilled? – Sisir Jul 26 '15 at 12:29
  • I thought that. If I remove the if statement altogether the code runs (albeit not in Safari). I've tried elseif (!isset($_SESSION['shortlist'])) I can't see how else I can do it. The session variable is either there or not I would have thought. – Badger Jul 26 '15 at 12:48
  • This is driving me crazy. In firefox it works, but only if I refresh the page and in Safari, it won't work at all. – Badger Jul 26 '15 at 13:01
  • I ought to mention, if it's relevant that the function is run from an AJAX call. – Badger Jul 26 '15 at 13:07
0

This function was being made by an ajax call. The anchor link that made the call pointed to the same url. I changed the href to # and it now works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.