codergirl Posted April 22, 2010 Share Posted April 22, 2010 Hey there, I just registered because I couldn't figure this out by myself after searching and trying for several hours. I'm sure this will be easy to do for most of you. I need a php script which will randomly chose one link from a list and assign this specific link to a single wordpress post only. The link needs to be chosen only once and be static from then on - the link is NOT supposed to change when refreshing the page. The link list would basically look like this: <a href="http://www.google.com" title="Google">Google</a> <a href="http://www.noodle.com" title="Noodle">Noodle</a> <a href="http://www.noodle.com/spaghetti/" title="Spaghetti">Spaghetti</a> <a href="http://www.fantasyunicornworld.com/genocide/" title="Weird">Nonsense</a> Well, you know what I mean. So it's not merely a list of urls but rather a list of tons of urls with different titles and anchor texts. Can anyone here help me achieve the functionality I just tried to explain? Thanks a lot in advance for any possible help. Quote Link to comment https://forums.phpfreaks.com/topic/199335-displaying-a-randomly-chosen-static-link/ Share on other sites More sharing options...
oni-kun Posted April 22, 2010 Share Posted April 22, 2010 You could base it off the ID of the post, Say randomly assign numbers to each multidementional entry of an array, if the sum of the numbers is xxx, then display a certain array entry. This would work on most posts, although there's a chance of repeating (Not sure if you want to use MySQL to handle it or something homebrewed.) Quote Link to comment https://forums.phpfreaks.com/topic/199335-displaying-a-randomly-chosen-static-link/#findComment-1046201 Share on other sites More sharing options...
codergirl Posted April 22, 2010 Author Share Posted April 22, 2010 Say randomly assign numbers to each multidementional entry of an array, if the sum of the numbers is xxx, then display a certain array entry. Thanks for your reply. I'm not a coder myself, I can only write extremely simple lines of code. I know how to set a link according to the category the post is in or according to certain keywords in the post title. I think I could also base it off the post ID in general, but the problem is that a lot of posts are scheduled and new posts are published on a daily basis. My goal is to automate the whole process, I don't have the time to go through hundreds of posts manually. And I have a long list of urls which I want to use. Your approach might work well but I don't have any clue on how to do this. Is there no simple way of selecting something randomly without making the output dynamic? Quote Link to comment https://forums.phpfreaks.com/topic/199335-displaying-a-randomly-chosen-static-link/#findComment-1046210 Share on other sites More sharing options...
oni-kun Posted April 22, 2010 Share Posted April 22, 2010 Meh, I was bored. I'm not sure this is the best way to go around this, but it does the job. You could also base the seed on the strlen of the keywords+title or whatever. $random = Array( 1 => '<a href="http://www.foo.com" title="foobar">Foo</a>' , 2 => '<a href="http://www.google.com" title="Google">Google</a>' , 3 => '<a href="http://www.noodle.com" title="Noodle">Noodle</a>' , 4 => '...' , 5 => 'And' , 6 => 'Some' , 7 => 'More' , 8 => 'Random' , 9 => 'Entries', 10 => 'Here' , 11 => 'And' , 12 => 'Here' ); //$_GET['id'] $postnumber = Array(332, 24, 62, 192, 52, 62, 12); foreach ($postnumber as $post) { $decide = str_split($post); $iterator = 0; foreach($decide as $add) { $iterator += $add; } print "Random: " . $random[$iterator] . '<br/>'; } Quote Link to comment https://forums.phpfreaks.com/topic/199335-displaying-a-randomly-chosen-static-link/#findComment-1046213 Share on other sites More sharing options...
Re321 Posted April 22, 2010 Share Posted April 22, 2010 I used a thing similar on my PHPBB blog. Although the best way is just to create a plugin that will reserve the sql in an entry, but maybe that's a lot of entries. Quote Link to comment https://forums.phpfreaks.com/topic/199335-displaying-a-randomly-chosen-static-link/#findComment-1046215 Share on other sites More sharing options...
codergirl Posted April 22, 2010 Author Share Posted April 22, 2010 Thanks for the code, I'll test it in a couple of hours to see if everything works. It's 5am around here and I already start to have hallucinations and slight paranoia due to lack of sleep. I'll take a nap first. A plugin using mysql would probably be (on of) the best solution(s). What exactly did you use for your PHPBB blog? Quote Link to comment https://forums.phpfreaks.com/topic/199335-displaying-a-randomly-chosen-static-link/#findComment-1046221 Share on other sites More sharing options...
codergirl Posted April 23, 2010 Author Share Posted April 23, 2010 I still need help, as I don't fully understand the code. I placed th following lines of code in my theme's functions.php to test it: function externalurl_is() { $random = Array( 1 => '<a href="http://www.foo.com" title="foobar">Foo</a>' , 2 => '<a href="http://www.google.com" title="Google">Google</a>' , 3 => '<a href="http://www.noodle.com" title="Noodle">Noodle</a>' , 4 => '...' , 5 => 'And' , 6 => 'Some' , 7 => 'More' , 8 => 'Random' , 9 => 'Entries', 10 => 'Here' , 11 => 'And' , 12 => 'Here' ); //$_GET['id'] $postnumber = Array(332, 24, 62, 192, 52, 62, 12); foreach ($postnumber as $post) { $decide = str_split($post); $iterator = 0; foreach($decide as $add) { $iterator += $add; } print "Random: " . $random[$iterator] . '<br/>'; } } I then simply called the function in my theme's single.php, the file for single posts: <?php externalurl_is(); ?> Now it gives me as many posts as there are numbers specified here: $postnumber = Array(332, 24, 62, 192, 52, 62, 12); Well, and now I'm confused. How I do I make it assign only one randomly chosen link per post now? Do I need to use another variable like page_id? Do I need to edit the line with the array above? I seriously have no clue, I never even added an own function before. Quote Link to comment https://forums.phpfreaks.com/topic/199335-displaying-a-randomly-chosen-static-link/#findComment-1047111 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.