For more about this example, check out the PHP Tutorial.

PHP Source: loadstrings.php.txt

Data file: saved.txt

Sample queries:

http://www.learningprocessing.com/php/loadstrings.php?type=clear

http://www.learningprocessing.com/php/loadstrings.php?type=load

http://www.learningprocessing.com/php/loadstrings.php?type=save&x=150&y=32

To run this example on your own server, you may need to set your file permissions for the text file to world readable and writable (chmod 777 filename.txt).

Thank you to Shawn Van Every, this example is based on code from his quick and dirty PHP tutorial.

Example
// Learning Processing
// http://www.learningprocessing.com
// Daniel Shiffman

// Example demonstrating how to use PHP for persistence
// Goes along with PHP tutorial
// http://www.learningprocessing.com/tutorials/php/

void setup() {
  size(600,200);
  background(255);
  smooth();
  loadData();
}

void draw() {
  noLoop();
}

void mousePressed() {
  String[] lines = loadStrings("http://www.learningprocessing.com/php/loadstrings.php?type=save&x=" + mouseX + "&y="+mouseY);
  println(lines[0]);
  loadData();
  redraw();
}

void keyPressed() {
  String[] lines = loadStrings("http://www.learningprocessing.com/php/loadstrings.php?type=clear");
  println(lines[0]);
  loadData();
  redraw();
  
}

void loadData() {
  background(255);
  String[] lines = loadStrings("http://www.learningprocessing.com/php/loadstrings.php?type=load");
  beginShape();
  stroke(0);
  noFill();
  for (int i = 0; i < lines.length; i++) {
    int[] vals = int(split(lines[i],","));
    if (vals.length > 1) {
      vertex(vals[0],vals[1]);
    }
  }
  endShape(CLOSE);
  for (int i = 0; i < lines.length; i++) {
    int[] vals = int(split(lines[i],","));
    if (vals.length > 1) {
      rectMode(CENTER);
      stroke(0);
      fill(175);
      rect(vals[0],vals[1],10,10);
    }
  }
}
  • PKHG

    Indeed, very nice, and the example works (18-2-2010)
    Peter

  • Marianoferle

    copie el mismo ejemplo y me funciona mas lento.

    estoy usando Mysql en 000webhost, sabras algo al respecto??
    gracias

  • helmut applebaum

    Hello,

    I’m using Wamp as a local host, and I have been getting processing sketches and csv data to appear in my web browser easily, but had trouble saving data to csv files, so I thought I would try this code example as a baseline (Thanks!);

    Your code works for me in his webpage, so I copied all the files to test if it would work on my windows machine, but I obviously have some permissions problem or am missing a file, as I cannot get the points to appear, even if I type them in.

    So now I wonder where the actual permission or saving problem is. The txt and csv files I use seem to have full write permissions for all users. I wonder if anyone has experience of this or suggestions?

    Using;
    Processing 2.0 – javascript export
    Windows 7 64
    Chrome
    Wamp server 2.4

  • helmut applebaum

    Ouch, I hadn’t changed the address!

    Now to see why my code doesn’t work :)