Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Question 1

I am currently working on a project where I am trying to retrieve values from a table in a SQL database and insert them into a webpage. This is all on the server side, Linux. The only unique value is the ID, and depending on the number of the ID the webpage will change accordingly.

For example:

ID: 1, Color: Red.

ID: 2, Color: Blue.

If the ID value 1 is retrieved, the background color will change to red. And if ID 2 is retrieved, the background color is blue.

The question is, is there a way to trigger this background color change using PHP and/or Javascript?

Question2

Now, when question 1 is answered, we have our webpage background color changed to a desired color. The next thing would be to take an image of our newly changed website. This will be done with PhantomJS (later on this will be made into a video, but that problem is solved).

Is it possible to make all of this automatized? So that when the webpage has changed background color, the Go.php will start running?

Thanks in advance!


Relevant to Question 1

Retrieves data from database

Takeinfo.php

<?php
$dbhost = 'localhost';
$dbuser = 'User';
$dbpass = 'Password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT id, color FROM thevariables;

mysql_select_db(TheDatabse);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "ID :{$row['id']}  <br> ".
         "Color :{$row['color']}  <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

Relevant to Question 2

Go.php will initiate run.sh, which in turn starts PhantomJS and proceeds to run runscript.js.

go.php

<?php 
echo shell_exec('sh gogo.sh');
 ?>

run.sh

phantomjs runscript.js

runscript.js

var page = require('webpage').create();
page.viewportSize = { width: 640, height: 480 };

page.open('index.html', function () {
  setTimeout(function() {
    page.render('TheScreenshot.png', { format: "png" });
    phantom.exit();
  }, 666);
});
share|improve this question
1  
First thing I'm noticing: $dbuser = 'User;, you're missing the close quote. I assume that was a quick edit to change your username/password out for the question, but it's screwing up the colors. Also, I'm not seeing an actual question on question #1. –  user2010925 Feb 20 at 9:11
    
I think your (primary) question is: can a colour change in a web page trigger a call to something? The short answer is, not automatically. You have two options: when the colour is changed by you programmatically, also set a flag to run PhantomJS. Or, you can run PhantomJS every X minutes, so the screenshot is only ever X minutes out of date. –  halfer Feb 20 at 9:18
    
@Danbopes thanks for noticing the errors I made. The code should properly work now and the question is added to question 1. –  Plankkan Feb 20 at 9:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.