试图从数据库中的字符串中检索数据

时间:2015-02-09 20:32:07

标签: php database string foreach get

我在Joomla的一个字段中有以下输出($ item-> 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; ?>

但它正在返回&#39;致命错误:在非对象上调用成员函数get()&#39;

有人可以帮忙吗?

谢谢, 马特

2 个答案:

答案 0 :(得分:0)

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

$BoxerCollectionObject = json_decode($item->attribs); $boxerArray = $BoxerCollectionObject->ctm_boxer->name; foreach ($boxerArray as $boxer) { var_dump($boxer); //Should output the name of your boxers. } 解除引用运算符只能用于PHP对象/类。

More on how to use the object operator.

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

尝试以下方法:

{{1}}

DEMO

答案 1 :(得分: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>

再次感谢,马特。