Recent Topics

1 Feb 03, 2007 13:26    

Here is some code that will let you add and remove items in a itemlist.

	function add_Item ( & $ItemList, $row, $update_postIDlist = false, $update_cats = false )
	{
		// Create the Obj
		$Obj = new $ItemList->objType($row, $ItemList->dbtablename, $ItemList->dbprefix, $ItemList->dbIDname);
				
		// Prepend the object
		array_unshift( $ItemList->Obj, $Obj );
			
		// Prepend the row
		array_unshift( $ItemList->rows, $row );
				
		// Append the id onto the postIDarray
		$ItemList->postIDarray[] = $row->post_ID;
				
		// To avoid potential future waste, cache this object:
		$ItemList->DataObjectCache->add( $ItemList->Obj[0] ); 
				
		// The row is new, so increase row count
		$ItemList->result_num_rows++;
		
		// 
		if ( $update_postIDlist )
			$ItemList->postIDlist = implode( ',', $ItemList->postIDarray );
		
		// 
		if ( $update_cats )
		{
			unset($GLOBALS['cache_postcats']);
			cat_load_postcats_cache();
		}
		
		return true;
	}

	function remove_Item ( & $ItemList, $id, $update_postIDlist = false )
	{	/**
		 * RETURNS:
		 * - TRUE: item existed and remove
		 * - FALSE: item did not exist, so wasn't removed
		 * - NULL: error
		 */
		
		$id_i = array_search($id, $ItemList->postIDarray);
		$item_exists = $id_i !== false;
		
		# Check if row already exists
		if ( $item_exists )
		{	
			// Determine the row index, as postIDarray is in reverse compared to rows and Objs
			$row_i = $ItemList->result_num_rows-1-$id_i;
			
			// Unset the post
			unset($ItemList->rows[$row_i]);
			unset($ItemList->Obj[$row_i]);
			unset($ItemList->postIDarray[$id_i]);
			
			// Recreate indexes
			$ItemList->rows = array_values($ItemList->rows);
			$ItemList->Obj = array_values($ItemList->Obj);
			$ItemList->postIDarray = array_values($ItemList->postIDarray);
			
			// Decrease row count
			$ItemList->result_num_rows--;
		}
		
		// 
		if ( $update_postIDlist )
			$ItemList->postIDlist = implode( ',', $ItemList->postIDarray );
		
		return $item_exists;
	}

here is a example use

		# -----------------------------
		# Add results to Main List
		
		// Create the Objs
		for ( $i = 0, $n = sizeof($rows); $i < $n; $i++ )
		{
			# Get the row
			$row = $rows[$i];
			
			// Remove the item if it already exists
			$this->remove_Item($MainList, $row->post_ID);
			
			// Decide if we want to re-add/display the item
			switch ( true )
			{
				case empty($page):
				case $this->Settings->get('all_pages_stickies') && $row->post_ptyp_ID == $this->Sticky->ID:
				case $this->Settings->get('all_pages_announcements') && $row->cat_blog_ID == $this->Announcements->ID:
					// We do
					$this->add_Item($MainList, $row, false, false);
					break;
				
				default:
					break;
			}
		}
		
		// Update the postIDlist
		$MainList->postIDlist = implode( ',', $MainList->postIDarray );
		
		// Update the cats
		unset($GLOBALS['cache_postcats']);
		cat_load_postcats_cache();

Taken from the Sticky Announcements Plugin


Form is loading...