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
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:
Well cool TY, just another quicky could you add a time zone? thanks for your help will check this code now
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):
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 >
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
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.
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
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?
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
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):
Thankyou, very useful will have a go with this code. Could automate the website for me daily. Thankyou
<?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
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.