V4.2.x Display Checkbox Extra Field as a List

Discussion in 'Modules / Plugins / Modifications' started by freeze2, Nov 30, 2011.

  1. freeze2 Super Moderator

    Using the Extra Fields plugin, I am trying to display the results of a checkbox as a <li>list</li>

    This is my code with the results showing a Pipe Character separating the results:

    Code:

    Code:
    {get_extra_field id=$view fid=14}
    Results:

    Air Conditioning|Cruise|Power Locks|Power Mirrors|Power Windows|

    I have read the following post that touches on this subject, but it appears that lots of things have changed in the last years. After trying various things mentioned in that post, I have failed at accomplishing this.

    Any help would be appreciated...thanks!
  2. Mike-N-Tosh Owner

    I haven't tested this, but This is extracted from my custom mnt_extra_fields.php plugin that I use all the time and modified for your particular output type (e.g. you may have to "fiddle" with it a little more ):

    From your copy of function.get_extra_field.php file around line 66:
    Find this:
    Code:
    $value = $rs['sValue'];
    Right after that ADD the following lines:
    Code:
    // Check if this is an extra field with multiple results
    	if (mb_substr_count($value, "|") > 0)
    	{
    		$value = str_replace("|", "</li><li>", $value'); // replace "|"
    		$str_count = mb_strlen($value); // Get length
    		$end = ($str_count - 5); // count w/o last <li>
    		$f_string = mb_substr($value'], 0, (int)$end);
    		// make the output list
    		$value = "<ul><li>";
    		$value .= $f_string;
    		$value .= "</ul>;
    	} else {
    
    After the next existing line:
    Code:
    $value = $rs['sValue'];
    ADD another line to close the "else" statement:
    Code:
    }
    Like I said, I didn't test this. I just spit it out, but this should work for you.

    Have fun,
    Mike
  3. freeze2 Super Moderator

    Thanks for this Mike...I will give it a whirl....

    p.s. you can spit a whole lot better than me
  4. freeze2 Super Moderator

    Hi Mike...I gave it a go and was unable to see the results due to parsing errors. Any thoughts, the error showed it was coming from:

    Code:
    $f_string = mb_substr($value'], 0, (int)$end);
  5. Mike-N-Tosh Owner

    Yep, there's an erroneous apostrophe and right bracket.

    $f_string = mb_substr($value'], 0, (int)$end);

    Just remove those

    $f_string = mb_substr($value, 0, (int)$end);
  6. freeze2 Super Moderator

    Thanks very much for the help Mike,

    I now get the following error:

    Parse error: syntax error, unexpected $end in /path-to-folder/plugins/function.get_extra_field2.php on line 88

    Line 88 being the end of the file

    The plugin code below:

    Code:
    <?php
    function smarty_function_get_extra_field2($params, &$smarty)
    {
    	global $db,$modules;
    	$id = isset($params['id']) ? (int) $params['id'] : 0;
    	$fid = isset($params['fid']) ? (int) $params['fid'] : 0;
    	$name = isset($params['name']) ? $params['name'] : FALSE;
    	if ($id > 0)
    	{
    		if ($name)
    		{
    			$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 ($result->isError() OR $result->size() == 0)
    			{
    				$value = FALSE;
    			}
    			$rs = $result->fetch();
    			$value = $rs['value'];
    			$result->freeResult();
    		}
    		else
    		{
    			$sql = 'SELECT sValue FROM '.PREFIX.'products_fields WHERE pID='.$id.' AND fID='.$fid;
    			$result = $db->query($sql);
    			if ($result->isError() OR $result->size() == 0)
    			{
    				return FALSE;
    			}
    			$rs = $result->fetch();
    			$value = $rs['sValue'];
    			// Check if this is an extra field with multiple results
    			if (mb_substr_count($value, "|") > 0)
    			{
    				$value = str_replace("|", "</li><li>", $value); // replace "|"
    				$str_count = mb_strlen($value); // Get length
    				$end = ($str_count - 5); // count w/o last <li>
    				$f_string = mb_substr($value, 0, (int)$end);
    				// make the output list
    				$value = "<ul><li>";
    				$value .= $f_string;
    				$value .= "</ul>;
    			} else {
    			$result->freeResult();
    			}
    		}
    		return $value;
    	}
    }
    /* End of file function.get_extra_field.php */
    /* Location: ./upload/plugins/function.get_extra_field.php */
    Any other thoughts?
  7. Mike-N-Tosh Owner

    Near the end, there is a missing quote

    $value .= "</ul>;

    Should be:
    $value .= "</ul>";

    Sheesh, who spit out this junk code!
  8. freeze2 Super Moderator

    Man, that I should have caught myself...

    I made the update and then noticed that the closing bracket was missing from the last list item:

    <ul><li>Air Conditioning</li><li>Cruise</li><li>Power Locks</li><li>Power Mirrors</li><li>Power Steering</li><li>Power Windows</li><li>Tilt Steering</li</ul>

    I changed the plugin code as below:

    Code:
    $value .= "</ul>";
    to

    Code:
    $value .= "></ul>";
    Now all appears to be working as expected...thanks very much for your help!

    It might be nice to see this as a core feature of the script in the future
  9. Mike-N-Tosh Owner

    Glad it worked for you, but your last edit (although it works) should actually be before in this line:
    Code:
    $end = ($str_count - 5); // count w/o last <li>
    change it to:
    Code:
    $end = ($str_count - 4); // count w/o last <li>
    That would be much better syntactically (is that a word????)
  10. freeze2 Super Moderator

    Well...I definetely like to be better syntactically .... Thanks again!

Share This Page