Hi, I think this is simple, but I don't know PHP. I searched a lot on Google and couldn't find. Don't know how these are called... It would work like this: *random content (with links to articles) should appear on a sidebar "box" *I will simply have an INCLUDE code for the PHP script sitewide and the script will be pulled from an external PHP file *that PHP file should be able to display random basic HTML content: images, text and links to my articles *what the user will see will actually be a snippet/preview box of related articles Perhaps the INCLUDED PHP script itself could pull the content from other PHP scripts, which contain the content to be displayed: content-01.php content-02.php content-03.php ...etc. And these are just rotated sitewide... You get the idea. How/where can I get such a script? (Hotscripts, yeah, I know But I can searched for ages, I couldn't find exactly the type of script I'd require, but a bunch of other scripts).
The php solution might not give you everything that you want. But I can give you a quick php hack that is essentially 2 lines of code. The first line creates a random number. The only thing to adjust here is how many articles you have. Lets say today you have 10. so you want it to pick a random number between 1 and 10. Next week say you have 20, but already displayed the first 10 alot, so you adjust the values so it displays articles randomly from 11 to 20. the second line of code calls the included file. <?php $article_number = rand(1,10); // first number in the brackets is the min number. the other is the max include 'basicarticlename' . $article_number . 'php'; // pulls the files into your site page ?> note: depending on your file structure, you may have difficulties. If all your articles are in a subfolder, you might have to add the folder name in front of 'basicarticlename'. Perhaps 'articles/basicarticlename' would be more suitable. Code (markup): Now I might save this as "article_generator.php" and for each page you want that script to run on, use: <?php include 'article_generator.php"; ?> Code (markup): and that should be all that is needed for the quick php hack.
Check this: http://www.webhostingtalk.com/showpost.php?s=12d3dd0dda5459ce019bcc2acd20e1cb&p=2029718&postcount=4 Could be useful, if this function work with your idea, then you need make the code of structure main of script. Example: <html> <head> <meta charset="utf-8"> <title>Title</title> </head> <body> <div id="content"> <div id="main"></div> <div id="sidebar"> <?php displayAaPHPrandomPage("directory", ".php"); // Function call ?> </div> </div> </body> </html> Code (markup):
I am going to try this out, thanks for helping. One question first - suppose I have 10 articles. I think I should enumerate those article file names somewhere, right? But how? I am not sure this would be correct... include 'article01.php', 'article02.php', 'article03.php' . $article_number . 'php'; I am not sure I understood this thing (how it goes from 1 to "n"). I remember I set something like that in Javascript, but I had to enumerate all file names there. I appreciate your help!
No in article_generator.php you'd have an array like this: $articles = array(15,19,26,28,33,45,61,67); $key = array_rand($articles); echo "<a href='/article.php?id={$articles[$key]}'>Read a random article</a>"; Code (markup): or you could go one step further and have $articles = array(15=>'Cats go mad',19=>'Cat Epic Fails',26=>'Kittens galore',28=>'Kittens at play',33=>'Cat versus Dog',45=>'Cat Compilation',61=>'Cute Kittens',67=>'Cat versus Bear'); $key = array_rand($articles); echo "<a href='/article.php?id={$key}'>{$articles[$key]}</a>"; Code (markup): The next step is to have a way of identifying the articles you want included in the random selection in the database and building the initial $articles based on that criteria.
I think there is a little problem in the code. I think the INCLUDE should be: <?php include ("articles-01.php");?>
Code is not working. I tried this: Placed this into the .php file, I have 4 articles and it should display inside a DIV: <?php $article_number = rand(1,4); // first number in the brackets is the min number. the other is the max include '../myarticle' . $article_number . 'php'; // pulls the files into your site page ?> PHP: The page on which the articles will be displayed is just 1 folder below the root, so I used "../". The articles are called: myarticle01.php, myarticle02.php, myarticle03.php, myarticle04.php On the host page I display this INCLUDE code: <?php include ("../feeder-01.php");?> PHP: Nothing is appearing The articles themselves are .php, look something like this: <?php echo ' <img src="../image.png" alt="Image" align="right"/> <p>bla bla bla </p> '; ?> PHP:
Lets start here. Your articles have 2 digits for the number but you are only building one into the filename. The file extension didn't include the fullstop. When you don't get what you expect echo/print/var_dump out the value of variables to see if your assumptions are correct. <?php $article_number = rand(1,4); // first number in the brackets is the min number. the other is the max include '../myarticle' . str_pad($article_number,2,'0', STR_PAD_LEFT) . '.php'; // pulls the files into your site page ?> PHP: so to debug you might have changed it to this <?php $article_number = rand(1,4); // first number in the brackets is the min number. the other is the max $filename = '../myarticle' . str_pad($article_number,2,'0', STR_PAD_LEFT) . '.php'; var_dump($filename); include $filename; // pulls the files into your site page ?> PHP: You don't need to use the brackets in an include statement.
It partially works. The bad thing is, above it appears this: string(20) "../myarticle02.php" I always use the () brackets with the INCLUDE statement, because the Notepad++ I'm using seems to require that. Normally, when a code is incorrect, it doesn't appear in colour, so I had to add the brackets. I think this part of the code displays that string(20) thing and I'm afraid I will either remove too much or too little of this code: $filename = '../mytrips' . str_pad($article_number,2,'0', STR_PAD_LEFT) . '.php'; var_dump($filename);
that's the var_dump showing you what the string is. Once it's right and the include works you can take that out. It's all well and good getting code snippets from us but if you aren't reading them line by line and finding out what the commands do and why we've recommended them then you'll be no further ahead with your next change you need to make.
Isn't it an option to fe : include('customsidebar.php'); and inside that customsidebar.php file establish a database connection to get the links, images or whatever from a database.