I build a code to eventually get a random item per category. Functions are working. I am just curious if the WHILE and IF are accurate. I am not sure if the first two unset() is good or do I need to drop those? And more important WHY the random ALWAYS give me the same result first result as if $myitems holds only one???

<?php

// loop from array //
foreach ($mycats as $catvalue) {

// get my items //
query_item(array('category_name'=>$catvalue));

// check for items and has category name //
while ( has_items() && item_category()==$catvalue) {

    // store items in array by title //
    $myitems[] = item_title() ;

    // make the array random //
    $selected = array_rand(array_flip($myitems));

    // request items again but with random title //
    query_item(array('category_name'=>$catvalue));
    while( has_items() && ( item_title()==$selected)){ 

        // display my item //
        draw_item($class);
    }

    // unset varible arrays but hold catvalue //
    unset($myitems);
    unset($selected);
}
}   

// unset all variable arrays //
unset($myitems);
unset($selected);
unset($catvalue);
?>        
Member Avatar for iamthwee

Is this wordpress by any chance?

no wordpress

Member Avatar for diafol

Seems to be a lot of unnecessary code in the loop. I think. ALso you have loads of user-defined functions that could be causing errors - we have no way of knowing.

$class seems to be holding the variable that decides on the output, but nowhere is this set in your snippet.

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.