1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

mysql/php script - insert random cell from corresponding field value

Discussion in 'Hire a Freelancer' started by pNuker, Jul 13, 2011.

  1. pNuker

    pNuker Junior Member

    Joined:
    Sep 15, 2008
    Messages:
    130
    Likes Received:
    12
    I'll try to describe what I need as best I can.

    I have a MySQL table with two fields: "id" and "merchant"

    I need to be able to send visitors to a single URL that will then go through a script, grab a random "id" cell that matches a particular "merchant", and then redirect to a page on my site using the "id" in the url.

    For example, I want to go to:
    http://www.mysite.com/walmart.php

    run through a script to grab a random id from a given merchant, and insert it into a url such as:
    http://www.mysite.com/random-walmart-id

    I hope this makes sense!
    I'll pay $25 for this via paypal.
     
    Last edited: Jul 13, 2011
  2. surender.singal

    surender.singal Registered Member

    Joined:
    Jun 4, 2011
    Messages:
    89
    Likes Received:
    3
    Occupation:
    Web Programmer
    Location:
    DL
    Hi ,
    i ma quite interested in this work.Pm sent
     
  3. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    Assuming I've got this right.. You have a table that looks something like:

    Code:
    mysql> select * from merchant_table;
    +------+----------+
    | id   | merchant |
    +------+----------+
    |  123 | walmart  |
    |  456 | walmart  |
    |  789 | walmart  |
    |  123 | macys    |
    |  456 | macys    |
    |  789 | macys    |
    +------+----------+
    6 rows in set (0.01 sec)
    
    and you want to specify a merchant, and then return a random ID belonging to that merchant and redirect to the appropriate page?

    Upload this as merchant.php. Add whatever merchant you want to use as a $_GET argument, eg. http://www.youdomain.com/merchant.php?merchant=walmart

    That will redirect to a matching id page for that merchant.

    You can edit the structure of the url your redirect to by editing the value of $url near the bottom.

    PHP:
    <?php

    /*************************************************************************\
    merchant.php

    Merchant to ID redirector

    By Autumn@BHW

    Usage:
    Set your details in the config section below.
    Call this script with the merchant name as a GET request.
    eg. http://www.yourdomain.com/merchant.php?merchant=walmart

    Created:  Thu Jul 14 16:57:57 NZST 2011
    \*************************************************************************/

    // Configuration

    // Your domain
    $domain 'www.yourdomain.com';

    // Mysql connection details
    $mysql_host 'localhost';
    $mysql_user 'username';
    $mysql_pass 'password';

    // Mysql database and table name
    $db_name    'merchant_db';
    $table_name 'merchant_table';





    /*****************************************************************************/

    // Get merchant as a GET request
    $merchant $_GET['merchant'];
    if(empty(
    $merchant)) {
        echo 
    "You must specify a merchant\n";
        exit;
    }



    // Make a basic connection to the database
    if(!$conn) {
        if(!
    $conn mysql_connect($mysql_host$mysql_user$mysql_pass)) {
            echo 
    "mysql_connect.php - ERROR: Error connecting to mysql.\n";
            exit;
        } else {
            
    // $conn has been created, continue
            //echo "Connected to database\n";
        
    }
    } else {
        
    // $conn already exists, continue
    }



    // Select the database
    mysql_select_db($db_name$conn);



    // Randomly select an id from this merchant
    $query  "SELECT id FROM $db_name.$table_name WHERE merchant='$merchant' ORDER BY RAND() LIMIT 1";
    $result mysql_query($query$conn);
    $row    mysql_fetch_row($result);
    $id     $row[0];



    // Generate the url to redirect to
    $url "http://$domain/$merchant-$id";
    //echo "$url\n";



    // Issue the redirect
    header("Location: $url"TRUE301);


    ?>
     
  4. pNuker

    pNuker Junior Member

    Joined:
    Sep 15, 2008
    Messages:
    130
    Likes Received:
    12
    Thanks guys, I found a solution.

    Autumn, that looks interesting, I'll try that out tomorrow. Thanks.
     
  5. nesterdwarf

    nesterdwarf Regular Member

    Joined:
    May 30, 2008
    Messages:
    276
    Likes Received:
    539
    Location:
    State of Misery...er, Missouri...
    pNuker,

    Autumn's code looks oddly familiar ;)

    ND
     
  6. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    Oddly familiar? I just wrote it.
     
  7. nesterdwarf

    nesterdwarf Regular Member

    Joined:
    May 30, 2008
    Messages:
    276
    Likes Received:
    539
    Location:
    State of Misery...er, Missouri...
    Autumn,

    Sorry if my comment somehow offended you...

    I did the project for pNuker, and my code looks almost exactly like yours with the exception that I hard coded the merchant name as one of the requirements of the original post was being able to simply have a URL with the merchant name. I also explored the GET option, but it doesn't allow for the directory structure that he specified...

    ND