通过数组的横幅的PHP旋转

I got 20 flash banners, 10 armenian ones, 10 english ones.

$query="SELECT* FROM `reklam` LIMIT 5";
        $result=mysql_query($query);
        while($row=mysql_fetch_array($result))
        {

        if($_SESSION['lang']=='arm')
        {
            $swf_name=$row['swf_arm'];
        }
        else
        {
            $swf_name=$row['swf_eng'];
        }
echo'<tr>
        <td>
        <div style="text-align: center">
            <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="290" height="299" align="middle">
                <param name="movie" value="reklam/'.$swf_name.'.swf" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#ffffff" />
                <param name="play" value="true" />
                <param name="loop" value="true" />
                <param name="wmode" value="transparent" />
                <param name="scale" value="showall" />
                <param name="menu" value="true" />
                <param name="devicefont" value="false" />
                <param name="salign" value="" />
                <param name="allowScriptAccess" value="sameDomain" />
                <!--[if !IE]>-->
                <object type="application/x-shockwave-flash" data="reklam/'.$swf_name.'.swf" width="290" height="299">
                    <param name="movie" value="reklam/'.$swf_name.'.swf" />
                    <param name="quality" value="high" />
                    <param name="bgcolor" value="#ffffff" />
                    <param name="play" value="true" />
                    <param name="loop" value="true" />
                    <param name="wmode" value="transparent" />
                    <param name="scale" value="showall" />
                    <param name="menu" value="true" />
                    <param name="devicefont" value="false" />
                    <param name="salign" value="" />
                    <param name="allowScriptAccess" value="sameDomain" />
                <!--<![endif]-->
                    <a href="http://www.adobe.com/go/getflash">
                        <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
                    </a>
                <!--[if !IE]>-->
                </object>
                <!--<![endif]-->
            </object>
        </div>
        </td>
        </tr>';
        }

The name of the banners are coming from database. If site language is set on armenian, it should appear armenian banners, if english - english one (session). As i said there is 10 banners, but i need to show 5 at once, so i included in query LIMIT 5. Now the task is, i need to do rotation of that banners. I mean when i refresh the page, it should choose banners randomly. How should i do it?

Thanks!

You can use MYSQL RAND()

$query="SELECT* FROM `reklam` ORDER BY RAND() LIMIT 5";

There are a whole bunch of ways to do this, but given that there's only a small number of records, you don't need to do anything too clever.

The easy options for you are:

  • Use ORDER BY RAND() in the SQL query.

  • Just load all the records, use PHP's shuffle() function to randomly re-order the array, and then just show the first five from the array.

To be honest, with the number of records you're talking about here, it won't make a huge amount of difference. For larger databases, neither of these options is a good idea, for performance reasons, but for tables as small as the one you've got both are perfectly fine.

You can add 1 field at your Table called - views (int)

To pick the banner with the less displays and after that run 1 update:

$sql = "SELECT * FROM `reklam` ORDER BY `views` ASC LIMIT 5";
$sql = "UPDATE `reklam` SET `views`=`views`+1 WHERE ID IN(IDS FROM SELECT)";

This way you will get and statistics how much displays your ads have..