Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Edit HTML file with PHP dynamically, and appending current link/html to latest video in html code automatically. (Dynamic HTML)

php html dynamic manipulation automatically

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1 elliottveares

elliottveares

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 08 November 2012 - 02:15 PM


I have got some batch scripts what makes timelapse videos at the end of every day and names them like "Timelapse_dd_mm_yyyy.mp4".

I now want to edit a basic and static html file automatically every night and add/append some html code on a certain line for a link to the latest time lapse video.

So every night the html files grows slightly larger.

How do I do this with PHP so it adds the relevant html code for the time lapse video at a certain time every night. The HTML content also needs to have the current file names of all the videos.


So for example, the following links would be added as so.

Time lapse 01/12/12 (Click to download) - Would be added on the first of December

Time lapse 02/12/12 (Click to download) - Would be added on the second of December

Time lapse 03/12/12 (Click to download) - Would be added on the third of December


Finally videos would be in "/time_lapse_videos" and html file would be in current directory


#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 08 November 2012 - 02:35 PM

The way I would implement this is by using PHP's Document Object Model capabilities to read in the HTML, add a new node, and save out the new HTML.

I believe it'll look something like this: (barring any possible mistakes I've made--I haven't tested the code below.)
$doc = new DOMDocument();
$doc->loadHTMLFile("path/to/file.html");
$body = $doc->getElementsByTagName('body').item(0);
$link = $doc->createElement('a');
$newLink = $body->appendChild($link);
$newLink->setAttribute("href", "path/to/link/to.mp4");
$linkText = $doc->createTextNode("Display Text For Link");
$newLink->appendChild($linkText);
$doc->saveHTMLFile("path/to/file.html");

Relevant PHP documentation pages:
http://www.php.net/manual/en/domdocument.loadhtmlfile.php
http://www.php.net/manual/en/domdocument.savehtmlfile.php
http://www.php.net/manual/en/domdocument.getelementsbytagname.php
http://www.php.net/manual/en/domdocument.createelement.php
http://www.php.net/m...appendchild.php
http://www.php.net/m...etattribute.php
http://www.php.net/m...etattribute.php
http://www.php.net/manual/en/domdocument.createtextnode.php

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 09 November 2012 - 01:15 AM

Greg gave you an elegant solution. My question is: do you have the file list in a database ?

Another way to do it could be, if you already now the set of links of the whole month, to include all the links in the html, and then, with javascript, tag them as hidden if their date is > than current. That way the javascript will be the link's availability manager

#4 elliottveares

elliottveares

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 15 November 2012 - 02:01 PM

Firstly, sorry for the late reply.

Thanks gregwarner, but dose not seem to be working properly.

Here is my code:


$doc = new DOMDocument();
$doc->loadHTMLFile("timelapse.html");
$body = $doc->getElementsByTagName('body').item(0);
$link = $doc->createElement('a');
$newLink = $body->appendChild($link);
$newLink->setAttribute("href", "Time_Lapse_01/12/2012.mp4");
$linkText = $doc->CreateTextNode("Time Lapse - 01/12/12");
$newLink->appendChild($linkText);
$doc->saveHTMLFile("timelapse.html");

a number of errors keep occurring, including a errors relating to the lines:

$body = $doc->getElementsByTagName('body').item(0);
$newLink = $body->appendChild($link);

If you can please sort out what is wrong, i would much appreciate it.

Thankyou

#5 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 15 November 2012 - 02:05 PM

a number of errors keep occurring,


A listing of those error messages would be helpful to us for figuring out what's wrong.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#6 elliottveares

elliottveares

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 15 November 2012 - 02:21 PM

Ok, will post them here in the next couple of days with the specifics and details.

#7 elliottveares

elliottveares

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 16 November 2012 - 10:02 AM

Here is the details - First the PHP Script called "timelapse.php" (please note although i have basic experience with PHP, i have no experience with DomDocument!)

<?php

error_reporting(E_ALL);

[color=#008000]	//Ensures HTML file is not written to if "Time_Lapse_01_12_2012" allready exist - Disabling this by comenting does not help.[/color]

$pattern = "Time_Lapse_01/12/2012";									
$file = readfile('timelapse.html');
if (preg_match('#' . $pattern . '#', $file, $match))

{}

else {

[color=#008000]   // Ensures below code only executes on or after 01/11/12 (Not using 01/12/12 so that following code is executed ) - Disabling this by comenting does not help.  [/color]

date_default_timezone_set('Europe/London');	  
$date = date('d/m/y');
if ($date >= 01/11/12)


{

$doc = new DOMDocument();														  [color=#008000]  // This section of code not working[/color]
$doc->loadHTMLFile("timelapse.html");				
[color=#0000ff]$body = $doc->getElementsByTagName('body').item(0);[/color]
$link = $doc->createElement('a');
[color=#800080]$newLink = $body->appendChild($link);[/color]
$newLink->setAttribute("href", "Time_Lapse_01/12/2012.mp4");
$linkText = $doc->CreateTextNode("Time Lapse - 01/12/12");
$newLink->appendChild($linkText);
$doc->saveHTMLFile("timelapse.html");


}

}


?>
Here is the PHP errors



Code above gives: Fatal error: Call to undefined function item() in C:\Apache24\htdocs\timelapse.php on line 22 (Blue Line)

IF i remove ".item(0)" from line 22 the above fatal error goes away but I get:


Fatal error: Call to undefined method DOMNodeList::appendChild() in C:\Apache24\htdocs\timelapse.php on line 24 (Purple Line)




Here is the HTML file - timelapse.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[url="http://www.w3.org/TR/html4/loose.dtd%22>"]http://www.w3.org/TR...ml4/loose.dtd">[/url]
<html>
<head>
<style type="text/css">

h4 {text-align:right}

BODY	 {color:#000080; background-color:#99D6FF;}


body {cursor:url(cur1.cur),url(cur2.ani),auto;}

head {cursor:url(cur1.cur),url(cur2.ani),auto;}

a {
cursor:url(tree.cur),url(cur1.cur),auto;
}



.footer  
{

	 text-align: center;
	 position: relative;
padding-top 5px;
	 bottom: 5%;
	 left: 0px;
	 width: 100%;
		 }

</style>
<title>Time Lapse's</title>
<?php require("timelapse.php"); ?>
</head>

<body>

</body>
</html>


Any help much appreciated: Elliott V :biggrin:

Edited by BenW, 18 November 2012 - 11:16 AM.
Added code tags


#8 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 16 November 2012 - 10:21 AM

I'm an idiot.
.item(0);
needs to be:
->item(0);

(PHP doesn't use the dot as the member access operator. My brain was still in Java mode.)

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#9 elliottveares

elliottveares

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 17 November 2012 - 01:47 PM

Thanks, gregwarner - Much better solution than using Javascript what was open to all sorts of potential issues.

#10 Colanth

Colanth

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 165 posts

Posted 04 December 2012 - 02:00 PM

I don't know why you're not just creating the entire HTML file in the PHP code, adding what you need every day. Remember,

php stuff
?>
<a href="Time_Lapse_01/12/2012.mp4">Time Lapse - 01/12/12</a>
<?php
more php code

will work just fine.




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download