This is a web app that works when user searches for an item to add to their list and the search results show an item that has previously been added to their list.
However, if the search results find an item that has not previously been added to the user’s list then the user should be able to add this new item to their list.
Here’s the the first function which works for adding an item that was previously on a user’s list; (note the “if ($user_item_id == ‘null’ …” line… this should be calling a second function called “addToUserItems”)
function addItem() {
$user_item_id = $this->uri->segment( 3 );
$master_item_id = $this->uri->segment( 4 );
$store = $this->uri->segment( 5, '' );
$user_id = $this->main_model->getMasterUser( $this->user_id );
if ( $user_item_id == 'null' ) $user_item_id = $this->main_model->addToUserItems( $master_item_id, $user_id );
$quantity = $this->main_model->addToShoppingList( $user_item_id, $store, $user_id );
$message = ( $quantity == '1' ) ? 'Added to list.' : 'On shopping list ' . $quantity . ' times.';
$message .= ' <span class="undo_button" params="unadd/' . $user_item_id . '">Undo?</span>';
$return = array(
'message' => $message,
'user_item_id' => $user_item_id,
);
echo json_encode( $return );
}
Here’s the second function which should be setting “user_item_id” but apparently is not;
function addToUserItems( $master_item_id, $user_id ) {
$query = $this->db->query( "
INSERT INTO user_item ( master_item_id, user_id, custom_category, custom_name, custom_store, expires_after, on_hand )
SELECT id, " . $user_id . ", original_category, original_name, '', default_expiration, 0
FROM master_item
WHERE id = '" . $master_item_id . "'
;" );
return $this->db->insert_id();
}
Any ideas why we are seeing “user_item_id” undefined? My guess, after hours of testing assumptions, is that somehow the second function is not setting the value for $user_item_id.
thanks for the suggested fixes; turns out that the user_item_id variable was not being set if the user_item_id field did not have a value. code in other file corrected to include $result[‘user_item_id’] = ‘null’; furthermore, the addItem function above was looking for a user_item_id variable value of ‘null’ whereas a javascript file, not mentioned in my original question, was looking for the user_item_id variable to be null. so, we had a variable that needed to be set and code in other files which needed to look consistently for ‘null’ vs null.