0

I am trying to put together a function that will echo certain text based on a range of numbers. One of number(s) I am trying to compare against is echoed in my template as such --

<?php if ( class_exists( 'MRP_Multi_Rating_API' ) ) {
    MRP_Multi_Rating_API::display_rating_result(array(
    'rating_item_ids' => 1,
    'show_count' => false,
    'echo' => true
) );
} ?>

And the code Im using to check against the number ranges is --

<?php
//Value Rating
$num = '43';
$value1 = '1';
$value2 = '20';
$value3 = '21';
$value4 = '40';
$value5 = '41';
$value6 = '60';
$value7 = '61';
$value8 = '80';
$value9 = '81';
$value10 = '100';
switch ($num){
        case ($num>= $value1 && $num<= $value2): 
            echo "within range 1";
        break;
        case ($num>= $value3 && $num<= $value4): 
            echo "within range 2";
        break;
case ($num>= $value5 && $num<= $value6): 
            echo "within range 3";
        break;
case ($num>= $value7 && $num<= $value8): 
            echo "within range 4";
        break;
case ($num>= $value9 && $num<= $value10): 
            echo "within range 5";
        break;
        default:
            echo "within no range";
        break;
     } ?>

So what I am trying to do is get the output from the first function and assign it to the $num variable to be used in my second function. How do I do this?

Also since its wordpress, I would like to know how to make the above answer into a function for functions.php so I can then call it from within any of my theme templates such as -- <?php echo $function(); ?> ??

Any help is appreciated. Thanks.

6
  • I'll guess the key would be to change the echo argument so it returns the value.
    – Milo
    Commented Oct 16, 2015 at 20:26
  • @Milo i dont understand what you mean?
    – 730wavy
    Commented Oct 16, 2015 at 20:57
  • you can assign the result of display_rating_result to a var by setting echo to false, so its value is returned, not echoed.
    – Milo
    Commented Oct 17, 2015 at 4:21
  • Ok but then how do I call it in the other php function? @Milo
    – 730wavy
    Commented Oct 17, 2015 at 16:04
  • the same way you do in the template, move the function call inside your function, or pass the result as a function argument.
    – Milo
    Commented Oct 17, 2015 at 16:12

1 Answer 1

2

Hi I'm the author of Multi Rating Pro. The simplest way to do this is to put this logic within the rating-result.php template file.

You can call the API function passing in your own result_type and then check this in the template.

if ( class_exists( 'MRP_Multi_Rating_API' ) ) {
    MRP_Multi_Rating_API::display_rating_result( array(
            'rating_item_ids' => 1,
            'show_count' => false,
            'result_type' => 'my_result_type'
    ) );
}

and then in rating-result.php template file, add the following as required:

if ( $result_type == 'my_result_type' ) {       

    $score_result = $rating_result['adjusted_score_result'];

    $value1 = 1;
    $value2 = 20;
    $value3 = 21;
    $value4 = 40;
    $value5 = 41;
    $value6 = 60;
    $value7 = 61;
    $value8 = 80;
    $value9 = 81;
    $value10 = 100;

    switch ( $score_result ){
        case ( $score_result >= $value1 && $score_result <= $value2 ):
            echo "within range 1";
            break;
        case ( $score_result >= $value3 && $score_result <= $value4 ):
            echo "within range 2";
            break;
        case ( $score_result >= $value5 && $score_result <= $value6 ):
            echo "within range 3";
            break;
        case ( $score_result >= $value7 && $score_result <= $value8 ):
            echo "within range 4";
            break;
        case ( $score_result >= $value9 && $score_result <= $value10 ):
            echo "within range 5";
            break;
        default:
            echo "within no range";
            break;
    }       
}

Hope this helps.

1
  • Yes! This seems to do the trick. Only thing is I had to change this -- ['adjusted_score_result'] to ['adjusted_percentage_result'] -- since the numbers compared are 1-100. Thanks for the help!
    – 730wavy
    Commented Oct 19, 2015 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.