get_extra_field by internal name

Discussion in 'Customizations' started by jimdoescode, Jul 15, 2010.

  1. jimdoescode Customer

    I wrote a small plugin to perform the get_extra_field function using a specified internal name instead of the id. Not sure if anyone would find this useful I was just having trouble remembering the extra field ids.

    Code:
    <?php
    /**
    Call like {get_extra_field_by_name id=entry.id name='internal-name'} 
    **/
    function smarty_function_get_extra_field_by_name($params, &$smarty)
    {
    	global $db,$modules;
    	$id = isset($params['id']) ? (int)$params['id'] : 0;
    	$name = isset($params['name']) ? $params['name'] : '';
    	if($id > 0)
    	{
    		$sql = "SELECT v.sValue AS value FROM ".PREFIX."fields n, ".PREFIX."products_fields v WHERE v.fID = n.fID AND v.pID = {$id} AND n.fInternal = '{$name}'";
    		$result = $db->query($sql);
    		//If everything checked out
    		$value = false;
    		if(!$result->isError() && $result->size() > 0)
    		{
    			$dbvals = $result->fetch();
    			$value = $dbvals['value'];
    		}
    		$result->freeResult();
    		return $value; 
    	}
    }	
    ?>
    
    Copy the code above into a file called function.get_extra_field_by_name.php and place it into your plugins folder. You call is using the same syntax you would with the regular get_extra_field but instead of specifying fID you would set name equal to the internal name of your field.

    There is a bit more sql processing that has to be done, but it will most likely be negligible unless you are displaying thousands of ads on a single page (in which case this plugin would be the least of your worries)

    Feel free the do whatever you want with this code.
  2. seymourjames All Hands On Deck

    A nice contribution and thanks for sharing.

Share This Page