This topic is locked

Display random images stored in database

1/6/2007 12:51:21 PM
PHPRunner General questions
D
Dale author

I have a table that has a field that contains images. Logos for customers.
I would like to be able to randomly place a logo of one of our customers in the top right corner of my supertop.php
The gifs are rougly all the same size. Everytime the page loads, I would like a different logo to appear.

Of course it would be nice to have the logo be a link. The www address for the customer is in the same table as the image.
Has anyone worked this one out using phprunner or come accross anything similar.

Any help to point me in the right direction would be graciously appreciated.

D
deanmeistr 1/6/2007

I have exactly the same kind of question; I'm just not clear on how to pull images from the MEDIUMBLOG, particularly reading through the Smarty templates to see what's calling the GD Image Magik library functions.

D
Dale author 1/6/2007

You bring up a good point regarding Smarty. I am still in build 3.0. To much customizing to bring over to the new version in the time I have to get this app into beta.
So maybe we can both get our answers or tips from support with regards to the different versions.

Sergey Kornilov admin 1/7/2007

In header.php the following code can be used to display images. Replace variables in bold with actual field names and values.

echo "<img border=0 src='TableName_imager.php?field=PictureField&key1=KeyColumnValue'>";


To display random image you need to following

  1. get the number of records (Total)
  2. find a random number that is less than total number of records (Rand)
  3. run a query

    select id from tablename order by id limit 0, Rand
  4. Proceed to the last record in this recordset and get ID of that record.
  5. Use this ID to display image.

D
Dale author 1/7/2007

Thanks Sergey,
I think I can take it from here. I will share my result in this post when I get it working.

M
mmponline 4/26/2008

DaleM
Have you finalised this script. I'd like ot see as I nquired about this and other features in a new post.

D
Dale author 4/27/2008

Stephan,

Actually I have successfully been running banner adds on my phprunner sites. I never did get the random feature going, I just simply start with the first image and work my way through with each refresh. A couple of other fields you may want to consider is a sort field and a weight field in your banner table. The sort can be used to order the banners for display and the weight would be good so you could define a routine to set a banner to show every fifth time, for example.
BUT, all my work was done using phprunner 3.0 and will be pretty useless for the new version.
What I did was created a file get_banners_2w.php ( 2 banners width wise )
Code as below

=========================

<?php // start of file ?>

<?php $debug=0; ?>

<table width="100%" cellpadding="0px" cellspacing="0px" border="0px">

<tr>

<?php

global $conn;

$go_url_title = '';

$go_url = '';

$ctr = 0;

$banner_ctr =0;

$strSQL1 = "select `purchase_id`, `customer_id`, `group`, `inventory_id`, `banner`, `banner_url` From `purchases` where ((`purchases`.`group` = 'Advertising') AND (`purchases`.`purchase_date` <= now()) AND (`purchases`.`product_id` != 1) AND (`purchases`.`purchase_id` < '".$_SESSION["last_side_banner"]."')) order by `purchase_id` DESC limit 2";

$rsSQL1 = db_query($strSQL1,$conn);

$done ="no";

while($data1=db_fetch_array($rsSQL1))

{

$_SESSION["last_side_banner"] = $data1["purchase_id"];

$ctr += 1;

$banner_ctr +=1;

$align_gif = "align=\"left\"";

if ($ctr <=2)

{

if ($ctr > 1) $align_gif = "align=\"right\"";

$i=70;

$value=$data1["banner"];

if ($data1["banner_url"])

{

$go_url = "window.open('http://".$data1["banner_url"]."';)";

$go_url_title ="Visit this advertisers web site at ".$data1["banner_url"]."&nbsp;(Opens&nbsp;in&nbsp;a&nbsp;new&nbsp;window)";

}

else

{

$go_url = "document.location.href='advertisers_rates.php'";

$go_url_title ="Get the information about Advertising in the Yellow Sheet";

}

if($debug == 1)

{

$go_url_title = "Debug Advertiser ".$data1["banner_url"]." Invoice: ".$data1["purchase_id"];

}

echo "\r\n<td colspan=\"2\" valign=\"bottom\" width=\"50%\" ".$align_gif." ><table cellpadding=\"0px\" cellspacing=\"0px\" border=\"0px\"><tr><td>";

$key = $data1["purchase_id"];

$iquery="field=".$i."&amp;key=".htmlspecialchars($key);

$disp='<img border="0" src="get_side_banners_imager.php?'.$iquery.'" onclick="'.$go_url.'" title="'.$go_url_title.'" alt="'.$go_url_title.'" />';

echo $disp;

echo "\r\n</td></tr></table></td>";

$done ="no";

}

}

