GFXVoid Graphic Design Forum

Remove Text Formatting
Loading...

» Online Users: 1,956

0 members and 1,956 guests
No Members online

» Site Navigation

 > FAQ

» Stats

Members: 35,442
Threads: 103,075
Posts: 826,688
Top Poster: cc.RadillacVIII (7,429)
Welcome to our newest member, Lekelindids
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2007
    Location
    London - England
    Posts
    1,841

    Default I think its PHP :S

    I need A code so that It genorates a random code from a database and once its been genorated it gets deleted from the database.
    Anyhelp will be much loved and also if Its possible to get it 1 per Ip every 24 hours thatll be even more loved ( I know proxies and stuff but I dont really care too much)

  2. #2
    Join Date
    Jul 2005
    Location
    Calgary, Canada
    Posts
    2,566

    Default

    What do you mean by "random code" and are you going to be managing your own database? I'll assume that since you are working with databases, you will be doing SQL queries and by "random code" you mean a random row from a table in your database. But I have to ask, are you going to be continually populating your db? Since you are deleting entries every time you generate one randomly.

    Anyway, heres the code to generate a random entry and then delete it in PHP

    Heres an example row:

    +-----+-------------+-------+----------+----------------------+---------------------+-------+
    | uid | username | name | password | email | date_registered |
    +-----+-------------+-------+----------+----------------------+---------------------+
    | 1 | carrotderek | Derek | poopie | derekszeto@gmail.com | 2008-04-26 01:07:37 |
    +-----+-------------+-------+----------+----------------------+---------------------+

    Every table has a unique identifier known as a primary key. In this case, the primary key is the uid.

    Code:
    <?php 
    //connection information
    $connection = mysql_connect ("<host>","<username>r", "<password>") OR DIE ("Cannot make the connection");
    
    //connect to database and select
    $db = mysql_select_db ("<databse>",$connection) OR DIE ("Cannot connect to database");
    
    //get a random row from a table (note: this will select more than 1 column, you will have to define variables for each column to display them)
    $query="SELECT * FROM <table> ORDER BY RAND() LIMIT 1";
    $result = mysql_query($query);
    
    //checks if your query brings up the row you want
    if(mysql_num_rows($result)>0){
         //return the row as an array -- this is how we assign each column to a variable
         $row = mysql_fetch_array($result);
          
         //$row is the array, get the elements inside the array
         $username = $row['username'];
         $uid = $row['uid'];
     
         //test some output
         echo $username;
         
         //delete what you generated from the database.
         $query = "DELETE FROM <table> WHERE uid='$uid'";
         mysql_query($query) OR DIE ("could not remove entry from db");
    }//end if
    
    ?>
    It's MORPHIN' time!
    github
    deviantART
    Last.FM
    carrotderek

  3. #3
    Join Date
    Nov 2007
    Location
    London - England
    Posts
    1,841

    Default

    what i mean is in my database say i have
    abcdefg
    efghsei
    egrilngrf etc.
    when one of them is genorated I want it to be delted after it is seen

  4. #4
    Join Date
    Jul 2005
    Location
    Calgary, Canada
    Posts
    2,566

    Default

    In that case, there is no need to have a database of codes if the data in it is going to be volatile. It's redundant to store volatile data in a database. You can just use the random function that is built into PHP.

    Just call function like this:
    Code:
    <?php
    
    function generateCode ($length){
        $letters = "abcdefghijklmnopqrstuvxyz";
        $string .= $letters;
     
        for($i=0; $i < $length; $i++){
             $code .= $string{rand() &#37; strlen($string) }; 
        }//end for
    
        return $code
    }//end function
    ?>
    So you can use the function as:

    Code:
    <?php 
        echo "Randomized code: ";
        echo generateCode(5);
    ?>
    will output something like "swpjt"
    It's MORPHIN' time!
    github
    deviantART
    Last.FM
    carrotderek

  5. #5
    Join Date
    Nov 2007
    Location
    London - England
    Posts
    1,841

    Default

    thankyou for your help so very much :]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Powered by vBadvanced CMPS v4.1.1