开发者

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

开发者 https://www.devze.com 2023-03-30 14:39 出处:网络
I\'m using the following preg_match: preg_match( \'!<div class=\"thumblock \">(.*)</div>!si\' , wp_gdsr_render_article_thumbs(0, false, \"\", 0, \"\", false) , $n );

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 ($开发者_Python百科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);?>">
0

精彩评论

暂无评论...
验证码 换一张
取 消