function get_by_catid($catid, $number = 10, $random) { global $CONFIG; global $base_query; $in = " AND pid IN "; $query = $base_query . " and (category={$catid} OR category in (SELECT cid from " . $CONFIG['TABLE_CATEGORIES'] . " WHERE parent=" . $catid . " ))"; $order = " ORDER BY ctime desc"; if ($random) { $limit = " LIMIT 1000"; } else { $limit .= " LIMIT {$number}"; } //logg("\n5:".$query); $result = cpg_db_query($query . $order . $limit); if ($random) { $in .= randomizer($result, $number); // die ($query.$in.$order); $result = cpg_db_query($query . $in . $order); } return cpg_db_fetch_rowset($result); }
function randomizer($max = 10, $type = 'both') { // key list switch ($type) { case 'int': $key = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); break; case 'chr': $key = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); break; case 'both': default: $key = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); break; } // default data $result = ''; // generate key for ($i = 1; $i <= $max; $i++) { $random = rand(0, count($key) - 1); $result .= $key[$random]; } // re-check string lens if (strlen($result) != $max) { $result = randomizer($max, $type); } // return data return $result; }