[nycphp-talk] Help!! - Fixed
Jim Musil
jim at nettmedia.com
Thu May 30 12:03:48 EDT 2002
Good job on fixing the problem. It seems, however, that you could simplify
this whole process by doing something like the following.
1) Grab all the data in random order like so...
$query = mysql_query("SELECT path FROM ads_text ORDER BY RAND()");
2) Put it all in an array like so...
while( $data = mysql_fetch_object($query)) {
$paths[] = $data->path;
}
3) Write a function get_next() that pulls one out and deletes it from the
array...
function get_next()
{
global $paths;
$path = array_pop($paths);
return $path;
}
4) Then everytime you need it you write...
<td>
<?
include( get_next() );
?>
</td>
Jim Musil
On 5/30/02 10:41 AM, "jose sanchez" <j_r_sanchez at yahoo.com> wrote:
> I fixed my problem. The while loop I had before was
> messing things up. Here's the fixed problem:
>
> $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**********
>
>
>
> =====
> "An ounce of gold cannot buy an ounce of time."
> - Anonymous
>
>
> www.whmicro.com
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! - Official partner of 2002 FIFA World Cup
> http://fifaworldcup.yahoo.com
>
More information about the talk
mailing list