Jump to content

Block PHP query


Ocean West

Recommended Posts

Posted

what do i need to change in this 3x code that worked as a custom block but currently doesn't work in 4x

$this->DB->build( array(
	'select' => 'ad_id',
	'from'   => 'nexus_ads',
	'where'  => 'ad_locations like "%fmf_square%"',
    'and' => 'ad_active=1',
	'order'  => 'rand()'
	)      );
$this->DB->execute();
$i=0;

while ( $row = $this->DB->fetch() )

{
    $ad_ids[] = $row['ad_id'];
}

foreach( $ad_ids as $ad_id ){
$i++;
   $str = file_get_contents( "{$this->settings['board_url']}/interface/advertisements.php?$ad_id" ) ;

$str = preg_replace('/\<[\/]{0,1}div[^\>]*\>/i', '', $str);

if($i==10) {
echo $str . "<br>";
} else {
echo $str . "  ";
}

}

 

Posted
	$i = 0;
	foreach ( \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'core_advertisements', 'ad_location LIKE "%fmf_square%" AND ad_active=1' ), 'IPS\core\Advertisement' ) as $ad )
	{
		$i++;
		$str = preg_replace( '/\<[\/]{0,1}div[^\>]*\>/i', '', $ad );
		if( $i == 10 )
		{
			echo $str . "<br>";
		}
		else
		{
			echo $str . "  ";
		}
	}

 

Posted

I am confused why is it now pulling from core advertisement and not from nexus_ads  why is there two tables for ads?

( i tried the above and it didn't work) 

Also I don't see in the code how it will randomize the results?

Posted

It's just core_advertisements now. We moved the feature out of Commerce and into the core.

Sorry I missed out the rand() order by clause. Add it as the 4th parameter to the select() method.

  • 2 weeks later...
Posted

Ok i tried this code and multiple variations but nothing will display...

my goal is every active square ad (with link) in random order

[ ad01 ] [ad02] [ ad02 ] [ad03] [ ad04 ] [ad05] [ ad06 ] [ad07] [ ad08 ] [ad09] [ ad10 ] <br>
[ ad12 ] [ad12] [ ad12 ] [ad13] [ ad14 ] [ad15] [ ad16 ] [ad17] [ ad18 ] [ad19] [ ad20 ]

My old code produced this http://fmforums.com/ad_test.html which was used in a block that cached every few minutes which i then used with the external API - ( i know that is not ready yet - but still need to restore the adds to the site footer )

  • 1 month later...
Posted

55 Days later and I still can't figure this out -

I have copied and pasted the code that @Mark posted but that doesn't do anything. 

I have tried to paste it into a block or even on a page, but I can't figure out the proper syntax. 

 

 

Posted

55 Days later and I still can't figure this out -

I have copied and pasted the code that @Mark posted but that doesn't do anything. 

I have tried to paste it into a block or even on a page, but I can't figure out the proper syntax. 

foreach ( \IPS\Patterns\ActiveRecordIterator

change to 

foreach ( new \IPS\Patterns\ActiveRecordIterator

 

Posted

.

Which one did you choose ? The correct one ?

 

2015-04-25_12-06-14.thumb.png.523a0e9ac9

 

$i = 0;
	foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'core_advertisements', 'ad_active=1' ), 'IPS\core\Advertisement' ) as $ad )
	{
		$i++;
		$str = preg_replace( '/\<[\/]{0,1}div[^\>]*\>/i', '', $ad );
		if( $i == 10 )
		{
			echo $str . "<br>";
		}
		else
		{
			echo $str . "  ";
		}
	}

 

Try this code first without the LIKE MySQL Statement .. it is working for me, tested it ..

 

 

 

.

Posted

Wow awesome - this is showing promise!  

	foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'core_advertisements', 'ad_active=1 AND ad_location=\'fmf_square\'' ), 'IPS\core\Advertisement' ) as $ad )

the WHERE clause is in single quotes and that the text string i am searching on has to be escaped and quoted.

so i have it rendering now but its one long list - need to strip out the <div><ul><li> tags...

 

 

Posted

Wow awesome - this is showing promise!  

	foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'core_advertisements', 'ad_active=1 AND ad_location=\'fmf_square\'' ), 'IPS\core\Advertisement' ) as $ad )

the WHERE clause is in single quotes and that the text string i am searching on has to be escaped and quoted.

so i have it rendering now but its one long list - need to strip out the <div><ul><li> tags...

 

 

​.

So you are using the LIKE too ?

Think you will have to play a bit with those tags what fit the best ..

 

.

Posted

.

If your ad_;location in your ads are all set with the same name fmf_square .. can't you not just use   'ad_location="fmf_square" and ad_active=1'   (even with double quotes, it does work for me) .. those slashes before single quote should be fine too I guess ..

 

.

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...