The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Help needed with update_batch();
Discuss Help needed with update_batch(); in the PHP Development forum on Dev Shed. Help needed with update_batch(); PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 22nd, 2013, 02:21 AM
|
 |
A Change of Season
|
|
|
|
|
Help needed with update_batch();
Hello;
I have a form with 200+ textareas and I need to create an array and batch_update so I update everything in one hit. The Active Record that I use works like this:
PHP Code:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('mytable', $data, 'title');
// Produces:
// UPDATE `mytable` SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name` END,
// `date` = CASE
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date` END
// WHERE `title` IN ('My title','Another title')
Can someone help me fix my form and so I can create the batch array?
This is my form:
Code:
<form action="http://localhost/systematicattraction/" method="post">
<ul>
<?php foreach ($items as $item):
?>
<li><?php echo $item['id'];?> <input name="id[]" checked="checked" type="checkbox" value="<?php echo $item['id'];?>" /> <textarea name="title[]"><?php echo $item['title'];?></textarea></li>
<?php endforeach; ?>
</ul>
<input type="submit" value="Assign" />
</form>
I tried a few things but I didn't succeed.
|

February 22nd, 2013, 04:05 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
automatic indexing with "name[]" won't get you very far, because it only creates simple arrays for each "name".
Make a counter and then use explictit indices:
PHP Code:
<?php
for ($i = 0; $i < 10; $i++)
echo '
<input type="text" name="test[' . html_escape($i) . '][foo]" />
<input type="text" name="test[' . html_escape($i) . '][bar]" />
<br />
';
|

February 22nd, 2013, 07:52 PM
|
 |
A Change of Season
|
|
|
|
Quote: | Originally Posted by Jacques1 Hi,
automatic indexing with "name[]" won't get you very far, because it only creates simple arrays for each "name".
Make a counter and then use explictit indices:
PHP Code:
<?php
for ($i = 0; $i < 10; $i++)
echo '
<input type="text" name="test[' . html_escape($i) . '][foo]" />
<input type="text" name="test[' . html_escape($i) . '][bar]" />
<br />
';
| Thanks all sorted but why is html_escape neccessary?
|

February 23rd, 2013, 04:51 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
In this particular case, it's not necessary. However, I'd generally escape all variables before echoing them, so that there's no risk of accidentally skipping a variable.
|

February 23rd, 2013, 02:59 PM
|
 |
A Change of Season
|
|
|
|
Quote: | Originally Posted by Jacques1 In this particular case, it's not necessary. However, I'd generally escape all variables before echoing them, so that there's no risk of accidentally skipping a variable. | How did you learn php? What do you recommend for a mid level programmer to do/study to become expert?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|