How to insert array to mysql one word one row?
I am using Zend Framework.
I would like to insert array $color = array('red', 'blue', 'green', 'yellow');
into mysql, one color to one row.
How do I code this in my model file?
Controller
public function addAction()
{
$this->view->Title = "Add Colors";
$this->view->headTitle($this->view->title, 'PREPEND');
$form = new Form_Color();
$form->submit->setLabel('Add');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
$color = $form->getValue('color');
$colors = new Model_DbTable_Colors();
$colors->addColor($color);
$this->_redirect('/');
} else {
$form->populate($formData);
}
}
}
Model
public function addColor($color)
{
$data = explode("\n&qu开发者_StackOverflow社区ot;,$color);
// How to coding for insert one color one row ????
}
thank you for your time.
You could do a foreach() on the array that gives you explode() and then foreach item issue a Add command on your DbTable model.
if $color is an array you can do a foreach loop
public function addColor($color) {
foreach($color as $color_key=>$color_data) {
// $color_data is the value like red, green, blue
// $color_key is the index of the array that holds the color
echo "The array index is: ".$color_key." for Color: ".$color_data."<br />\n";
}
}
精彩评论