I am inside a while loop.
Different categories are displayed with their name and id in a list.
Then I need to query each category and get a item title from there.
This query is not working although the function osc_category_id/name is changing accordingly.
Or am I doing it wrong?

   while (osc_has_categories ());

   // check category id and name inside while loop is ok!
   echo osc_category_id(), ": ", osc_category_name() ;

   echo "<p>results:</p>" ;

   // query_items inside the while loop after category is changed.
   // note: "studio" -             show results from "studio" BUT everywhere, 
   // note: osc_category_name() -  show ALL results from ALL categories AND everywhere,
   // note: "$osc_category_name" - show ALL results from ALL categories AND everywhere,

   osc_query_item ("studio") ;

   // check if items present
   while ( osc_has_custom_items() ) { 

   // echo result
   echo osc_item_title () ; 
   } 

   // reset query
   osc_reset_custom_items () ;

Please help!

The first while loop has no code since you did not use any curly brackets. Is this suppose to be like that? Or would be more correct like this:

while (osc_has_categories ()) {

    // check category id and name inside while loop is ok!
    echo osc_category_id(), ": ", osc_category_name() ;

    echo "<p>results:</p>" ;

    // query_items inside the while loop after category is changed.
    // note: "studio" -             show results from "studio" BUT everywhere, 
    // note: osc_category_name() -  show ALL results from ALL categories AND everywhere,
    // note: "$osc_category_name" - show ALL results from ALL categories AND everywhere,

    osc_query_item ("studio") ;

    // check if items present
    while ( osc_has_custom_items() ) { 

        // echo result
        echo osc_item_title () ; 
    } 

    // reset query
    osc_reset_custom_items () ;
}

yes, forgot to paste it when I placed this code online.

I am having trouble with the function which I need:

query_item()

in relation to this function that has its value changed inside this while loop:

osc_category_name()

Now I have "studio" inside the first.
But, whenever a category change, I still get the same studio data, which I do not want.

When I echo the function osc_category_name() before or after the function query_item(), and inside the while loop, the name of the category change as it should.

So, I am guessing I need THAT output INSIDE the "".

Hope you understand.

Sory, but I do not quite understand. Please explain in more detail.

Do you have the code for query_item() and osc_category_name() functions? If yes, please post them.

The function osc_query_item() is as follows, the other one is way down below:)

*/
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 'author':
                $tmp = explode(",", $value);
                foreach($tmp as $t) {
                    $mSearch->fromUser($t);
                }
                break;

            case 'category':
            case 'category_name':
                $tmp = explode(",", $value);
                foreach($tmp as $t) {
                    $mSearch->addCategory($t);
                }
                break;

            case 'country':
            case 'country_name':
                $tmp = explode(",", $value);
                foreach($tmp as $t) {
                    $mSearch->addCountry($t);
                }
                break;

            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;

            case 'city_area':
            case 'city_area_name':
                $tmp = explode(",", $value);
                foreach($tmp as $t) {
                    $mSearch->addCityArea($t);
                }

            case 'results_per_page':
                $mSearch->set_rpp($value);
                break;

            case 'page':
                $mSearch->page($value);
                break;

            case 'offset':
                $mSearch->limit($value);
                break;

            default:
                osc_run_hook('custom_query', $key, $value);
                break;
        }
    }
    View::newInstance()->_exportVariableToView("customItems", $mSearch->doSearch());
}

The function osc_category_name() is as follows:

function osc_category_name($locale = "") {
    if ($locale == "") $locale = osc_current_user_locale() ;
    return osc_category_field("s_name", $locale) ;
}

I do not understand your code yet but one thing is strange. You call a foonction

osc_query_item ("studio");

and if you look at this code snippet of the osc_query_item function definition

if($params==null) {
    $params = array();
} else if(is_string($params)){
    $keyvalue = explode("=", $params);
    $params = array($keyvalue[0] => $keyvalue[1]);
}

it seems like if parameter is string it is expected to contain = character by which the string is exploded to an array. In the following bit of code:

$params = array($keyvalue[0] => $keyvalue[1]);

$keyvalue[1] is undefined (not set) since there was no = character in the string studio.

I do not know how much sense that makes since I do not know the purpose of the code and all the elements (like the Search class). Hope it helps.

Yes I know. It supposed to be like this example:

osc_query_item("category_name=value")

or

osc_query_item(array('category_name' => 'value','results_per_page' => '1'))

But I need to fill the value dynamicly, I know the names of the categories.
And the function osc_category_name() changes dynamicly inside the while loop.

How would you fill the values dinamically? From the user input, form the database or some other way?

The data I need is present in the database. The functions just get it from the database. Example:

//Category names: AAA, BBB and CCC. 
//Items in AAA: Item1, Item3.
//Items in BBB: Item2, Item4.
//The while loop is measured by a counter so only runs 3 times.

while (osc_has_categories()) { 
osc_count_categories();
echo osc_category_name(); 

// echo's category name
// now I need to get the items from that category with:

osc_query_item()
// some other code here, close WHILE.
}

So inside the loop, I echo osc_category_name() and it displays correct value at any place.
I want to collect that to use in the function osc_query_item which only accept "" or "" in arrays. Some examples I tried:

This works, but only for one category named Studio:

osc_query_item("category_name=studio");

Then I tried this, not sure, but threats the last function like empty "", so I get all data:

osc_query_item(osc_category_name());

Then I tried any of these two, works, but only on the first category. After that the $cat stays the same value:

$cat = osc_category_name(); osc_query_item($cat);
$cat = osc_query_item(array("category_name"=>$cat)) ;

First I thought it had to do with the loop. But after those two tryouts, when I

echo osc_category_name();

it give me correct value anyway. That is why I was thinking I could just echo the result from one function in the other function.

I was also looking at the PhP unset() but that only unset one time and consequently UPSET me!
I was also looking at the PhP call_user_func() but do not really understand.

Hope someone can be of assistence!

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.