Define PHP variable from a seperate API if statement

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.

1 Answer
1

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.

Leave a Comment