Array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
st89
Forum Newbie
Posts: 17
Joined: Sat Nov 28, 2009 5:42 pm

Array problem

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array problem

Post 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.
Last edited by AbraCadaver on Sun Dec 13, 2009 2:25 pm, edited 2 times in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
st89
Forum Newbie
Posts: 17
Joined: Sat Nov 28, 2009 5:42 pm

Re: Array problem

Post 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", ]);" />
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array problem

Post 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 .= "]);\" />";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array problem

Post 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;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array problem

Post 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)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
st89
Forum Newbie
Posts: 17
Joined: Sat Nov 28, 2009 5:42 pm

Re: Array problem

Post 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
Post Reply