if ($banner_ctr == 0)

{

$_SESSION["last_side_banner"] = 999999;

$go_url = "document.location.href='advertisers_rates.php'";

if($debug == 1)

{

$go_url_title ="Debug Mode First Banner holder.";

}

else

{

$go_url_title ="Get the information about Advertising in the Yellow Sheet.";

}

echo "<td valign=\"bottom\" width=\"50%\" align=\"left\"><table cellpadding=\"0px\" cellspacing=\"0px\" border=\"0px\"><tr><td>";

echo "<img src=\"images/no_logo_1.gif\" border=\"0px\" onclick=\"".$go_url."\" title=\"".$go_url_title."\" alt=\"".$go_url_title."\" />";

echo "</td></tr></table></td>";

$banner_ctr = 1;

}

if ($banner_ctr <= 1)

{

$_SESSION["last_side_banner"] = 999999;

$go_url = "document.location.href='advertisers_rates.php'";

if($debug == 1)

{

$go_url_title ="Debug Mode Last Banner holder.";

}

else

{

$go_url_title ="Get the information about Advertising in the Yellow Sheet.";

}

echo "<td valign=\"bottom\" width=\"50%\" align=\"right\"><table cellpadding=\"0px\" cellspacing=\"0px\" border=\"0px\"><tr><td>";

echo "<img src=\"images/no_logo_2.gif\" border=\"0px\" onclick=\"".$go_url."\" title=\"".$go_url_title."\" alt=\"".$go_url_title."\" />";

echo "</td></tr></table></td>";

}

?>

</tr>

</table>

<?php // end of file ?>

=================
Then in my supertop.php I inserted the snippet below.

=================

<table style="border: 0px solid orange; padding:0px; margin:0px auto; width:100%;" cellspacing="0px">

<tr>

<td>

<?php

if(file_exists("get_banners_2w.php"))

{ include("get_banners_2w.php");}

?>

</td>

</tr>

</table>

=================
Finally in my _list.php template, I just added the snippet below to call the file.

=================

<?php

if($_SESSION["GroupID"] == 'Customer' && file_exists("include/supertop_customer_print.php"))

include("include/supertop_customer_print.php");

?>

=================
My goal was to get banner advertising happening on all my Customer views and print pages. This works quite nicely and is all generated with PHPRunner 3.0 when I hit the build button. If I get time, I would indeed like to add random and weights to the function, but time is just to short right now.
I hope this gives you an idea or a helpfull hint but again its all using the old version of PHPRunner. When the smarty template method came into play, there was too much code to modify for me to upgrade so I have just stuck with the old version until I can get time to build a new project with the new stuff.
Im sure support will suggest a more elegant way and hopefully even include a banner ad feature in future releases.
A couple of notes, I was using ajax at first to get the banner ads going, but it was acutally a larger hit on the bandwidth and a few older browser incompatibilities that pulled me back to just using phprunner templates and code to make it work. And it works quite nicely. Second, all my clicks on the banners, open the advertiser in a new or blank window. I use SSL on my sites and opening a page that is not secure, from a secure web page brings up to many warnings that only confuse the user.
Hope this helps somewhat. You can actually go to www.yellowsheet.ca and see all this in action. THe pages you see before you log in have banner ads displaying, they are displayed 2 deep in the right hand side. Once inside as a customer, you will see the banner ads 2 wide across the top of any print outs and reports.