试图从数据库中的字符串中检索数据
Trying to retrieve data from a string in database
我在 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; ?>
但它正在返回 'Fatal error: Call to a member function get() on a non-object'
谁能帮忙解释一下为什么?
谢谢,
马特
正如您在 var_dump
中看到的那样 $item->attrib
是一个 string
,显示为 string(2494)
。不是 object/PHP class。这就是您得到 Fatal Error: Call to a member function on a non-object
的原因。因为,$item->attrib
是正常的 string
.
->
解引用运算符只能用于 PHP Objects/Classes。
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
太好了 - 谢谢 - 完美地工作...我用以下内容对其进行了一些扩展以引入字符串的其他部分:
<?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>
再次感谢马特
我在 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; ?>
但它正在返回 'Fatal error: Call to a member function get() on a non-object'
谁能帮忙解释一下为什么?
谢谢, 马特
正如您在 var_dump
中看到的那样 $item->attrib
是一个 string
,显示为 string(2494)
。不是 object/PHP class。这就是您得到 Fatal Error: Call to a member function on a non-object
的原因。因为,$item->attrib
是正常的 string
.
->
解引用运算符只能用于 PHP Objects/Classes。
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
太好了 - 谢谢 - 完美地工作...我用以下内容对其进行了一些扩展以引入字符串的其他部分:
<?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>
再次感谢马特