1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need PHP script to display random content

Discussion in 'PHP' started by Mr.Dog, Nov 23, 2014.

  1. #1
    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 :p But I can searched for ages, I couldn't find exactly the type of script I'd require, but a bunch of other scripts).
     
    Solved! View solution.
    Last edited: Nov 23, 2014
    Mr.Dog, Nov 23, 2014 IP
  2. #2
    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.
     
    matt_62, Nov 23, 2014 IP
    Mr.Dog likes this.
  3. jslirola

    jslirola Active Member

    Messages:
    39
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    58
    #3
    jslirola, Nov 23, 2014 IP
    Mr.Dog likes this.
  4. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #4
    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!
     
    Mr.Dog, Nov 26, 2014 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #5
    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.
     
    sarahk, Nov 26, 2014 IP
  6. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I am going to test and see what I understood and then will return with questions in a few days.
     
    Mr.Dog, Dec 1, 2014 IP
  7. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #7
    I think there is a little problem in the code. I think the INCLUDE should be:

    <?php include ("articles-01.php");?>
     
    Mr.Dog, Dec 2, 2014 IP
  8. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #8
    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:
     
    Last edited by a moderator: Dec 2, 2014
    Mr.Dog, Dec 2, 2014 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #9
    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.
     
    sarahk, Dec 2, 2014 IP
    Mr.Dog likes this.
  10. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #10
    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);
     
    Last edited: Dec 2, 2014
    Mr.Dog, Dec 2, 2014 IP
  11. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #11
    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.
     
    sarahk, Dec 2, 2014 IP
    Mr.Dog likes this.
  12. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #12
    I just posted before you... how much should I take out?

    I really appreciate your help ;)
     
    Mr.Dog, Dec 2, 2014 IP
  13. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #13
    OK, I just removed this:
    var_dump($filename);

    And it seems like it's working :)
     
    Mr.Dog, Dec 2, 2014 IP
  14. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    43
    #14
    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.
     
    freelanceDeveloper, Dec 9, 2014 IP
  15. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #15
    sarahk, thank you again!

    I will contact you or post question if I have more issues regarding this :)
     
    Mr.Dog, Dec 16, 2014 IP
  16. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #16
    random () function and then a list of websites /pages to randomly select
    easiest way
     
    ezprint2008, Jan 12, 2015 IP