Web Analytics Made Easy -
StatCounter random images - CodingForum

Announcement

Collapse
No announcement yet.

random images

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • random images

    I couldn't find a decent tutorial, or one that worked for that matter on how to do this. I want to read a directory insted of having to mess with all this text file mess. An array might be good. I want these images to be linked so, it might have to be an array. Thanks to all.

    Stevie
    Last edited by SDP2006; Feb 28, 2004, 02:47 PM.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    shouldnt be too complex, though ive just been out and a bit tipsy so dont asume that this will work.

    PHP Code:
    <?php

    $imgs 
    = array();

    array_push($imgs, array(
    'image' =>'image1.gif'
    'link' => 'image1-link.html'
    'alt' => 'alt1'
    ));

    array_push($imgs, array(
    'image' => 'image2.gif'
    'link' => 'image2-link.html'
    'alt' => 'alt2'
    ));

    srand(microtime()*100000000);

    $random_data $imgs[(rand(0, (count($imgs)-1)))];

    echo 
    '<a href="'.$random_data['link'].'"><img src="'.$random_data['image'].'" style="border: 0px" alt="'.$random_data['alt'].'" /></a>';
    That should point you in the right direction... I hope.
    PHP Weekly - A PHP Developers Resource
    PHP 5.1.4 and Ruby on Rails web hosting
    Moderator of PHP and Work offers and Requests
    Install Apache/PHP/MySQL
    (by marek_mar) | TinyPlugin Architecture

    Comment


    • #3
      Thanks, but that could get very lengthy. Is there any way to shorten it?
      Stevie Peele
      Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
      #dev - any programming,etc. question
      #design - design discussion and critque
      #central - general chat
      Come join us!

      Comment


      • #4
        oh sorry, missed the directory reading part

        ok, well if you want each one to be linked then you will need some form of array or textfile, unless you all wanted them linked to the same location?

        Personally, I think you would be better off with a file or array rather than reading a directory everytime someone visits, or even better a mysql database storing information?
        PHP Weekly - A PHP Developers Resource
        PHP 5.1.4 and Ruby on Rails web hosting
        Moderator of PHP and Work offers and Requests
        Install Apache/PHP/MySQL
        (by marek_mar) | TinyPlugin Architecture

        Comment


        • #5
          Database sounds good. Could you show me how to randomize it now? Query's, setting it up will be easy.

          Thanks
          Stevie Peele
          Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
          #dev - any programming,etc. question
          #design - design discussion and critque
          #central - general chat
          Come join us!

          Comment


          • #6
            Okay, I got my database table setup, so all I need to do is know how to randomize it. Here is what I got so far -
            PHP Code:
            <?php
            $link 
            mysql_connect("localhost","root","rootpass") or die(mysql_error());
            mysql_select_db("test",$link) or die(mysql_error());
            $query "SELECT id,href,img,alt FROM randlinks ORDER BY id DESC";
            $result mysql_query($result) or die(mysql_error());
            if(
            $result){
            if(
            mysql_num_rows($result) == "0"){
            echo  
            "<b>No information in database!</b>";
            }
            else {
            while(
            $row mysql_fetch_assoc($result)){
            /*RANDOMIZE*/
            }
            }
            ?>
            Stevie Peele
            Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
            #dev - any programming,etc. question
            #design - design discussion and critque
            #central - general chat
            Come join us!

            Comment


            • #7
              Forgot to include it, here is my table structure (attached)
              Attached Files
              Stevie Peele
              Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
              #dev - any programming,etc. question
              #design - design discussion and critque
              #central - general chat
              Come join us!

              Comment


              • #8
                Look into the rand() function at http://www.mysql.com/doc/en/Mathematical_functions.html and use a select with " order by rand() limit 1"
                Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                Comment


                • #9
                  Thats just giving random decimal values. I need 1,2,3 instead of 0.9233482386203. Could you revise my code to show how to did it properly?

                  Thanks for all the help, guys.
                  Stevie Peele
                  Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
                  #dev - any programming,etc. question
                  #design - design discussion and critque
                  #central - general chat
                  Come join us!

                  Comment


                  • #10
                    Just like i told you.
                    PHP Code:
                    <?php
                    $link 
                    mysql_connect("localhost","root","rootpass") or die(mysql_error());
                    mysql_select_db("test",$link) or die(mysql_error());
                    $query "SELECT id,href,img,alt FROM randlinks ORDER BY RAND() LIMIT 1";
                    $result mysql_query($result) or die(mysql_error());
                    if(
                    $result){
                    if(
                    mysql_num_rows($result) == "0"){
                    echo  
                    "<b>No information in database!</b>";
                    }
                    else {
                    while(
                    $row mysql_fetch_assoc($result)){
                    /*RANDOMIZE*/
                    }
                    }
                    ?>
                    Just tested it on a table of mine and it works perfecty.
                    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                    Comment


                    • #11
                      Thanks a lot. It works like a freaking charm

                      Here is my code, it's inside of a class, btw.
                      PHP Code:
                      function randImgs(){
                          
                              
                      $this->connect();
                              
                      $query "SELECT id,href,img,alt FROM randlinks ORDER BY RAND() LIMIT 1";
                              
                      $result mysql_query($query) or die(mysql_error());
                              if(
                      $result){
                                  if(
                      mysql_num_rows($result) == "0"){ echo "No Information in database!"; }
                                  else {
                                      while(
                      $row mysql_fetch_assoc($result)){
                                      
                                          
                      $this->href $row['href'];
                                          
                      $this->img $row['img'];
                                          
                      $this->alt $row['alt'];
                                          
                                          echo 
                      "<a href='$this->href' target='_blank'><img src='$this->img' alt='$this->alt' border='0' width='88' height='31' /></a>";
                                      }
                                  }
                              }
                          } 
                      Stevie Peele
                      Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
                      #dev - any programming,etc. question
                      #design - design discussion and critque
                      #central - general chat
                      Come join us!

                      Comment

                      Working...
                      X