简体   繁体   English

如何显示Joomla K2的其他字段

[英]How to display Extra Fields from Joomla K2

I have this bit of code I am using to get the information from the k2_items table in the database, but all I get for the extra_fields is a JSON string and not the human readable id/values I need to print. 我有这段代码可以用来从数据库的k2_items表中获取信息,但是我获得的extra_fields只是一个JSON字符串,而不是我需要打印的可读ID /值。

Here is the code I'm using to display the database information, and the attempted code to pull the extra_fields values. 这是我用来显示数据库信息的代码,以及尝试提取extra_fields值的代码。

<?php


define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'' ));
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();





  $user = JFactory::getUser();


  $session =& JFactory::getSession();
  $user = JFactory::getUser();

  //echo 'Username is ' . $user->username;





  if($user->username === NULL)
  //user is not logged in
  {
        exit('You are not authorized to view this page. <a href = "' . JURI::root() . '">Please return to the homepage and login using the proper username/password.</a>' . $user->username);


  }
  else
  //user is logged in
  {

    echo "<p>Welcome $user->username</p>";
    echo '<p><a href = "http://ac.tradewindus.com/index.php?option=com_content&view=featured&Itemid=143">Go here to log out</a></p>';
  $db=JFactory::getDBO();
    $sql="SELECT * FROM #__k2_items WHERE published='1'";
    $db->setQuery($sql);
    $list=$db->loadObjectList();



    foreach($list as $item)
    {
        echo "<pre>";
        echo print_r($item, true);
        echo "</pre>";

    //convertArray to use ids as key
    $extrafields = array();
    foreach($item->extra_fields as $item)
    {
    $extrafields[$item->id] = $item->value;
    }

    echo $extrafields[1];
    echo $extrafields[2];
    echo $extrafields[3];
    echo $extrafields[4];
    echo $extrafields[5];

        //echo "<p>Title: $item->title</p>";
        //echo $item->introtext;
  }
}

?>

And this is the result of what I get for each "foreach" loop output. 这是我对每个“ foreach”循环输出所得到的结果。

stdClass Object
(
    [id] => 83
    [title] => SPEC SHEET: PANORAMAH 38 SASH BI-RAIL
    [alias] => spec-sheet-panoramah-38-sash-birail
    [catid] => 4
    [published] => 1
    [introtext] => 

SPEC SHEET: PANORAMAH 38 SASH BI-RAIL

    [fulltext] => 
    [video] => 
    [gallery] => 
    [extra_fields] => [{"id":"1","value":""},{"id":"2","value":["1"]},{"id":"3","value":["4"]},{"id":"4","value":["1","2"]},{"id":"5","value":["7"]},{"id":"6","value":["4"]}]
    [extra_fields_search] =>  PanoramAH  Minimal Frame  Door Window  Sliding  Spec Sheets  
    [created] => 2014-01-09 00:00:00
    [created_by] => 550
    [created_by_alias] => 
    [checked_out] => 0
    [checked_out_time] => 0000-00-00 00:00:00
    [modified] => 2014-08-06 22:44:57
    [modified_by] => 550
    [publish_up] => 2014-01-09 00:00:00
    [publish_down] => 0000-00-00 00:00:00
    [trash] => 0
    [access] => 1
    [ordering] => 14
    [featured] => 0
    [featured_ordering] => 0
    [image_caption] => 
    [image_credits] => 
    [video_caption] => 
    [video_credits] => 
    [hits] => 0
    [params] => {"catItemTitle":"","catItemTitleLinked":"","catItemFeaturedNotice":"","catItemAuthor":"","catItemDateCreated":"","catItemRating":"","catItemImage":"","catItemIntroText":"","catItemExtraFields":"","catItemHits":"","catItemCategory":"","catItemTags":"","catItemAttachments":"","catItemAttachmentsCounter":"","catItemVideo":"","catItemVideoWidth":"","catItemVideoHeight":"","catItemAudioWidth":"","catItemAudioHeight":"","catItemVideoAutoPlay":"","catItemImageGallery":"","catItemDateModified":"","catItemReadMore":"","catItemCommentsAnchor":"","catItemK2Plugins":"","itemDateCreated":"","itemTitle":"","itemFeaturedNotice":"","itemAuthor":"","itemFontResizer":"","itemPrintButton":"","itemEmailButton":"","itemSocialButton":"","itemVideoAnchor":"","itemImageGalleryAnchor":"","itemCommentsAnchor":"","itemRating":"","itemImage":"","itemImgSize":"","itemImageMainCaption":"","itemImageMainCredits":"","itemIntroText":"","itemFullText":"","itemExtraFields":"","itemDateModified":"","itemHits":"","itemCategory":"","itemTags":"","itemAttachments":"","itemAttachmentsCounter":"","itemVideo":"","itemVideoWidth":"","itemVideoHeight":"","itemAudioWidth":"","itemAudioHeight":"","itemVideoAutoPlay":"","itemVideoCaption":"","itemVideoCredits":"","itemImageGallery":"","itemNavigation":"","itemComments":"","itemTwitterButton":"","itemFacebookButton":"","itemGooglePlusOneButton":"","itemAuthorBlock":"","itemAuthorImage":"","itemAuthorDescription":"","itemAuthorURL":"","itemAuthorEmail":"","itemAuthorLatest":"","itemAuthorLatestLimit":"","itemRelated":"","itemRelatedLimit":"","itemRelatedTitle":"","itemRelatedCategory":"","itemRelatedImageSize":"","itemRelatedIntrotext":"","itemRelatedFulltext":"","itemRelatedAuthor":"","itemRelatedMedia":"","itemRelatedImageGallery":"","itemK2Plugins":""}
    [metadesc] => 
    [metadata] => robots=
author=
    [metakey] => 
    [plugins] => 
    [language] => *
)

The Extra Fields should be showing at the very bottom of this, but I guess nothing is being returned. Extra Fields应该显示在此的最底部,但是我想什么也没有返回。 When extra_fields is printed in the db output, the values are numbers like [1]; 在数据库输出中打印extra_fields时,值是类似于[1]的数字; but when I look at the K2 backend, they are text answers or dropdown selections; 但是当我查看K2后端时,它们是文本答案或下拉选项; human readable. 人类可读。 I'm not really a Joomla developer so I don't know if there is some kind of JRegistry thing or something I should be using, but I would appreciate being pointed in the right direction. 我不是真正的Joomla开发人员,所以我不知道是否有某种JRegistry东西或我应该使用的东西,但是我希望能指出正确的方向。 :D :D

Extra fields in K2 are stored in a JSON format. K2中的额外字段以JSON格式存储。 You will need to use json_decode. 您将需要使用json_decode。

$extra_fields = json_decode($item->extra_fields);

For more on K2 extra items, see this: http://www.itoctopus.com/on-leveraging-the-power-of-k2s-extra-items 有关K2额外项目的更多信息,请参见: http : //www.itoctopus.com/on-leveraging-the-power-of-k2s-extra-items

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM