View Single Post
Old 01-15-2013, 08:35 PM   PM User | #1
Saber
New Coder

 
Join Date: Sep 2012
Posts: 34
Thanks: 9
Thanked 0 Times in 0 Posts
Saber is an unknown quantity at this point
Random function limited to the first 10 ID's of db, must take 10 random from entireDB

Hello, Coding Forums, i have problem with option in site.
User goes to the "10 Random Jokes" in the site. In the db there may be tens of thousands of Joke ID's, the user is seeing only the first 10 joke ID's cuz of the desc function of the script and they are mixed, but only the first ten. And if the user refreshes the page, he will see the first 10 jokes from DB again, only by different order again. What needs to happen is when the user goes to the "10 random jokes" he must see 10 random jokes from those tens of thousands Joke ID's and when refreshing the page again 10 random jokes from thousands, not the first 10 jokes only mixed differently, i beleave the function and the module is all here, but can anyone do this ?

Code:
<?
include ("config_file.php");
include(DIR_LNG.'top_random_jokes.php');
$display_nr = $display_nr_top_random_joke;
$type = TEXT_RANDOM_JOKES_ON." ";
$database_table_name1 = $bx_db_table_joke_categories;  
$database_table_name2 = $bx_db_table_jokes;            
$jtype = "random";

isset($HTTP_POST_VARS['cat_id']) ? $cat_id = $HTTP_POST_VARS['cat_id'] : (isset($HTTP_GET_VARS['cat_id']) ? $cat_id = $HTTP_GET_VARS['cat_id'] : "");

if(isset($cat_id) && $cat_id != '0')
	$condition = " where category_id='".$cat_id."' and validate='1' and slng='".$slng."' ORDER BY emailed_value desc, rating_value DESC limit 0, $display_nr";
else
	$condition = " where validate='1' and slng='".$slng."' ORDER BY emailed_value desc, rating_value DESC limit 0, $display_nr";

$SQL = "select * from $database_table_name2 ".$condition;

$result_array = generate_random_array($SQL, $display_nr, $mode);

$show_joke_categories="yes";
include (DIR_SERVER_ROOT."header.php");
include (DIR_FORMS."jokes_category_with_jokes_form.php");
include (DIR_SERVER_ROOT."footer.php");
?>
Code:
/**************************************************************
Random array for any $SQL, it'll return result array compatible step function
**************************************************************/
function generate_random_array($SQL, $nr_random=10 , &$mode)
{
	$random_row = 0;
	$array = null;
	$sel = bx_db_query($SQL); 
	SQL_CHECK(0,"SQL Error at ".__FILE__.":".(__LINE__-1));

	$count = bx_db_num_rows($sel);

	srand((double)microtime()*1000000); 
	if ($count!=0)
	{
		if ($count >= $nr_random)
			$nr_random = $nr_random;		
		else
			$nr_random = $count;
		
		for($i = 0; $i < $nr_random; $i++)
		{
			$random_row = @rand(0, ($count - 1));
			$exist = random_once($array, $random_row);
			if($exist != 1)
				$array[$i] = $random_row;
			else
				$i--;
		}
	}
	$i = $j = 0;   

	for ($i = 0; $i < sizeof($array) ; $i++)
	{
		$record=bx_db_data_seek($sel, $array[$i]);
		$result_ads=bx_db_fetch_array($sel);
		$result_array[$i] = $result_ads;
	}
	
	$mode = "random";
	return $result_array;
}


function string_break($text, $length, $symbol = "...")
{
	$length_text = strlen($text);
	$length_symbol = strlen($symbol);
	if ($length_text <= $length || $length_text <= $length_symbol || $length <= $length_symbol)
	{
		return($text);
	}
	else
	{
		if ((strrpos(substr($text, 0, $length - $length_symbol)," ") > strrpos(substr($text, 0, $length - $length_symbol),".")+25) && (strrpos(substr($text, 0, $length - $length_symbol)," ") < strrpos(substr($text, 0, $length - $length_symbol),",")+25)) {
			return(substr($text, 0, strrpos(substr($text, 0, $length - $length_symbol)," ")). $symbol);
		}
		else if (strrpos(substr($text, 0, $length - $length_symbol)," ") < strrpos(substr($text, 0, $length - $length_symbol),".")+25) {
			return(substr($text, 0, strrpos(substr($text, 0, $length - $length_symbol),".")). $symbol);
		}
		else if (strrpos(substr($text, 0, $length - $length_symbol)," ") < strrpos(substr($text, 0, $length - $length_symbol),",")+25) {
			return(substr($text, 0, strrpos(substr($text, 0, $length - $length_symbol),".")). $symbol);
		}
		else{
			return(substr($text, 0, strrpos(substr($text, 0, $length - $length_symbol)," ")). $symbol);
		}
	}
}

Last edited by Saber; 01-15-2013 at 09:14 PM..
Saber is offline   Reply With Quote