2015-02-09 41 views
1

检索从字符串数据我有一个字段下面的输出中的Joomla($用品 - > attribs):试图在数据库

{"show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_layout":"ja_teline_v:p4p50","content_type":"p4p","ads":"1","ctm_boxer":{"name":["Floyd Mayweather","Manny Pacquiao","Wladimir Klitschko","Miguel Cotto","Gennady Golovkin","Sergey Kovalev","Guillermo Rigondeaux","Juan Manuel Marquez","Carl Froch","Saul Alvarez","Danny Garcia","Roman Gonzalez","Amir Khan","Mikey Garcia","Jhonny Gonzalez","Leo Santa Cruz","Adonis Stevenson","Abner Mares","Terence Crawford","Adrien Broner","Timothy Bradley","Juan Francisco Estrada","Robert Guerrero","Bernard Hopkins","Marco Huck","Nicholas Walters","Sergio Martinez","Julio Cesar Chavez Jr","Arthur Abraham","Nonito Donaire","Orlando Salido","Fernando Montiel","Humberto Soto","Marcos Maidana","Naoya Inoue","Donnie Nietes","Alexander Povetkin","Juan Carlos Reveco","Peter Quillin","Lucas Matthysse","Brian Viloria","Jamie McDonnell","Juergen Braehmer","Kell Brook","Deontay Wilder","Erislandy Lara","Jean Pascal","Carl Frampton","Omar Narvaez","Tomoki Kameda"],"rating":["5","5","4","4","3","3","3","4","3","3","3","3","3","3","3","2","3","2","2","3","3","2","3","5","2","2","3","2","3","4","2","3","2","3","3","2","2","2","2","2","2","2","2","1","1","1","2","1","2","1"],"movement":["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],"record":["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1-0, 1 KO","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1-0","0","0","0","0","0"],"ranking":["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50"],"highlight":["","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]},"ctm_topic_id":""} 

如何检索ctm_boxer每个值 - >名称中清单?

我试过了下面,但无济于事!

<?php $boxers = $item->attribs->get('ctm_boxer', array()); ?> 
<?php foreach($boxers['name'] as $index => $boxer_type): ?> 
<?php echo $boxer_type; ?> 

但它返回“致命错误:调用一个成员函数的get()一个非对象”

谁能帮助,为什么好吗?

谢谢, 马特

+0

'$ item-> attribs'应该返回某种类型的对象。你确定你已经在第一个地方设置了一个对象的'$ item'变量吗? – Tek 2015-02-09 20:37:21

+0

它在正确的表格中输出关联的字段,所以我假设是这样,是的... $ item被设置为特定的Joomla文章...感谢 – Matt 2015-02-09 21:06:16

+0

var_dump($ item-> attribs);'output ? – Tek 2015-02-09 22:03:07

回答

0

正如你可以看到从里面var_dump$item->attrib显示为string(2494)一个string。不是对象/ PHP类。这就是你得到Fatal Error: Call to a member function on a non-object的原因。因为再次,$item->attrib是正常的string

->解除引用操作符只能用于PHP对象/类。

More on how to use the object operator.

该变量似乎包含JSON格式的字符串。

尝试以下操作:

$BoxerCollectionObject = json_decode($item->attribs); 

$boxerArray = $BoxerCollectionObject->ctm_boxer->name; 

foreach ($boxerArray as $boxer) { 
    var_dump($boxer); //Should output the name of your boxers. 
} 

DEMO

0

这是伟大的 - 感谢 - 完美的作品......我扩大了一点用下面的一些字符串的其他部分带来:

<?php 
$BoxerCollectionObject = json_decode($item->attribs); 
$boxerArray = $BoxerCollectionObject->ctm_boxer->name; 
$boxerRating = $BoxerCollectionObject->ctm_boxer->rating; 
?> 
    <table class="table"> 
    <tbody> 
     <?php $i = 1;?> 
     <?php $ic = 1;?> 
     <?php foreach ($boxerArray as $index => $boxer) : ?> 
     <tr itemprop="boxer" class="boxer<?php echo $ic++; ?>"> 
     <td><strong><?php echo $i++; ?></strong></td> 
     <td><span><a href="/index.php?searchword=<?php echo $boxer; ?>&searchphrase=all&Itemid=215&option=com_search"><?php echo $boxer; ?></a></span></td> 
     <td itemprop="rating"><?php if ($boxerRating[$index] <= 0): ?> 
      <i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i> 
      <?php endif;?> 
      <?php if ($boxerRating[$index] == 1): ?> 
      <i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i> 
      <?php endif;?> 
      <?php if ($boxerRating[$index] == 2): ?> 
      <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i> 
      <?php endif;?> 
      <?php if ($boxerRating[$index] == 3): ?> 
      <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i> 
      <?php endif;?> 
      <?php if ($boxerRating[$index] == 4): ?> 
      <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i> 
      <?php endif;?> 
      <?php if ($boxerRating[$index] >= 5): ?> 
      <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i> 
      <?php endif;?></td> 
     </tr> 
     <?php endforeach; ?> 
    </tbody> 
    </table> 
</div> 

再次感谢,马特。