0

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);
?>        

Edited by mjsmitten

3
Contributors
3
Replies
18
Views
4 Years
Discussion Span
Last Post by diafol
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.