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.

PHP changing content in one php page simply?

Discussion in 'PHP' started by mrbrown123, Nov 10, 2015.

  1. #1
    Hi I have a page in very basic php. I want to change a file either by random or by time of day or whatever is possible here is the code
    <?
    
    include dirname(__FILE__)."/templates/header.php";
    
    include dirname(__FILE__)."/templates/content.php";
    
    include dirname(__FILE__)."/templates/footer.php";
    
    ?>
    Code (markup):
    I want to random change the content.php to say content2.php within that page. Is it possible

    The idea is for displaying a videoplayer change

    thanks
     
    mrbrown123, Nov 10, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Of course it's possible. It won't be dynamic, however - it will involve the user actually reloading the page to see the change.
    To change it randomly (so that anyone loading the page will either get the content.php or the content2.php) you can do something like this:
    
    <?php
    
    include dirname(__FILE__)."/templates/header.php";
    
    $files = ['content.php','content2.php'];
    $get_file = array_rand($files);
    
    include dirname(__FILE__)."/templates/".$files[$get_file];
    
    include dirname(__FILE__)."/templates/footer.php";
    
    ?>
    
    PHP:
    If you want to base it on time of day, you can do something like this:
    
    <?php
    
    include dirname(__FILE__)."/templates/header.php";
    
    $time = new DateTime(now);
    
    if ($time->format('H:m') < '18:00') {
       include dirname(__FILE__)."/templates/content.php";
    } else {
       include dirname(__FILE__)."/templates/content2.php";
    }
    
    include dirname(__FILE__)."/templates/footer.php";
    
    ?>
    
    PHP:
     
    PoPSiCLe, Nov 10, 2015 IP
  3. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Well cool TY, just another quicky could you add a time zone? thanks for your help will check this code now
     
    mrbrown123, Nov 10, 2015 IP
  4. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #4
    For the first code i get a error, the second doesnt seem to do anything. hmm will carry on trying
    Parse error: syntax error, unexpected '[' in  on line 5
    Code (markup):
     
    mrbrown123, Nov 10, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    That means you have an old version of PHP.

    Change this line:
    $files = ['content.php','content2.php'];
    to
    $files = array('content.php','content2.php');

    The second one will change at 6 pm. If you want to see it working, change the caret: < or >
     
    PoPSiCLe, Nov 10, 2015 IP
  6. artexcreative

    artexcreative Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    Before you call the DateTime function you can use the 'date_default_timezone_set' function to set your default timezone. I believe that you have to be using PHP 5.1 or higher to use this. http://php.net/manual/en/function.date-default-timezone-set.php

    date_default_timezone_set('America/New_York');
    PHP:
    Time zones can be found here for the above function: http://php.net/manual/en/timezones.php
     
    artexcreative, Nov 17, 2015 IP
  7. Parth01

    Parth01 Greenhorn

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #7
    I want to change a file either by haphazard or by time of day or whatever is possible here is the code Code (markup): I want to random change the content.php to say content2.php within that page.

    Code (markup):

    I want to random change the content.php to say content2.php within that page. Is it possible

    <?php
    echo "Hello World";
    echo "\n";
    ?>

    Alternatively you can also concatenate "\n" to the last echo/print in the block.

    <?php
    echo "Hello World" . "\n";
    ?>

    Note: the string "\n" must be inside double quotes; using single quotes will output the literal string.

    ---
    2. Add two line feeds after the closing ?> tag.

    By explicitly adding two line feeds after the closing ?> tag, the first one will be removed by php, and the second one will appear in the output.

    ---
    One advantage of both approaches is that you don't need to add a space after the closing ?> tag
    (to force a space and a line feed to be output), instead, only the line feed is output.
     
    Parth01, Nov 24, 2015 IP
  8. AZ Website Solution

    AZ Website Solution Banned

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #8
    of course you can chenge the pages without reloading page I can suggest with example of changing the pages. You can take the variables and calling the function so that

    for this examples

    <?

    include dirname(__FILE__)."/templates/header.php";

    include dirname(__FILE__)."/templates/content.php";

    $id ='1';

    function recp( $rId )
    {
    $id = $rId;
    }
    ?>

    <a href="#" onClick="<?php recp('1')?>" > One </a>
    <a href="#" onClick="<?php recp('2')?>" > Two </a>
    <a href="#" onClick="<?php recp('3')?>" > Three </a>

    <divid='myStyle'>
    <?php
    require('myConnect.php');
    $results = mysql_query("SELECT para FROM content WHERE para_ID='$id'");

    if( mysql_num_rows($results)>0)

    {
    $row = mysql_fetch_array( $results );
    echo $row['para'];
    }

    ?>
    </div>

    This code is very simple and changing the pages and see the data.Look this sample code and get the idea for your programm
     
    AZ Website Solution, Nov 24, 2015 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Hurrah. Another useless attempt at making the Web more vulnerable, even to add to a thread that's been solved already. Would you please avoid providing examples that allows for SQL injection?
     
    PoPSiCLe, Nov 25, 2015 IP
  10. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #10
    Thanks this is very helpful,

    my last question. If I wanted to show different content over 7 days? would this be possible?

    for example

    Mon show content 1 php
    tues show content 2 php

    and so on, do the same princibles appy?

    many thanks for your time
     
    mrbrown123, Nov 30, 2015 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    Yes, what you would have to do is format this line:
    
    if ($time->format('H:m') < '18:00') {
    
    Code (markup):
    to something like this:
    
    if (strtolower($time->format('D')) == 'mon') {
    //include code goes here
    } elseif (strtolower($time->format('D')) == 'tue') {
    //and so forth
    
    Code (markup):
    That is the simple solution - however, you might wanna change the code to something a little more efficient:
    
    $currentday = strtolower($time->format('D'));
    switch ($currentday) {
       case 'mon':
       include 'content.php';
       break;
       case 'tue':
       include 'content2.php';
       break;
       //and so on
    }
    
    Code (markup):
     
    PoPSiCLe, Nov 30, 2015 IP
  12. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #12
    Thankyou, very useful will have a go with this code. Could automate the website for me daily. Thankyou
     
    mrbrown123, Nov 30, 2015 IP
  13. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #13
    <?php
    
    include dirname(__FILE__)."/templates/header.php";
    
    $currentday = strtolower($time->format('D'));
    switch ($currentday) {
       case 'mon':
       include 'content.php';
       break;
       case 'tue':
       include 'content2.php';
       break;
      
    }
    include dirname(__FILE__)."/templates/footer.php";
    
    ?>
    Code (markup):
    Getting a error on line 5. what am I doing wrong here? or am I using out dated php? thanks
     
    mrbrown123, Dec 1, 2015 IP
  14. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #14
    You're missing the assignment of the $time variable, for a start. Look at the first code I posted, and use that, just change the if/else.
     
    PoPSiCLe, Dec 1, 2015 IP