I have a question about using variables inside a function. The first part of code is not working. It appears that the variable '$myvalue' is not being served to the function although when I echo the $myvalue it has the right text.

If I however use normal text, it WORKS!
How to do this dynamically so that the varibale CAN be used?

First code is here and is NOT working!

<?php 
$myregions = array('delaware','florida','colorado');
foreach ($myregions as $myvalue){
osc_query_item(array('region'=>$myvalue));
    if ( osc_has_custom_items() && osc_item_region()==$myvalue) {
        bender_draw_item($class);
    } 
}
?>

Second code is here and IS working!

<?php 
osc_query_item(array('region'=>'Delaware'));
    if ( osc_has_custom_items() && osc_item_region()=='Delaware') {
        bender_draw_item($class);
    } 

    osc_query_item(array('region'=>'Florida'));
    if ( osc_has_custom_items() && osc_item_region()=='Florida')  {
        bender_draw_item($class);
    } 

    osc_query_item(array('region'=>'Colorado'));
    if ( osc_has_custom_items() && osc_item_region()=='Colorado') {
        bender_draw_item($class);
    } 

   ?>

The function is here (I took some of the 'case' out because readability:

function osc_query_item($params = null) {
    $mSearch = Search::newInstance();
    if($params==null) {
        $params = array();
    } else if(is_string($params)){
        $keyvalue = explode("=", $params);
        $params = array($keyvalue[0] => $keyvalue[1]);
    }
    foreach($params as $key => $value) {
        switch($key) {
        case 'region':
            case 'region_name':
                $tmp = explode(",", $value);
                foreach($tmp as $t) {
                    $mSearch->addRegion($t);
                }
                break;

            case 'city':
            case 'city_name':
                $tmp = explode(",", $value);
                foreach($tmp as $t) {
                    $mSearch->addCity($t);
                }
                break;
                }
    }
    View::newInstance()->_exportVariableToView("customItems", $mSearch->doSearch());
}

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

You tried trimming it. It might have some white space around it. The only other thing I noticed that was different is you have capitals for 'D' delaware but not in the array example...

Both seems same except for capital latters in second example.In second one you are taking first letters as capital but not in previous example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.