php if-statement based on integers is not displaying results? - php

I'm using the following preg_match:
preg_match( '!<div class="thumblock ">(.*)</div>!si' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
$thumbs_string = strip_tags( $n[1] );
To extract the number between the span tags:
<div class="thumblock ">
<span class="rating-result">
<div id="gdsr_thumb_text_12_a" class="gdt-size-20 voted inactive gdthumbtext">+1</div>
</span>
<div class="ratingtext ">
<div class="raterclear"></div>
</div>
(in the example above, result is a string: "+1")
So I tried converting it into an integer with this:
$thumbs_number = (int)$thumbs_string;
which is used in this function:
function get_rating_class($thumbs_number) {
if ($thumbs_number < 0) return ' bad';
if ($thumbs_number < 2) return ' average';
if ($thumbs_number < 4) return ' good';
return ' excellent';
}
function rating_class($thumbs_number) {
echo get_rating_class($thumbs_number);
}
to output a div class:
<div class="topic-like-count<?php rating_class($thumbs_number); ?>">
I even did var_dump():
<h2><?php var_dump($thumbs_string); ?></h2>
<h2><?php var_dump($thumbs_number); ?></h2>
and the results were:
string(2) "+1" and int(1) respectively (I directly copy/pasted them here).
But no div class is being output.
Any suggestion to fix this?
EDIT:
The class is indeed being output in the HTML source, but it isn't having any effect (and my stylesheet is not being cached). I have another version of the function which doesn't add an extra div around the span tags, and that one works but unfortunately I need that div.

If the class name is being displayed in the HTML then the PHP code is fine. The stylesheet is likely the problem.

Sure.. add an echo before the call to rating_class($thumbs_number);:
<div class="topic-like-count<?php echo rating_class($thumbs_number); ?>">

Why are you using an extra function that's only used to echo? The following should work just fine.
<div class="topic-like-count<?=get_rating_class($thumbs_number);?>">

Related

PHP function or how to use this code many times in different instances on one page, not using php include

So I want to include this code many times on one page with different data:
if (!empty($faq[$fisf][$title]) ) {
echo '<div class="card card-body col-12"><h5 class="card-title">'.$faq[$fisf][$title].'</h5>';
if (!empty($faq[$fisf][$image]) ) {
echo '<img src="'.$faq[$fisf][$image].'" alt="'.$faq[$fisf][$title].'" class="img-fluid">';
}
if (!empty($faq[$fisf][$title]) ) {
echo $faq[$fisf][$text];
}
echo '</div>';
}
What I normally do is put this code in a separate file and then include it as many times as I need it and depending on where that code is in a loop or foreach it will show the relevant data!.
But I'm trying to figure out a better way of doing it, like using a function!
Kind of like this:
function faqmodal() {
if (!empty($faq[$fisf][$title])) {
echo '<div class="card card-body col-12"><h5 class="card-title">'.$faq[$fisf][$title].'</h5>';
if (!empty($faq[$fisf][$image])) {
echo '<img src="'.$faq[$fisf][$image].'" alt="'.$faq[$fisf][$title].'" class="img-fluid">';
}
if (!empty($faq[$fisf][$title])) {
echo $faq[$fisf][$text];
}
echo '</div>';
}
}
I want this function to run within different foreach's and loading the relevant data to where its run.
faqmodal();
So this part will depend on what the foreach telling it to load it with :
$faq[$fisf][$title]
But now the function does not convert the code above to be relevant whats in the foreach loop is telling it to fill it with!
But this might not be the smartest way of doing it, I'm open for any ideas!
I'm not sure I understood correctly, this should be solvable by passing the data as an argument of the function like this:
function faqmodal($title, $image, $text){
if (!empty($title) && !empty($image) && if (!empty($text)) {
echo '
<div class="card card-body col-12">
<h5 class="card-title">'.$title.'</h5>
<img src="'.$image.'" alt="'.$title.'" class="img-fluid">
'.$text.'</div>'; //you might wanna think about moving the text into e.g. a <span> tag
}
}
If you now want to call the function you'll do it like so:
faqmodal($faq[$fisf][$title], $faq[$fisf][$image], $faq[$fisf][$title]);
And the data you pass will vary according to the data you get via the foreach loop. If this isn't what you mean, please let me know via a comment and we'll work our way through this.

display none if php function is empty

I have a wordpress function that displays adverts every so often. When are not shown essentially I would prefer to the div to display:none;
I can not seem to figure out the correct PHP function in order for the div not to display when a advert is uploaded.
<div class="advert" <?php if(!empty($_GET['details'])) {echo "style='display: none'";} ?>></div>
Why not completely not echo "advert" element?
if ( !empty($_GET['details']) ){
echo '<div class="advert">add text</div>';
}
if you really want to just hide, you can assign hide class
<div class="advert <?php echo ( empty($_GET['details'])? 'hide' : '' );">add text</div>
then you would need to add "hide" class with display:none in your style.css
Above is shorthand/ternary if/else statement used, its great if you need output some string.
And, please don't output/trust any user input i.e. $_GET['details'] 'as is' anywhere without escaping it, for security reasons.
Wordpress have plenty easy-to-use escape functions, like esc_attr() and esc_html().
This should do it for you
<?php
$advert_display_string = "";
if (!isset($_GET['details'])) {
$advert_display_string = "style='display: none'";
}
?>
<div class="advert" <?php echo $advert_display_string ; ?> ></div>`
but having said that, instead of displaying it and then hiding it with css, you could just choose only to display it if you need it there, like below
<?php
if (isset($_GET['details'])) {
?>
<div class="advert"></div>
<?
}
?>

PHP: How can I put HTML Code with PHP Code within into a PHP Variable?

I have a question about " and ' in PHP. I have to put a complete <li> element into a PHP variable but this doesn't work, the output is completely false...
$list =
"
<li class=\"<?php if($info[1] < 700) {echo \"half\";} else {echo \"full\";} ?>\">
<div class=\"onet-display\">
<?php if ($image = $post->get('image.src')): ?>
<a class=\"onet-display-block\" href=\"<?= $view->url('#blog/id', ['id' => $post->id]) ?>\"><img class=\"onet-thumb\" src=\"<?= $image ?>\" alt=\"<?= $post->get('image.alt') ?>\"></a>
<?php endif ?>
<h1 class=\"onet-thumb-title\"><?= $post->title ?></h1>
<div class=\"uk-margin\"><?= $post->excerpt ?: $post->content ?></div>
</div>
</li>
";
Is it because there is PHP Content in the HTML Code? How can I solve this?
Can someone help me and explain why this doesn't work?
<?php ... <?php
Since your string contains PHP tags, I suppose you expect them to be evaluated. The opening PHP tag within another PHP tag is interpreted as a part of the PHP code. For example, the following outputs <?php echo time();:
<?php echo "<?php echo time();";
There are several ways to build a PHP string from PHP expressions.
Concatenation
You can create functions returning strings and concatenate the calls to them with other strings:
function some_function() {
return time();
}
$html = "<li " . some_function() . ">";
or use sprintf:
$html = sprintf('<li %s>', some_function());
eval
Another way is to use eval, but I wouldn't recommend it as it allows execution of arbitrary PHP code and may cause unexpected behavior.
Output Buffering
If you are running PHP as a template engine, you can use the output control functions, e.g.:
<?php ob_start(); ?>
<li data-time="<?= time() ?>"> ...</li>
<?php
$html = ob_get_contents();
ob_end_clean();
echo $html;
Output
<li data-time="1483433793"> ...</li>
Here Document Syntax
If, however, the string is supposed to be assigned as is, use the here document syntax:
$html = <<<'HTML'
<li data-time="{$variable_will_NOT_be_parsed}">...</li>
HTML;
or
$html = <<<HTML
<li data-time="{$variable_WILL_be_parsed}">...</li>
HTML;
You want to store some html into a variable.
Your source should (if not yet) start with
<?php
Then you start building the contents of $list.
Starting from your code the nearest fix is to build $list by appending strings:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "\"half\""; // RESULT: <li class="half"
}
else
{
$list .= "\"full\""; // RESULT: <li class="full"
}
// ...and so on...
Now a couple things to note:
$list .= "... is a short form of $list = $list . "...
Where the . dot operator joins two strings.
Second thing you may make code easier to read by mixing single and double quotes:
Use double quotes in PHP and single quotes in the generated HTML:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "'half'"; // RESULT: <li class='half'
}
else
{
$list .= "'full'"; // RESULT: <li class='full'
}
// ...and so on...
This way you don't need to escape every double quote
i think you have to work like this
$test = "PHP Text";
$list = "<strong>here ist Html</strong>" . $test . "<br />";
echo $list;

Special chars does not convert to html entities (html_entity_decode not working)

I'm trying to encode special entities with a function, here what I'm trying to do:
its a ratable function, with ratables stars, i have two functions that displays a ratable message, one is displaying default results, and the second when i give it a new rate
function showStars($ratableKey) {
...
$textDesc = "<div id=\"rabidRating-$ratingId-description\" class=\"ratingText\">"
.$this->getStarMessage($rating)."</div>";
echo $textDesc
}
i am call the getStarMessage in two different ways
function getStarMessage($rating) {
$stars = $this->percentToStars($rating['rating']);
if ($rating[totalRatings] > 1) $s = "s";
$div_stars = "<div class=\"rate\">";
$result= "$stars/$this->stars ".$div_stars." ($rating[totalRatings] avis)</div>";
$result= html_entity_decode($result);
return $result;
}
In the defaut way (in showStars function ) it works fine (with or without the html_entity_decode)
but when I called the getStarMessage function in another function
function doVote($ratableId, $percent) {
...
$rating = $this->loadRating($id);
$return = $this->getStarMessage($rating);
echo $return; ==> the problem is here
}
in source code it displays
<div class="ratingText " id="rabidRating-9-description">2.8/5 <div class="rate"> (141 avis)</div></div>
and therefore, the output is this
2.8/5 <div class="rate"> (142 Stars)</div>
It's not displaying as html entities but as simple string.
Am I doing something wrong ?
Here some screenshot :
displaying default :
After rating (clik on stars)
You want that div.rate to be rendered as HTML? if so, then you shouldn't put the "&lt" etc entities -> that makes the exact opposite from what you want - it keeps the "less than" character in the string.
So, if I understood you well, your code has to be
<div class="ratingText " id="rabidRating-9-description">2.8/5 <div class="rate"> (141 avis)</div></div>
Good luck.

Set two classes from array

I want to change the classes on every third output from a query:
<?php
$style_classes = array('box','box','box no_right_margin');
$style_index = 0;
?>
I set this on the div:
<div <?php $k = $style_index%4; echo "class=$style_classes[$k]"; $style_index++; ?>>
On the third div I want the class to look like this:
<div class="box no_right_margin">
Right now it looks like:
<div class="box" no_right_margin>
You need to enclose the class names in quotes. Your script is actually outputting class=box no_right_margin. (I think the example you gave as the current output is not what the script is sending, but the view of the DOM from something like Firebug, which is showing the browser as only seeing the first class in the list)
So you could do this:
<div class="<?php $k = $style_index%4; echo $style_classes[$k]; $style_index++; ?>">
or even
<div class="<?php echo $style_classes[$style_index++ % 4]; ?>">
You should use %3 instead of %4, so you actually get indexes 0, 1 and 2.
And you need correct quotes in your HTML output:
<?php echo '<div class="' . $style_classes[$k++ % 3] . '">'; ?>
Else your browser (Safari?) would probably correct it with a " at the wrong place, as shown in your example. Btw, it's better style to use hyphens for CSS class names, not underscores (unlike IDs).

Categories