p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Help!! - Fixed


Message #1 by WH Micro <wh_micro@y...> on Thu, 30 May 2002 07:53:58 -0700 (PDT)
Fixed the problem I was having with my script. Felt
like sharing the solution with the list:

$text_path ='NULL';
$text = array( 0,0,0,0,0 );
$text_ads_count = 5
$current = 1;

This function will be called 4 or five times per page
like this:
<tr>
<td>
<?
	get_text_ad( $current++ );
	include( $text_path );
?>
</td>
</tr>


function get_random_number( $m, $l )

{	
	srand((double) microtime() * 1000000);
	return rand( $m, $l );		
}
		

		
function get_text_ad( $cur ){
		
// declare global variables that will be used in the
function
		global $text;
		
		// declare local variable
		$useit = 'no';
		
		// set the current table to be used
		$table_name = 'ads_text';
		$update_field = 'presence';
		
		// if is the first ad displayed on the page; go
ahead and use it
		if( $cur == 1 )
		{   // set the id from a random generated number
			$id = get_random_number( 1,
$GLOBALS['text_ads_count'] );
			
			// Based on this number select the record from the
db
			$sql = "select path from $table_name where
ad_id=$id";
			
			$result = runQuery( $sql, "-- Select from
$table_name --"  );
			
			// set the path to the loaded path
			list( $GLOBALS['text_path'] ) = mysql_fetch_array(
$result );
			
			// set the current posistion in the array with the
selected id
			$text[$cur] = $id; 
					
			update_table( $table_name, $id, $update_field );			
		}// end if first occurrance
		else
		{
			while( $useit == 'no' ){
				$useit = checkit( $id = rand( 1,
$GLOBALS['text_ads_count'] ),
$GLOBALS['text_ads_count'] );
				
				if( $useit == 'yes' ){
					// Based on this number select the record from
the db
					$sql = "select path from $table_name where
ad_id=$id";
				
					$result = runQuery( $sql, "-- Select from
$table_name --" );
				
					// set the path to the loaded path
					list( $GLOBALS['text_path'] ) 
mysql_fetch_array( $result );
					
					// set the current posistion in the array with
the selected id
					$text[$cur] = $id;	
							
					update_table( $table_name, $id, $update_field );
				
				}// end if useit or not
				else
					continue; // continue looping
				
			}// end while loop
		}//end else
	}// end function get_text********************
	

	function checkit( $randomnumber, $count ){
		// set local variables with global variables' data
		$t = $GLOBALS['text'];
			
		for( $i = 1; $i <= $count; $i++ )
		{
			if( $t[$i] == $randomnumber )
				return 'no'; // don't use the number/id
		}// end for loop
		return 'yes';	 // use the number/id
	}//end function checkit**********

=====
www.whmicro.com

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com

  Return to Index