I have searced the forums and to my surprise I could not find anything on this subject.

I am looking to rotate flash swf banners when the page is loaded or refreshed. The values for the banner ads should come out of the database, not an array.

Can anyone give me some guidance? I am guessing that I would begin by using the mt_rand() function...?

    If the info you need is in a database, use '... ORDER BY RAND() LIMIT 1' in your query. This will randomly select one entry from your database.

      That's it! There are no paramets for the rand() function?

      Query:

      mysql_select_db($database_dbname, $dbname);
      $query_rsFlashRotate = "SELECT swfID,swfName,swfPath
      FROM swf_tbl
      ORDERBY RAND() LIMIT 1;"

      banner.inc.php:

      <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="800" height="38" id="tool_banner" align="middle">
                  <param name="allowScriptAccess" value="sameDomain" />
                  <param name="movie" value="<?php echo $row_rsFlashRotate['swfPath']; ?>" />
                  <param name="quality" value="high" />
                  <param name="bgcolor" value="#FFFFFF" />
                  <embed src="<?php echo $row_rsFlashRotate['swfPath']; ?>" quality="high" bgcolor="#FFFFFF" width="800" height="38" name="tool_banner" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
      </object>

      So I woud do something like that... right?

        Pretty much. But you left out a little.

        $result = mysql_query($query_rsFlashRotate) or die("Query failed: " . mysql_error());
        $row = mysql_fetch_assoc($result);

        Then where you have

        echo $row_rsFlashRotate['swfPath'];

        it would be

        echo $row['swfPath'];

        etc.

          That is just great! I can't believe it is that easy.

          One question: Is it a good idea to have the limit specified in the query or should that be scripted in common practice?

            If you're only wanting one row from your database you'll want to limit your query to return one row, otherwise your database is doing more work than it needs to, especially if there are hundreds (thousands?) of rows. If you don't limit the query the database will return ALL of them. Now, of course, the code will only accept the first row it returns.

              That is sweat!

              ORDERBY RAND() LIMIT is all SQL I presume.

              That spurns another question: Could I use the LIMIT if I would like to use next-n-last navigation (I can't remember exactly what that is called, but it is where the links for previous, next, first and last are available when appropriate)?

                If you're just needing one row at a time you'll probably be better off using the primary key of the table. If you're returning multiple results you'll probably want to use something like 'LIMIT $start, $perpage'.

                For example, if I wanted to display 10 per page, page 1 would be 'LIMIT 0, 10' page 2 would be 'LIMIT 10, 10', page 3 would be 'LIMIT 20, 10', etc...

                Do a search in this forum for pagination for more info.

                One other thing -- It's not ORDERBY it's ORDER BY

                  Thank you so much, you have been the most helpful person.

                  Thanks for the correction of ORDER BY. I also meant to say that you spawned another question, not spurned. hehe 🙂

                    Not a problem. I'm always willing to help someone who's willing to learn and who's shown that they've put some effort into trying to do it themselves. I've certainly been helped by others.

                      Write a Reply...