Trying to retrieve data from a string in database - php
I have the following output in one field in 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":""}
How do I retrieve every value from ctm_boxer -> name in a list?
I've tried the below, but to no avail!
<?php $boxers = $item->attribs->get('ctm_boxer', array()); ?>
<?php foreach($boxers['name'] as $index => $boxer_type): ?>
<?php echo $boxer_type; ?>
but it's returning 'Fatal error: Call to a member function get() on a non-object'
Can anyone help as to why please?
Thanks,
Matt
As you can see from var_dump inside $item->attrib is a string shown as string(2494). Not an object/PHP class. That's why you're getting Fatal Error: Call to a member function on a non-object. Because again, $item->attrib is a normal string.
The -> dereference operator can only be used on PHP Objects/Classes.
More on how to use the object operator.
That variable seems to contain a string in JSON format.
Try the following:
$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
That's great - thanks - works perfectly... I expanded it a bit with the following to bring in some of the other parts of the string:
<?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><?php echo $boxer; ?></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>
Thanks again, Matt.
Related
Showing Star Rating based on text file number in array [duplicate]
This question already has answers here: Displaying star rating with Font Awesome icons (4 answers) Laravel - Star rating - Optimization? (5 answers) Closed last month. I have a script which reads an array from a text file and grabs each line and produces star rating images based on number in array. The script is kind of big and was wondering if there was a better way of doing this. <?php if ($row[2] == '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 } elseif ($row[2] == '4.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-half-o"></i> <?php } elseif ($row[2] == '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 } elseif ($row[2] == '3.5') { ?> <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i> <?php } elseif ($row[2] == '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 } elseif ($row[2] == '2.5') { ?> <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i> <?php } elseif ($row[2] == '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 } elseif ($row[2] == '1.5') { ?> <i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i> <?php } elseif ($row[2] == '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 } elseif ($row[2] == '.5') { ?> <i class="fa fa-star-half-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 } elseif ($row[2] == '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 } ?>
Yes, better ways... A loop will help, maybe something like this (PHP demo at tio.run). function star_printer($num) { $star_class = [ 'blank' => 'far fa-star', 'half' => 'fas fa-star-half', 'full' => 'fas fa-star']; $star_html = '<i class="%s"></i>'; $star_str = ""; for($i=1; $i<=5; $i++) { $is_half = $i==ceil($num) && ceil($num)!=$num; $star = $is_half ? "half" : ($i>$num ? "blank" : "full"); $star_str .= sprintf($star_html, $star_class[$star]); } return $star_str; } echo star_printer(3.5);
You can work off this function (doesn't include the half stars, shows you how to do the full star rating - I just noticed you run halfs as well); function getStars($rating){ $stars = ""; $x = 0; while($x < $rating) { $stars = $stars.'<i class="fa fa-star"></i>'; $x++; } return $stars; } echo getStars(3); in your case - echo getStars($row[2]); You can tweak this that if the number contains a decimal you can append to the string '' for example; you get the gist :P -------------- Edit Tweaked it and added the half star rating :) function getStars($rating){ $stars = ""; $x = 1; while($x <= $rating) { $stars = $stars.'<i class="fa fa-star"></i>'; $x++; } if (strpos($rating, '.') !== false) { $stars = $stars . '<i class="fa fa-star-half">'; } return $stars; } echo getStars(3.5);
function getStar($rating) { $wholeStar = "<i class="fa fa-star"></i>"; $halfStar = "<i class="fa fa-star-half-o"></i>"; // round the rating for whole stars $roundedRating = round($rating); // Add the whole stars using str_repeat $starString = str_repeat($wholeStar, round($rating)); // Add the half star if the rating is bigger than it's rounded value if ($roundedRating < $rating) { $starString .= $halfStar; } return $starString } echo getStar($row[2]);
How to make if condition in HTML sidebar in PHP
I have a sidebar as follows in HTML, <div class="sidebar"> <h2 style="font-family: Verdana;">Dashboard</h2> <ul> <li><i class="fa fa-home"></i> Home</li> <li><i class="fa fa-paper-plane"></i> dummy1</li> <li><i class="fa fa-mobile"></i> dummy2</li> <li><i class="fa fa-phone"></i> dummy3</li> <li><i class="fa fa-plug"></i> dummy4</li> </ul> </div> I need to check the username and show the tab.By using below code part I could get the current user logged in. I need to check whether below value is equal to John if and only if I need to show the tabs. <?php echo htmlspecialchars($_SESSION["username"]); ?> As an example, if( htmlspecialchars($_SESSION["username"])=='John'){ <li><i class="fa fa-paper-plane"></i> dummy1</li> <li><i class="fa fa-mobile"></i> dummy2</li> <li><i class="fa fa-phone"></i> dummy3</li> <li><i class="fa fa-plug"></i> dummy4</li> } Can someone show me how to achieve this with php and HTML?
You're nearly there - just wrap the PHP bits in PHP tags. Also as an aside, you don't need htmlspecialchars() unless you're outputting the value into a HTML document. When you're just using it in an if, you're not outputting it. <?php if( $_SESSION["username"] =='John'){ ?> <li><i class="fa fa-paper-plane"></i> dummy1</li> <li><i class="fa fa-mobile"></i> dummy2</li> <li><i class="fa fa-phone"></i> dummy3</li> <li><i class="fa fa-plug"></i> dummy4</li> <?php } ?>
Why does my PHP tag does not work even though i (think i) did everything right?
I have a php file (converted from html) with a php tag inside that does not work, for some reason... Here is part of my code, thanks a lot for your help... <footer class="w3-container w3-blue w3-center w3-margin-top"> <p>Find me on social media.</p> <a href="https://facebook.com/maximilien.ollier"> <i class="fa fa-facebook-official w3-hover-opacity"></i><a/> <a href="https://www.instagram.com/mxollier/"> <i class="fa fa-instagram w3-hover-opacity"></i><a/> <a href="https://twitter.com/Maxollier"> <i class="fa fa-twitter w3-hover-opacity"></i><a/> <a href="https://www.linkedin.com/in/maximilien-ollier-526094154/"> <i class="fa fa-linkedin w3-hover-opacity"></i><a/> <a href="https://www.reddit.com/user/Heroic_watermelon"> <i class="fa fa-reddit w3-hover-opacity"></i><a/> <i class="fa fa-wechat w3-hover-opacity"></i><a/> <a> Copyright © <?php echo date("Y")." "; ?> <?php bloginfo('name')." "; ?>. All Rights Reserved.</a> </footer>
how do I edit the output of fgetcsv
I'm using eKomi and want to pull in reviews to my website. I've managed to get this working but there's a slight problem. Currently every single review is getting added, and in reverse order. You'll see from the code below that I am using fgetcsv. Is it possible to limit to the 12 latest reviews? and then order them from newest to oldest? Here's the code I've put together. <div class="row"> <div class="row__colspaced"> <?php $row = 1; if (($handle = fopen("http://api.ekomi.de/get_feedback.php?interface_id=66630&interface_pw=f2d097e83db1880e85e2f77aa&range=3m&type=csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo "<div class='colspan12-4 colspan6-3 as-grid with-gutters'><div class='content-module--review-list__item item' data-mh='review-item'>"; $num = count($data); $row++; for ($c=0; $c < $num; $c++) { if($c == 0){ echo gmdate('Y-m-d H:i:s',$data[$c]); } else if($c == 2){ if($data[$c] == 1){ echo '<div class="fa-star-rating"> <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> </div>'; } if($data[$c] == 2){ echo '<div class="fa-star-rating"> <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> </div>'; } if($data[$c] == 3){ echo '<div class="fa-star-rating"> <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> </div>'; } if($data[$c] == 4){ echo '<div class="fa-star-rating"> <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> </div>'; } if($data[$c] == 5){ echo '<div class="fa-star-rating"> <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> </div>'; } } else { echo '<div class="inner-item item'.$c.'">' . $data[$c] . "</div>"; } } echo "</div></div>"; } fclose($handle); } ?> </div> </div>
If statement inside echoed html not showing inside the html
I want to shorten a string when it's longer than a certain length for example 20. But when I add the if statement to my echoed html it outputs the result outside of the echoed html. Any idea why this happens? My code: foreach($contentcr as $content) { $contentje .= '<li class="job_listing"> <a href="http://www.website.nl/_extern/website/content.php?alias='.$content['alias'].'"> <div class="location job_img"> <img src="images/samples/person1.jpg" alt="" class="company_logo"> </div> <div class="location boldfont"> '.$content['title'].' </div> <div class="location"> '.$content['fulltext'].''; if (strlen($content['fulltext']) >= 20){ echo 'blala'; } $contentje .='</div> <div class="rating location"> <div class="rating-stars"> <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> </div> </div> <div class="location"> '.$content['metakey'].' </div> <div class="location"> <button type="submit" class="btn btn-primary buttonblock" onClick="location.href="http://www.website.nl/_extern/website/content.php?page="'.$content['alias'].'">Nu huren</button> </div> </a> </li>'; // Here } This is a table like structure, but the output of the if statement is echoed outside the html markup.
You are echoing the string inside if statement rather than appending that to your $contentje variable. See if statement below: foreach($contentcr as $content) { $contentje .= '<li class="job_listing"> <a href="http://www.website.nl/_extern/website/content.php?alias='.$content['alias'].'"> <div class="location job_img"> <img src="images/samples/person1.jpg" alt="" class="company_logo"> </div> <div class="location boldfont"> '.$content['title'].' </div> <div class="location"> '.$content['fulltext'].''; if (strlen($content['fulltext']) >= 20){ $contentje .= 'blala'; } $contentje .='</div> <div class="rating location"> <div class="rating-stars"> <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> </div> </div> <div class="location"> '.$content['metakey'].'</div> <div class="location"> <button type="submit" class="btn btn-primary buttonblock" onClick="location.href="http://www.website.nl/_extern/website/content.php?page="'.$content['alias'].'">Nu huren</button> </div> </a></li>'; // Here }