mathiasppc Posted May 1, 2011 Share Posted May 1, 2011 Hey, I don't know much PHP yet. I'm trying to write an offer and landing page rotation. This is what I've come up with so far: <?php extract($_REQUEST); //Landing page rotation $counthandle=fopen("lprotation.txt","r"); $getcurrent=fread($counthandle,filesize("lprotation.txt")); switch($getcurrent){ case "lp1": $lp_link = "lp1.php"; $getcurrent = "lp2"; break; case "lp2": $lp_link = "lp2.php"; $getcurrent = "lp1"; break; } fclose($counthandle); $counthandle1=fopen("lprotation.txt","w"); fputs($counthandle1,$getcurrent); fclose($counthandle1); //Offer rotation $counthandle=fopen("offerrotation.txt","r"); $getcurrent=fread($counthandle,filesize("offerrotation.txt")); switch($getcurrent){ case "offer1": $offer = "a Pair of UGG Shoes"; $offer_link = "offer1.php"; $getcurrent = "offer2"; break; case "offer2": $offer = "an Apple iPhone 4"; $offer_link = "offer2.php"; $getcurrent = "offer1"; break; } fclose($counthandle); $counthandle1=fopen("offerrotation.txt","w"); fputs($counthandle1,$getcurrent); fclose($counthandle1); include($lp_link); ?> The problem with this code is that it alternates between lp1+offer1 and lp2+offer2. I can think of several ways to fix this e.g. doubling the offers. offer1 = offer2, offer3 = offer4 and so on...The more offers and lp's I add the more ugly the code gets. I need it to rotate like this: lp1 + offer1, lp2 + offer1, lp1+offer2, lp2+offer2. Can anyone think of a more simple and elegant way to do this? Thanks alot :-) Mathias Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/ Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 You could use sessions instead. Rather than writing $getcurrent to your lprotation and offerrotation text files. Also you could store all your rotation pages and offeres within a database. That way you don't need to keep defining your pages/offers each time they change. You can then completely automate the rotation with a loop or to. Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209125 Share on other sites More sharing options...
mathiasppc Posted May 1, 2011 Author Share Posted May 1, 2011 I don't know much of PHP. Could you elaborate more about sessions? I don't know if the database idea would be better. This script is for use with prosper202, so I would already have to enter the offers and landing pages there, and just copy them over to the php code. Are you sure there aren't any quick fixes for my current code? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209129 Share on other sites More sharing options...
mathiasppc Posted May 1, 2011 Author Share Posted May 1, 2011 bump :'( Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209198 Share on other sites More sharing options...
mathiasppc Posted May 2, 2011 Author Share Posted May 2, 2011 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209311 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 i was looking at this last night, and have an idea for an answer i will get back to you in a few hours, few errands to run first Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209313 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 is the page rotation and offer rotation to work per user or for all users i.e dave arrives at your site and gets page1, offer 1 then pete arrives and gets page2 offer 1.... or should he also get page1,offer1 if its global, dave refreshes the page and gets page2 offer1, or since pete had that page, would dave then get page1 offer2? and how long does the rotation last? as a session variable (only while visitor is on site), a cookiee (can be months) or put it all another way, does each visitor get to see every offer in rotation, or just chance what rotation is when they arrive. I guess thats the difference between global and specific roation if that makes anysense. only the code solution would be quite different with your current code, you can certainly make it cleaner, as you could store all the info on 1 text file for starters. I think arrays might be a good solution if you cant use a database. $currentinfo = array("page"=>1,"offer"=>1); $pages = array(1=>"lp1.php",2=>"lp2.php",3=>"lp3.php"); $offers = array( $offer1= array("offer"=>"UGG Shoes","link"=>"offer1.php"),$offer2 = array("offer"=>"iphone 4","link"=>"offer2.php"),$offer3 = ("offer"=>"karma sutra dvd","link"=>"offer3.php") ); essentially you would update the $currentinfo array to hold current landing page and , and from that derive the page and offer needed. it is easily expandable in this format too. The current infoarray could be held in the session variable if its to be per user, otherwise you need to keep it somewhere where it gets updated by any users (i.e in a text file/database) Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209369 Share on other sites More sharing options...
wildteen88 Posted May 2, 2011 Share Posted May 2, 2011 Here is an example script I was going to post last night but my internet was not working. <?php session_start(); $pages = array('page1', 'page2', 'page3', 'page4'); // add pages into this array $offers = array('offer1', 'offer2'); // add offers into this array ?> <a href="<?php echo basename(__FILE__) ?>?reset">Reset</a> | <a href="<?php echo basename(__FILE__) ?>">Next</a> <hr /> <?php if(isset($_GET['reset'])) { $_SESSION['shownOffers'] = array(); $_SESSION['shownPages'] = array(); $_SESSION['currentPage'] = null; } function getPage($pages) { if(isset($_SESSION['currentPage']) && $_SESSION['currentPage'] == end($pages)) { $_SESSION['shownPages'] = array(); $_SESSION['currentPage'] = null; } foreach($pages as $key => $page) { if(!in_array($page, $_SESSION['shownPages'])) { $_SESSION['shownPages'][] = $page; $_SESSION['currentPage'] = $page; return $page; } } } function getOffer($offers) { if(isset($_SESSION['currentOffer']) && $_SESSION['currentOffer'] == end($offers)) { $_SESSION['shownOffers'] = array(); $_SESSION['currentOffer'] = null; } foreach($offers as $key => $offer) { if(!in_array($offer, $_SESSION['shownOffers'])) { $_SESSION['shownOffers'][] = $offer; $_SESSION['currentOffer'] = $offer; return $offer; } } } $currentPage = getPage($pages); $currentOffer = getOffer($offers); ?> <h1><?php echo $currentPage; ?></h1> <h2><?php echo $currentOffer; ?></h2> <?php echo '<pre>' . print_r($_SESSION, true) . '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209371 Share on other sites More sharing options...
mathiasppc Posted May 2, 2011 Author Share Posted May 2, 2011 Wow thanks alot I guess this would be global. Visitor clicks the site, goes to lp1 and offer1, the next visitor goes to another landing page with the same offer(or opposite). 3offers and 2 lp's would look something like this: visitor 1: lp1 + offer1 visitor 2: lp2 + offer1 visitor 3: lp1 + offer2 visitor 4: lp2 + offer2 visitor 5: lp1 + offer3 visitor 6: lp2 + offer3 In this example one complete cycle would consist of 3offer X 2lp = 6 combinations. Normally the site would recieve a couple of hundreds clicks a day, and by doing this kind of rotation I'm able to analyze and see which combinations performs better. wildteen88 I tried your code, but I needed an uneven number of lp's and rotations to get it to go through the combinations?(i.e. 2 offers and 3 lp's) Also sessions wouldn't work for global? I think to save in .txt would be the best solution. What are the advantages of databases? Is this something I can access from cPanel? Thanks again guys!! Mathias Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209557 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 if your host has mysql they might well have something in a cpanel. and failing anything in the cpanel, phpmyadmin is a very simple install. without a database you can still do it I think then a combination arrays and textfile for storing the current offer and page will do the job just as well though. you should be able to read and write to a file in one object and use that to get current page/offer array, as well as updating it for the next user to hit the site. then from there just pull the info from the other arrays storing the pages and offers. way I see it only one thing changes and thats the page/offer combo so thats the only thing that needs to be in a text file. obvioulsy afte ra month a set of new offers might be available but just change that manually as and when. databases are awesome btw, they are essentially textfiles too, but with a very good supporting set of functions and operations e.g. SQL language of some sort, which allows excellent colation and manipulation of data. Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209566 Share on other sites More sharing options...
mathiasppc Posted May 2, 2011 Author Share Posted May 2, 2011 Do you know how to do the actual rotation? I just can't figure out how to do get it to go through one of the variabes with the other one waiting. I know this could be easily done with two loops in other programming languages, but I guess it's completely different when you have to save and update information for each visitor -Mathias Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209589 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 even though I have my own challenges to deal with, why are other peoples more fun to resolve! will post a script for you shortly (i hope ) Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209600 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 ok so i have ended up using a switch statement dispite everything i said before. it sort of works, only I cant get the file open mode right. text file: 1,1 php file: <?php $counthandle=fopen("file.txt","r+"); //w+ indicates read and write and empty file contents upon writing, rather than appending which r+ would do(this is where I am getting stuck) $getcurrent=fread($counthandle,filesize("file.txt")); echo $getcurrent; switch ($getcurrent){ case "1,1": //redirect to lp1,offer1 fwrite($counthandle,"1,2"); break; case "1,2": //redirect to lp1,offer2 fwrite($counthandle,"2,1"); break; case "2,1": //redirect to lp2,offer1 fwrite($counthandle,"2,2"); break; case "2,2": //redirect to lp2,offer2 fwrite($counthandle,"1,1"); break; default://incase nothing in file or out of specified ranges //redirect to lp1,offer1 fwrite($counthandle,"1,2"); break; } fclose($counthandle); ?> only the script is appending on the end, and not overwriting, if i open in w+ mode it hangs on the file size bit . This is because I am trying to only open the file object once, you could easily re work this to just open file, read, close. open file, write, close Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209620 Share on other sites More sharing options...
mathiasppc Posted May 2, 2011 Author Share Posted May 2, 2011 Thanks spider! I really appreciate it:) So what you suggest I do is that I use my original code, and instead of the seperate switch statements for offers and lps, I use one with LP x offer number of combinations? There wouldn't any easier way to do this so If I i.e. use 4 LP's and 5 offers, I would add 9 elements instead of 20 combinations of 2 elements? Don't answer this if you don't have the time! Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209628 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 im working on this one a bit more first, but that said i might have 2 solutions in the end, using the arrays again on teh second, depends how much i procrastinate on this instead ofdoing my own stuff, lol Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209630 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 ok this works but of course the switch statement will expand with more pages or offers: <?php function updatefile($newcounthandle){ $counthandle=fopen("file.txt","w"); fwrite($counthandle,$newcounthandle); fclose($counthandle); } $counthandle=fopen("file.txt","r"); $getcurrent=fread($counthandle,filesize("file.txt")); fclose($counthandle); echo $getcurrent; switch ($getcurrent){ case "1,1": updatefile("1,2"); //redirect to lp1,offer1 break; case "1,2": updatefile("2,1"); //redirect to lp2,offer1 break; case "2,1": updatefile("2,2"); //redirect to lp1,offer2 break; case "2,2": updatefile("1,1"); //redirect to lp2,offer2 break; default://incase nothing in file or out of specified ranges updatefile("1,2"); //redirect to lp1,offer1 break; } ?> note i have changed around a bit, in text file 1,1 : the first one is landing page, the second 1 is offer going to try an array version now lol but don't hold ya breath lol Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209638 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 i gave up after 5 mins because i couldn't see a way to avoiding the switch function, so it seemed all a bit pointless as it would be just as much code in the end. Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209648 Share on other sites More sharing options...
mathiasppc Posted May 3, 2011 Author Share Posted May 3, 2011 Thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209757 Share on other sites More sharing options...
mathiasppc Posted May 3, 2011 Author Share Posted May 3, 2011 It worked great.. Still trying to figure out the 'optimal' way though Found this about offer rotation: http://prosper.tracking202.com/scripts/rotate-offers/ Is someone able to modify it to rotate lp's aswell? Mathias Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209777 Share on other sites More sharing options...
spiderwell Posted May 3, 2011 Share Posted May 3, 2011 you are the man to do that as the lone ranger supposedly once said 'my work here is done' Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1209805 Share on other sites More sharing options...
mathiasppc Posted May 4, 2011 Author Share Posted May 4, 2011 I got it working now: $myFile = "offerrotation.txt"; $myFile2 = "lprotation.txt"; $fh = @fopen($myFile, 'r'); $offerNumber = @fread($fh, 5); @fclose($fh); $fh = @fopen($myFile2, 'r'); $lpNumber = @fread($fh, 5); @fclose($fh); if ($lpNumber >= count($lp)) { $lpNumber = 1; if ($offerNumber >= count($offer)) { $offerNumber = 1; } else { $offerNumber = $offerNumber + 1; } } else { $lpNumber = $lpNumber + 1; } $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $offerNumber . "\n"; fwrite($fh, $stringData); fclose($fh); $fh = fopen($myFile2, 'w') or die("can't open file"); $stringData = $lpNumber . "\n"; fwrite($fh, $stringData); fclose($fh); Does anyone know how I can combine "offerrotation.txt" and "lprotation.txt" into one .txt. The first number corresponding offer and the second lp? Is it even possible to read and write to specific parts of a .txt? Thanks:) Mathias Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1210539 Share on other sites More sharing options...
spiderwell Posted May 5, 2011 Share Posted May 5, 2011 i only used one text file..... Quote Link to comment https://forums.phpfreaks.com/topic/235289-simple-rotation/#findComment-1210693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.