#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    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'];?>&nbsp;&nbsp;<input name="id[]" checked="checked" type="checkbox" value="<?php echo $item['id'];?>" />&nbsp;<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.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    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 />
        '
    ;
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    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?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    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.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    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?

IMN logo majestic logo threadwatch logo seochat tools logo