Page 1 of 1

Array problem

Posted: Sun Dec 13, 2009 1:45 pm
by st89
I have identified the following code as troublesome:

Code: Select all

public function add_button($value, $class) {
    $this->_content .= "<input type=\"submit\" value=\"".$value."\" onClick=\"ajax(".$class.", [";
    for ($i = 0; $this->_propertiesArray >= $i && $this->_propertiesArray <= $this->_propertiesCount; $i++) {
      $this->_content .= "\"".$this->_propertiesArray[$i]."\", ";
    }    
    $this->_content .= "]);\" />";
  }
The above is part of a larger class called form:

Code: Select all

class form {
  private $_id;
  private $_content;
  private $_propertiesCount = 0;
  private $_propertiesArray = array();
  private $_output;
 
  public function __construct($id) {
    $this->_id = $id;
    return(TRUE);
  }
 
  public function add_field($label, $name) {
    $this->_content .= "<label>".$label."</label><input type=\"text\" name=\"".$name."\" />";
    $this->_propertiesArray[$this->_propertiesCount] = $name;
    $this->_propertiesCount += 1;
  }
 
  public function add_button($value, $class) {
    $this->_content .= "<input type=\"submit\" value=\"".$value."\" onClick=\"ajax(".$class.", [";
    for ($i = 0; $this->_propertiesArray >= $i && $this->_propertiesArray <= $this->_propertiesCount; $i++) {
      $this->_content .= "\"".$this->_propertiesArray[$i]."\", ";
    }    
    $this->_content .= "]);\" />";
  }
  
  public function build() {
    $this->_output = "<form id=\"".$this->_id."\" action=\" \">";
    $this->_output .= $this->_content;
    $this->_output .= "</form>";
    return($this->_output);  
  }
 
  public function __destruct() {
    return(NULL);
  }  
}
An example of how form is used is below (from home.php):

Code: Select all

$form->add_field('First word', 'first');
$form->add_field('Second word', 'second');
$form->add_button('Click me', 'concatenate');
$result = $form->build();
echo $result;
Using the above example from home.php, I built the $form->add_button(); method to
  • 1) Add 'Click me' as the text on the submit button;
    2) Add 'concatenate' as the name of a parameter to use in my ajax() JavaScript function;
    3) Add to an array the names of all the fields in the form (in this case, 'first' and 'second');
    4) Output the contents of the array to a string in the form of:
    ["first", "second", ]
    5) With the result being HTML that looks like:
    <input type="submit" value="Click me" onClick="ajax(concatenate, ["first", "second", ]);" />
I would be grateful if someone has seen where I've gone wrong and is willing to share it with me. Thanks in advance for any help/tips/pointers :D

Re: Array problem

Posted: Sun Dec 13, 2009 2:10 pm
by AbraCadaver
st89 wrote:I would be grateful if someone has seen where I've gone wrong and is willing to share it with me. Thanks in advance for any help/tips/pointers :D
You haven't stated what is wrong, only what you expect.

Re: Array problem

Posted: Sun Dec 13, 2009 2:24 pm
by st89
AbraCadaver wrote:
st89 wrote:I would be grateful if someone has seen where I've gone wrong and is willing to share it with me. Thanks in advance for any help/tips/pointers :D
You haven't stated what is wrong, only what you expect.
Sorry, I should have made it clear. At the moment I get:

Code: Select all

<input type="submit" value="Click me" onClick="ajax(concatenate, []);">
When I want:

Code: Select all

<input type="submit" value="Click me" onClick="ajax(concatenate, ["first", "second", ]);" />

Re: Array problem

Posted: Sun Dec 13, 2009 2:26 pm
by AbraCadaver
Not sure if this will work, but your loop is kinda funny looking. foreach() can be your friend:

Code: Select all

public function add_button($value, $class) {
    $this->_content .= "<input type=\"submit\" value=\"".$value."\" onClick=\"ajax(".$class.", [";
    foreach($this->_propertiesArray as $property) {
      $this->_content .= "\"".$property."\", ";
    }    
    $this->_content .= "]);\" />";
}

Re: Array problem

Posted: Sun Dec 13, 2009 2:31 pm
by AbraCadaver
Also, you seem to be over complicating some things. It may be personal preference, but I would do this:

Code: Select all

public function add_field($label, $name) {
     $this->_content .= "<label>".$label."</label><input type=\"text\" name=\"".$name."\" />";
     $this->_propertiesArray[] = $name;
     // don't need this, use count($this->_propertiesArray) or a foreach(($this->_propertiesArray) when needed
     //$this->_propertiesCount += 1;
}

Re: Array problem

Posted: Sun Dec 13, 2009 2:40 pm
by AbraCadaver
Oh, and just a thought, but you don't even need the loop:

Code: Select all

public function add_button($value, $class) {
    $this->_content .= "<input type=\"submit\" value=\"".$value."\" onClick=\"ajax(".$class.", [\"".implode('","', $this->_propertiesArray)."\"]);\" />";
}
The important part being:

Code: Select all

implode('","', $this->_propertiesArray)

Re: Array problem

Posted: Sun Dec 13, 2009 3:37 pm
by st89
AbraCadaver wrote:Oh, and just a thought, but you don't even need the loop:

Code: Select all

public function add_button($value, $class) {
    $this->_content .= "<input type=\"submit\" value=\"".$value."\" onClick=\"ajax(".$class.", [\"".implode('","', $this->_propertiesArray)."\"]);\" />";
}
The important part being:

Code: Select all

implode('","', $this->_propertiesArray)
Thanks for the replies! It now works brilliantly when I call the methods. :D