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