Please consider the following class :

<?php
class p0110dbinsertbuilder {
  public $table="";
  private $fields ;
  public function add($Field,$Value){
    $this->fields = array($Field,$Value);
  }
  public function insertstring(){
    $count = count($this->fields,0);
    $c = "INSERT INTO " . $this->table . "(";
    for ($i=0;$i<$count;$i++){
      $c .= $this->fields[$i][0];
      if ($i <> $count-1){
        $c .= ",";
      }
    }
    $c .= " ) VALUES ( ";
    for ($i=0;$i<$count;$i++){
      $c .= $this->fields[$i][1];
      if ($i <> $count-1){
        $c .= ",";
      }
    }
    $c .= ")";
    return $c;
  }
}
?>

and calling it as follows:

  $o = new p0110dbinsertbuilder();
  $o->table = "suppliers";
  $o->add("supplier_name",'"SuppplierName"');
  echo $o->insertstring();

I have 2 questions :

1) why do I does count($this->fields,0) return 2, whereas I would expect only 1.
2) why does $o->insertstring();return

INSERT INTO suppliers(s," ) VALUES ( u,S)

The add function should be :

 public function add($field,$value){
    $this->fields[] = array($field,$value);
  }

The '[]' are were missing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.