PHP variables built out of string and vars

I have a need to build a dynamic query that requires a variable to structured like so;

$my_var = $var_1.'_'.$var_2.'_band_'.$var_3;

$my_var is then used within a loop.

Read More

While I can echo the $my_var data successfully, this does not work in my loop. Is this even a possibility?

Here is the actual loop. $my_var would be used as a lookup for a column title. The vars used in $my_var would be strings.

global $wpdb;

    $my_var = $var_1.'_'.$var_2.'_band_'.$var_3;

    $results = $wpdb->get_results ( "SELECT * FROM quote WHERE Postcode_area LIKE '%$find%'" );

    foreach ( $results as $result ) {
        $savings = $result->$my_var * 52;
    }

SOLVED

Thank you everyone for your input, I’m sorry to say I was being a complete idiot! The column I was using to lookup data as a test did not required one additional VAR. Truly sorry to waste anyones time!

The loop is now working without issue.

Related posts

1 comment

Comments are closed.