When multiple variables match, remove from array PHP - php

So I currently have this, so if $iDontWant matches something in the array from $data->Name, it removes the whole line. Using the same method how could I do the same but with multiple $iDontWant? So say another $iDontWant2, and this also remove from the foreach? Every way I have tried seems to break things!
$iDontWant="bla"
foreach($datas as $data) {
if($data->Name == $iDontWant) continue;
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
Any help would be great, thanks!

Declare $iDontWant as an array and use in_array():
$iDontWant = ["one", "two"];
foreach($datas as $data) {
if(in_array($data->Name, $iDontWant)) {
continue;
}
}

As an alternative to only loop the ones you want, just take the difference and loop that:
$iDontWant = ["one", "two"];
foreach(array_diff($datas, $iDontWant) as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}

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]);

Convert number to Stars

I've created this shortcode that converts a number to a star rating. At the moment, the shortcode works but not fully :
function Shortcode() {
$starNumber = get_field('note_independant');
for($x=1;$x<=$starNumber;$x++) {
$output .= '<i class="fa fa-star" aria-hidden="true"></i>';
}
if (strpos($starNumber,',')) {
$output .= '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
$x++;
}
while ($x<=5) {
$output .= '<i class="fa fa-star-o" aria-hidden="true"></i>';
$x++;
}
return $output;
}
add_shortcode('helloworld', 'Shortcode');
Let say my field value note_independant = 3.
The shortcode output is ★★★ instead of ★★★☆☆.
I have also an issue when using a number with decimal like 3.5. The shortcode won't output half stars...
the problem may depend on font Awesome version you use
in FA 5.5 use fa fa-star-half-alt for half star and far fa-star for blank star
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<div class="wrap">
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star-half-alt" aria-hidden="true"></i>
<i class="far fa-star" aria-hidden="true"></i>
</div>
and don't forget to replace comma , with dot . in if (strpos($starNumber,',')) {
First remove aria-hidden="true" to display all blank and half star. Read more about aria-hidden
Replace ',' to '.' to get work with decimal
if (strpos($starNumber,'.')) {
Otherwise your code is working well : Check Online
You can use either use above solutions or WordPress star rating function instead.
Learn more about wp_star_rating here

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>

PHP Hide text from array

I'm pulling data from an array, but show certain parts of the data above others, I've managed to choose certain text to go above others. Is there anyway to hide text from the array if it matches the piece of text I give it, using the below code?
<?php $datas = array_slice($facilities, 0, $leftoshownumber); // First 6 items
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
This is the full code for this section
<div class="checkmark-outer">
<?php $startnumber = 6; $parkingnumber = 0; $gardennumber = 0; $dishwashernumber = 0; $summerrentnumber = 0; ?>
<?php if (strpos($facilitiesstring, 'Reduced Summer') !== false) { $summerrentnumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Reduced Summer Rent Available</div></div>';}; ?>
<?php if (strpos($facilitiesstring, 'Parking') !== false) {$parkingnumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Parking Available</div></div>';}; ?>
<?php if (strpos($facilitiesstring, 'Garden') !== false) {$gardennumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Garden</div></div>';}; ?>
<?php if (strpos($facilitiesstring, 'Dishwasher') !== false) {$dishwashernumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Dishwasher</div></div>';}; ?>
<?php $totalfixednumber = $parkingnumber + $gardennumber + $dishwashernumber + $summerrentnumber; $leftoshownumber = $startnumber - $totalfixednumber;?>
<?php $datas = array_slice($facilities, 0, $leftoshownumber); // First 6 items
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
<div class="clearfix"></div>
<div class="read-more-show hide">Show More</div><div class="read-more-content"><?php $datas = array_slice($facilities, 6); // Items after 6
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?><div class="clearfix"></div><div class="read-more-hide hide">Show Less</div></span></div>
</div>
Any help would be great!
Do you mean you don't want to print the whole line, when the name matches a given variable?
Then I would do:
$iDontWant="bla"
foreach($datas as $data) {
if($data->Name == $iDontWant) continue;
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
EDIT:
if you want multiple names not to show you could do:
$iDontWant1="bla1"
$iDontWant2="bla2"
$iDontWant3="bla3"
foreach($datas as $data) {
if($data->Name == $iDontWant1 || $data->Name == $iDontWant2 || $data->Name == $iDontWant3) continue;
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
('||' stands for 'or' by the way)
So basically what you are doing is saying, when
either the name is the same as the content of $iDontWant1, which is "bla1"
or the name is the same as $iDontWant2, which is "bla2"
or the name is the same as the content of $iDontWant3, which is "bla3"
do continue (don't print this part).
However if you know what "bla1", "bla2" ... are, it is easier to just do
foreach($datas as $data) {
if($data->Name == "bla1" || $data->Name == "bla2" || $data->Name == "bla3") continue;
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
You will just compare to the String directly instead of comparing to the variable containing the string.
I hope I can help you with this.
But if you'd like to do more stuff with PHP I'd recommend a tutorial like https://secure.php.net/manual/en/tutorial.php to really understand the concepts behind this language

PHP Foreach Skip Results from Array

I'm trying to limit the amount of results that show in my foreach, I've got this so far:
<?php $facilitiescounter = 0; ?>
<?php foreach ($facilities as $data) {
if (++$facilitiescounter == 7) break;
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; }?>
<a class="read-more-show hide" href="#">Show More</a> <span class="read-more-content">Show all other results from array <a class="read-more-hide hide" href="#">Show Less</a></span>
I have managed to limit it to the first 6 results, is it possible to do another foreach to get all the other results excluding the first six?
Or is there a better way of doing this?
Thanks!
A simple for loop is more appropriate in this case, as we can determine begin and end easily
To the first six results:
for($i = 0, $t = min(6, count($facilities)); $i < $t; ++$i)
{
$facilities[$i]->Name
....
The other ones:
for($i = 6, $t = count($facilities); $i < $t; ++$i)
{
$facilities[$i]->Name
....
Note: added minimum verification in case $facilities has less than 6 elements. Thanks to #lanis
You can use array_slice() http://php.net/manual/fr/function.array-slice.php
$datas = array_slice($facilities, 0, 6); // First 6 items
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}
$datas = array_slice($facilities, 6); // Items after 6
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}
Yes, you can do it as a better way. Just get your $facilities array in some temporary array which is called $temp and then whenever your foreach loop is executing just remove that element from the $temp array. After that you just replace $facilities array with the $temp.
<?php $facilitiescounter = 0; ?>
<?php foreach ($facilities as $data) {
if (++$facilitiescounter <= 7) break;
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; }?>
<a class="read-more-show hide" href="#">Show More</a> <span class="read-more-content">Show all other results from array <a class="read-more-hide hide" href="#">Show Less</a></span>
i have changed the if condition only. it will give you proper result. try it!

Categories