Jump to content

President Obama

Members
  • Posts

    55
  • Joined

  • Last visited

    Never

Posts posted by President Obama

  1. Don't worry I got it working, was a couple of problems.

     

    A) I wasn't connecting to my database at all in the ajax.php

    B) I didn't use semicolons on the 4 javascript variables, which is strange because I thought javascript didn't need semicolens

    C) Apparently having the code in another document like I did also stuffs it up, probably because of the php I used in it.

  2. Fine.

    include 'config.php';
    $result = mysql_query("SELECT * FROM adverts") or die(mysql_error()); // Get Ads
    $result2 = mysql_query("SELECT * FROM lil_urls WHERE id=$text") or die(mysql_error()); // Get User Link
    $link = mysql_fetch_array($result2); // Array Convert User Link
    $ads = array(); // array for ad id
    $bds = array(); // array for ad url
    $cds = array(); // array for the two aboved combined to set the index as the id
    
    // Fill first 2 arrays
    while($row = mysql_fetch_array($result)){
    $ads[] = $row['id'];
    $bds[] = $row['url'];
    }
    // Combine the 2 arrays to set the index as the id
    $cds = array_combine($ads, $bds);
    
    
    // Start Generating 4 random numbers to choose 4 random adds  
    $result3 = array();
    $numberCount = 4;
    $thisCount   = 0;
    while($thisCount<$numberCount) {
        $thisNumber = array_rand($cds);
        if(!in_array($thisNumber, $result3)) {
          $result3[$thisCount] = $thisNumber; 
     $thisCount++;
       }
      }  
    // End generating. 
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <script type="text/javascript" src="jquery/jquery.js"></script>
    <script type="text/javascript" src="jquery/javascript.js"></script>
    </head>
    <body>
    <pre>
    <?php print_r($result3); print_r($cds); ?>
    </pre>
    <a id="advert1" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[0]]; ?>')">Banner #1</a>
    <a id="advert2" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[1]]; ?>')">Banner #2</a>
    <a id="advert3" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[2]]; ?>')">Banner #3</a>
    <a id="advert4" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[3]]; ?>')">Banner #4</a>
    </body>
    </html>

  3. Yeah I redesigned the code, this works kinda. Well it did in a test but in practical it won't, do you see the problem in this?

    ajax.php

    <?php
    if(isset($_POST['functionid'])){
    switch ($_POST['functionid']){
    	case 1:
    		ad1();
    		break;
    	case 2:
    		ad2();
    		break;
    	case 3:
    		ad3();
    		break;
    	case 4:
    		ad4();
    		break;
    }
    
    } 
    function ad1(){
    if(isset($_POST['result'])){
    	$advert = $_POST['result'];
    	mysql_query("UPDATE adverts SET views = views+1 WHERE id = $advert");
    }
    }
    ?>
    
    

    Javascript.js:

    $(document).ready(function() {
    
    // Get advert numbers
    var advert1 = <?php echo $cds[$result3[0]]; ?>
    var advert2 = <?php echo $cds[$result3[1]]; ?>
    var advert3 = <?php echo $cds[$result3[2]]; ?>
    var advert4 = <?php echo $cds[$result3[3]]; ?>
    	  
      	$("#advert1").click(function(){
    	alert("Success")
      $.post("ajax.php?", 
      		{result: advert1, functionid: 1},
    		function(data){alert("Success")})
      	});
    $("#advert2").click(function(){
      $.post("ajax.php?", 
      		{result: advert2, functionid: 2})
      	});
    $("#advert3").click(function(){
      $.post("ajax.php?", 
      		{result: advert3, functionid: 3})
      	});
    $("#advert4").click(function(){
      $.post("ajax.php?", 
      		{result: advert4, functionid: 4})
      	});
    
    });

     

    HTML:

    <a id="advert1" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[0]]; ?>')">Banner #1</a>
    <a id="advert2" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[1]]; ?>')">Banner #2</a>
    <a id="advert3" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[2]]; ?>')">Banner #3</a>
    <a id="advert4" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[3]]; ?>')">Banner #4</a>

     

  4. What I want to do is when someone clicks on a link, call a php function in another file which updates the database. This is what I've kinda come up with, doesn't work though. (Uses Jquery)

    <?php
    $result3 = array();
    $result3[0] = $_POST['result0'];
    $result3[1] = $_POST['result1'];
    $result3[2] = $_POST['result2'];
    $result3[3] = $_POST['result3'];
    mysql_query("UPDATE adverts SET views = views + 1 WHERE id = $result3[0]")or die(mysql_error());
    mysql_query("UPDATE adverts SET views = views + 1 WHERE id = $result3[1]")or die(mysql_error());
    mysql_query("UPDATE adverts SET views = views + 1 WHERE id = $result3[2]")or die(mysql_error());
    mysql_query("UPDATE adverts SET views = views + 1 WHERE id = $result3[3]")or die(mysql_error());
    ?>

     

    Jquery:

    $(document).ready(function(){
    var result0 = <?php echo $result3[0]; ?>
    var result1 = <?php echo $result3[1]; ?>
    var result2 = <?php echo $result3[2]; ?>
    var result3 = <?php echo $result3[3]; ?>
    $('.advert').click(function(){
    	$.post("functions.php", {result0: "result0", result1: "result1", result2: "result2", result3: "result3" })
    })
    });

     

    HTML:

    <a class="advert" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[0]]; ?>')">Banner #1</a>

  5. $bl = array(); 
    $link1 = array_rand($cds); 
    $link2 = array_rand($cds); 
    $link3 = array_rand($cds); 
    $link4 = array_rand($cds); 
    $A = 0;
    while (!in_array($link1, $bl)){
    $link1 = array_rand($cds);
    $bl[$A] = $link1;
    $A++;
    }
    while (!in_array($link2, $bl)){
    $link2 = array_rand($cds);
    $bl[$A] = $link2;
    $A++;
    }
    while (!in_array($link3, $bl)){
    $link3 = array_rand($cds);
    $bl[$A] = $link3;
    $A++;
    }
    while (!in_array($link4, $cds)){
    $link4 = array_rand($cds);
    $bl[$A] = $link4;
    $A++;
    }
    echo $link1;
    echo $link2;
    echo $link3;
    echo $link4;

     

    Pretty much I'm trying to generate 4 number's between the min and max of an array but I don't want any duplicates. So it checks if the number exists in a bl array if it does it generates another number, well thats the theory. Doesn't work like that though.

  6. eh should of explained my self better. What I'm doing is pulling 4 ads at random out of the db, but with each pull I want to add 1 to the views for that specific ad.

     

    As in the links I only want the URL but with the views I want the Id to compare to the database and find out which ad to add a view to. Also if I use rand 4 times it will pull duplicates which I also don't want. Hard to explain.

  7. Ok I'm really lost here, what I need to do is shuffle a 2D array. Now when I shuffle them I want to keep both columns aligned to say. Like I only want the index to change.

     

    Kinda like this:

     

    Index        Id          URL

    0                [2          www.e] I want the bracketed stuff to stay aligned.

    1                [6          www.b]

    2                [12        www.c]

    3                [60        www.h]

     

    But I want them in different positions on every page load. So when I echo array[1][0] it will always be different. Is this even possible?

     

     

  8. Don't worry, got it working. heres the code I used:

    <?php 
    include 'config.php';
    $result2 = mysql_query("SELECT * FROM adverts") or die(mysql_error())
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html>
    <head>
    </head>
    <body>
    
    <?php 
    $ads = array();
    $A = 0;
    while($row = mysql_fetch_array($result2)){
    $ads[] = $row['url'];
    }
    shuffle($ads);
    
    echo $ads['1'];
    ?>
    
    
    </body>
    </html>
    

  9. Eh that didn't work. I tried a heap of variations but couldn't work it out.

    <?php 
    include 'config.php';
    $result2 = mysql_query("SELECT * FROM adverts") or die(mysql_error());
    
    $mixed = array();
    while ($row = mysql_fetch_array($result2)){
      $mixed[] = $row;
    }
    $advert = shuffle($mixed);
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html>
    <head>
    </head>
    <body>
    
    <?php echo $advert['1']; ?>
    
    
    </body>
    </html>

     

  10. Ok I got the url shortening working but I have hit a problem that is annoying me.

    <?php 
    include 'config.php';
    $result = mysql_query("SELECT * FROM lil_urls WHERE id=$text") or die(mysql_error());
    $result2 = mysql_query("SELECT id FROM adverts") or die(mysql_error());
    $link = mysql_fetch_array($result);
    $mixed = mysql_fetch_array($result2);
    $advert = shuffle($mixed);
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html>
    <head>
    </head>
    <body>
    <a href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $advert['1']; ?>')">Banner #1</a>
    
    <?php echo $advert['1']; ?>
    
    
    </body>
    </html>

    What I'm trying to do is choose one of the ads at random from the array but it won't work. I really can't figure out why.

     

  11. Well this is the thought I had:

    	if ( $id != '' && $id != basename($_SERVER['PHP_SELF']) )
    {
    	$location = $lilurl->get_url($id);
    
    	if ( $location != -1 )
    	{
    		header('Location: '.$location);
    	}
    	else
    	{
    		$msg = '<p class="error">Sorry, but that lil&#180; URL isn\'t in our database.</p>';
    	}
    }

     

    In that section to add another if inside the current one which uses $_SERVER['REQUEST_URI'] and removes the first 5 letters (the domain and slashes) and the last 4 (extension) which then only leaves the numbers, then run something ( I got a bit lost here) which checks that number against all the records in the number base then if it exists POST the number to the advert page and direct the person there. Then on the advert page check the number against the database and pull the link and apply it to wherever its needed. What can I do for the part that I got lost in, I'm not sure if I'm meant to use an if loop inside a for loop.

  12. Ok So I browsed around and found this code:

    Index:

    <?php /* index.php ( lilURL implementation ) */
    
    require_once 'includes/conf.php'; // <- site-specific settings
    require_once 'includes/hjurl.php'; // <- lilURL class file
    
    $lilurl = new lilURL();
    $msg = '';
    
    // if the form has been submitted
    if ( isset($_POST['longurl']) )
    {
    // escape bad characters from the user's url
    $longurl = trim(mysql_escape_string($_POST['longurl']));
    
    // set the protocol to not ok by default
    $protocol_ok = false;
    
    // if there's a list of allowed protocols, 
    // check to make sure that the user's url uses one of them
    if ( count($allowed_protocols) )
    {
    	foreach ( $allowed_protocols as $ap )
    	{
    		if ( strtolower(substr($longurl, 0, strlen($ap))) == strtolower($ap) )
    		{
    			$protocol_ok = true;
    			break;
    		}
    	}
    }
    else // if there's no protocol list, screw all that
    {
    	$protocol_ok = true;
    }
    
    // add the url to the database
    if ( $protocol_ok && $lilurl->add_url($longurl) )
    {
    	if ( REWRITE ) // mod_rewrite style link
    	{
    		$url = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']).'/'.$lilurl->get_id($longurl);
    	}
    	else // regular GET style link
    	{
    		$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?id='.$lilurl->get_id($longurl);
    	}
    
    	$msg = '<p class="success">URL is: <a href="'.$url.'">'.$url.'</a></p>';
    }
    elseif ( !$protocol_ok )
    {
    	$msg = '<p class="error">Invalid protocol!</p>';
    }
    else
    {
    	$msg = '<p class="error">Creation of your lil&#180; URL failed for some reason.</p>';
    }
    }
    else // if the form hasn't been submitted, look for an id to redirect to
    {
    if ( isSet($_GET['id']) ) // check GET first
    {
    	$id = mysql_escape_string($_GET['id']);
    }
    elseif ( REWRITE ) // check the URI if we're using mod_rewrite
    {
    	$explodo = explode('/', $_SERVER['REQUEST_URI']);
    	$id = mysql_escape_string($explodo[count($explodo)-1]);
    }
    else // otherwise, just make it empty
    {
    	$id = '';
    }
    
    // if the id isn't empty and it's not this file, redirect to it's url
    if ( $id != '' && $id != basename($_SERVER['PHP_SELF']) )
    {
    	$location = $lilurl->get_url($id);
    
    	if ( $location != -1 )
    	{
    		header('Location: '.$location);
    	}
    	else
    	{
    		$msg = '<p class="error">Sorry, but that lil&#180; URL isn\'t in our database.</p>';
    	}
    }
    }
    
    // print the form
    
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html>
    
    <head>
    	<title>HarryJerry.com Linx Generator</title>
    
    	<style type="text/css">
    	body {
    		font: .8em "Trebuchet MS", Verdana, Arial, Sans-Serif;
    		text-align: center;
    		color: #333;
    		background-color: #fff;
    		margin-top: 5em;
    	}
    
    	h1 {
    		font-size: 2em;
    		padding: 0;
    		margin: 0;
    	}
    
    	h4 {
    		font-size: 1em;
    		font-weight: bold;
    	}
    
    	form {
    		width: 28em;
    		background-color: #eee;
    		border: 1px solid #ccc;
    		margin-left: auto;
    		margin-right: auto;
    		padding: 1em;
    	}
    
    	fieldset {
    		border: 0;
    		margin: 0;
    		padding: 0;
    	}
    
    	a {
    		color: #09c;
    		text-decoration: none;
    		font-weight: bold;
    	}
    
    	a:visited {
    		color: #07a;
    	}
    
    	a:hover {
    		color: #c30;
    	}
    
    	.error, .success {
    		font-size: 1.2em;
    		font-weight: bold;
    	}
    
    	.error {
    		color: #ff0000;
    	}
    
    	.success {
    		color: #000;
    	}
    
    	</style>
    
    </head>
    
    <body onload="document.getElementById('longurl').focus()">
    
    	<h1>HarryJerry URL Generator</h1>
    
    	<?php echo $msg; ?>
    
    	<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
    
    		<fieldset>
    			<label for="longurl">Enter a long URL:</label>
    			<input type="text" name="longurl" id="longurl" />
    			<input type="submit" name="submit" id="submit" value="Make it sexy!" />
    		</fieldset>
    
    	</form>
    
    	<h4>Powered by <a href="http://harryjerry.com">HarryJerry.com</a></h4>
    
    </body>
    
    </html>
    
    

    Conf.php

    <?php /* conf.php ( config file ) */
    
    // page title
    define('PAGE_TITLE', 'lil&#180; URL Generator');
    
    // MySQL connection info
    define('MYSQL_USER', 'test');
    define('MYSQL_PASS', 'password');
    define('MYSQL_DB', 'Clx');
    define('MYSQL_HOST', 'localhost');
    
    // MySQL tables
    define('URL_TABLE', 'lil_urls');
    
    // use mod_rewrite?
    define('REWRITE', true);
    
    // allow urls that begin with these strings
    $allowed_protocols = array('http:', 'https:', 'mailto:');
    
    // uncomment the line below to skip the protocol check
    // $allowed_procotols = array();
    
    ?>

    hjurl.php

    <?php /* lilurl.php ( lilURL class file ) */
    
    class lilURL
    {
    // constructor
    function lilURL()
    {
    	// open mysql connection
    	mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Could not connect to database');
    	mysql_select_db(MYSQL_DB) or die('Could not select database');	
    }
    
    // return the id for a given url (or -1 if the url doesn't exist)
    function get_id($url)
    {
    	$q = 'SELECT id FROM '.URL_TABLE.' WHERE (url="'.$url.'")';
    	$result = mysql_query($q);
    
    	if ( mysql_num_rows($result) )
    	{
    		$row = mysql_fetch_array($result);
    		return $row['id'];
    	}
    	else
    	{
    		return -1;
    	}
    }
    
    // return the url for a given id (or -1 if the id doesn't exist)
    function get_url($id)
    {
    	$q = 'SELECT url FROM '.URL_TABLE.' WHERE (id="'.$id.'")';
    	$result = mysql_query($q);
    
    	if ( mysql_num_rows($result) )
    	{
    		$row = mysql_fetch_array($result);
    		return $row['url'];
    	}
    	else
    	{
    		return -1;
    	}
    }
    
    // add a url to the database
    function add_url($url)
    {
    	// check to see if the url's already in there
    	$id = $this->get_id($url);
    
    	// if it is, return true
    	if ( $id != -1 )
    	{
    		return true;
    	}
    	else // otherwise, put it in
    	{
    		$id = $this->get_next_id($this->get_last_id());
    		$q = 'INSERT INTO '.URL_TABLE.' (id, url, date) VALUES ("'.$id.'", "'.$url.'", NOW())';
    
    		return mysql_query($q);
    	}
    }
    
    // return the most recent id (or -1 if no ids exist)
    function get_last_id()
    {	
    	$q = 'SELECT id FROM '.URL_TABLE.' ORDER BY date DESC LIMIT 1';
    	$result = mysql_query($q);
    
    	if ( mysql_num_rows($result) )
    	{
    		$row = mysql_fetch_array($result);
    		return $row['id'];
    	}
    	else
    	{
    		return -1;
    	}
    }	
    
    // return the next id
    function get_next_id($last_id)
    { 
    
    	// if the last id is -1 (non-existant), start at the begining with 0
    	if ( $last_id == -1 )
    	{
    		$next_id = 0;
    	}
    	else
    	{
    		// loop through the id string until we find a character to increment
    		for ( $x = 1; $x <= strlen($last_id); $x++ )
    		{
    			$pos = strlen($last_id) - $x;
    
    			if ( $last_id[$pos] != 'z' )
    			{
    				$next_id = $this->increment_id($last_id, $pos);
    				break; // <- kill the for loop once we've found our char
    			}
    		}
    
    		// if every character was already at its max value (z),
    		// append another character to the string
    		if ( !isSet($next_id) )
    		{
    			$next_id = $this->append_id($last_id);
    		}
    	}
    
    	// check to see if the $next_id we made already exists, and if it does, 
    	// loop the function until we find one that doesn't
    	//
    	// (this is basically a failsafe to get around the potential dangers of
    	//  my kludgey use of a timestamp to pick the most recent id)
    	$q = 'SELECT id FROM '.URL_TABLE.' WHERE (id="'.$next_id.'")';
    	$result = mysql_query($q);
    
    	if ( mysql_num_rows($result) )
    	{
    		$next_id = $this->get_next_id($next_id);
    	}
    
    	return $next_id;
    }
    
    // make every character in the string 0, and then add an additional 0 to that
    function append_id($id)
    {
    	for ( $x = 0; $x < strlen($id); $x++ )
    	{
    		$id[$x] = 0;
    	}
    
    	$id .= 0;
    
    	return $id;
    }
    
    // increment a character to the next alphanumeric value and return the modified id
    function increment_id($id, $pos)
    {		
    	$char = $id[$pos];
    
    	// add 1 to numeric values
    	if ( is_numeric($char) )
    	{
    		if ( $char < 9 )
    		{
    			$new_char = $char + 1;
    		}
    		else // if we're at 9, it's time to move to the alphabet
    		{
    			$new_char = 'a';
    		}
    	}
    	else // move it up the alphabet
    	{
    		$new_char = chr(ord($char) + 1);
    	}
    
    	$id[$pos] = $new_char;
    
    	// set all characters after the one we're modifying to 0
    	if ( $pos != (strlen($id) - 1) )
    	{
    		for ( $x = ($pos + 1); $x < strlen($id); $x++ )
    		{
    			$id[$x] = 0;
    		}
    	}
    
    	return $id;
    }
    
    }
    
    ?>
    

     

    Now this is the important part here:

    // if the id isn't empty and it's not this file, redirect to it's url
    if ( $id != '' && $id != basename($_SERVER['PHP_SELF']) )
    {
    	$location = $lilurl->get_url($id);
    
    	if ( $location != -1 )
    	{
    		header('Location: '.$location);
    	}
    	else
    	{
    		$msg = '<p class="error">Sorry, but that lil&#180; URL isn\'t in our database.</p>';
    	}
    }
    }

    How do I manipulate that to show a page but keep the url the same like www.clickme.com/453456 but with the banner page on it, even though that page doesn't exist?

  13. So a friend is making a URL shortening site, well hes already made it and got it working but he wanted me to do some work on it and then he showed me the code and I just felt like shooting myself in the head for offering to help. Its an absolute mess the are tonnes of folders with tonnes of files in each with more folders in them that contain code that could easily be functions. He said he added on top of some other code he got from somewhere.

     

    So I'm thinking of just starting again. Everything that needs to be done I can do except for the URL shrinking. How do I make a redirect without actually creating the file. So say www.clickme.com/randomnumbers but that random numbers isn't a file and just comes up with a page with adverts then a link to continue on.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.