WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] trying to convert a fuction that uses mysqli to use the wpdb class (2 posts)

  1. Robert Hurst
    Member
    Posted 4 years ago #

    Hi guys i got a bit of a problem, I'm trying to make a set of function to pull items from an added table and build an interface to sort products with. the problem is that i can't figure out how to get the wpdb class to work like mysqli. i wrote it with mysqli and it worked till i updated to 2.8.2 and i figure useing mysql over wordpress is a bit of a hack. anyway here are my two functions that talk (or at least use to) to the database.

    function get_product_manufacturers($var=NULL)
    {
    	global $wpdb;
    	$query = 'SELECT * FROM akt_products ORDER BY id asc';
    	$results = $wpdb->query($query, 'OBJECT');
    	if($results->num_rows) {
    		while($row = $results->fetch_object())
    		{
    			$manufacturers_all[$row->id] = stripcslashes(htmlspecialchars_decode($row->manufacturer));
    		}
    		$manufacturers = array_unique($manufacturers_all);
    		if($var=='all')
    		{
    			return $manufacturers_all;
    		}else{
    			return $manufacturers;
    		}
    	}
    }
    
    function get_product_categories($var=NULL)
    {
    	//get the catergories
    	global $wpdb;
    	$query = 'SELECT * FROM akt_products ORDER BY id asc';
    	$results = $wpdb->query($query);
    	if($results->num_rows) {
    		while($row = $results->fetch_object())
    		{
    			$categories_all[$row->id] = stripcslashes(htmlspecialchars_decode($row->category));
    		}
    	}
    
    	//build the array - NOT UNIQUE!
    	$manufacturers_all = get_product_manufacturers('all');
    	if($var==NULL):
    		break;
    	elseif($var=='all'):
    		$categories = array_unique($categories_all);
    	else:
    		$i=0;
    		foreach($manufacturers_all as  $key => $manufacturer)
    		{
    			if($manufacturer != $var)
    			{}
    			elseif($manufacturer == $var)
    			{
    				$categories_current[$i] = $categories_all[$key];
    			}
    			$i++;
    		}
    		$categories = array_unique($categories_current);
    	endif;
    	return $categories;
    }
  2. Robert Hurst
    Member
    Posted 4 years ago #

    i figured it out this is what I have now

    function get_product_manufacturers($var=NULL)
    {
    	global $wpdb;
    	$query = 'SELECT * FROM akt_products ORDER BY id asc';
    	$results = $wpdb->get_results($query);
    	foreach($results as $row)
    	{
    		$manufacturers_all[$row->id] = stripcslashes(htmlspecialchars_decode($row->manufacturer));
    		$manufacturers = array_unique($manufacturers_all);
    	}
    	if($var=='all')
    	{
    		return $manufacturers_all;
    	}else{
    		return $manufacturers;
    	}
    }
    
    function get_product_categories($var=NULL)
    {
    	//get the catergories
    	global $wpdb;
    	$query = 'SELECT * FROM akt_products ORDER BY id asc';
    	$results = $wpdb->get_results($query);
    	foreach($results as $row)
    	{
    		$categories_all[$row->id] = stripcslashes(htmlspecialchars_decode($row->category));
    	}
    
    	//build the array - NOT UNIQUE!
    	$manufacturers_all = get_product_manufacturers('all');
    	if($var==NULL):
    		break;
    	elseif($var=='all'):
    		$categories = array_unique($categories_all);
    	else:
    		$i=0;
    		foreach($manufacturers_all as  $key => $manufacturer)
    		{
    			if($manufacturer != $var)
    			{}
    			elseif($manufacturer == $var)
    			{
    				$categories_current[$i] = $categories_all[$key];
    			}
    			$i++;
    		}
    		$categories = array_unique($categories_current);
    	endif;
    	return $categories;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic