0

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)

1
Contributor
1
Reply
15
Views
4 Years
Discussion Span
Last Post by PerplexedB
1

The add function should be :

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

The '[]' are were missing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.