SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2000
    Location
    Washington, DC
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating/adding several entries at the same time

    I'm writing a script that has products and potentially several ads per product.

    I'm trying to make a page that will make it easy to create an ad for each product and it's got me stumped.

    I'd like to pull all the products out and have all of the appropriate ad fields with the corresponding product, and a checkbox. This is straight forward and I've got that working.

    If you want to add an ad, you fill out the ad fields and the check the Update checkbox.

    The page that receives the submission should add/update any ads that were checked. A forum search showed that foreach(...) is a common way of checking to see if the checkbox was checked.

    Is it possible to pass all ad variables with the checkbox so that I can make a new entry containing the info from the ad fields?

    Either way, what's the best way to accomplish this?

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you give us some example form markup (or better your code)?

  3. #3
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it possible to pass all ad variables with the checkbox so that I can make a new entry containing the info from the ad fields?
    Not 100% sure I understand what your asking but here goes...

    I think theres 2 ways if I understand you.
    1. if you name the check boxes "update[num]" then you could name the input fields "update[num][input1]" etc. Problem with this is that cause text fields submit as empty strings if they arent filled in then your check boxes are useless.

    2. make the value of the ckeckboxes like "ad1", and then name your input (text) fields "ad1[num]" so using your foreach statment on the checkbox fields tells you which of the text fields to use, and you ignore the rest.
    PHP Code:
    <input type="checkbox" name="update[]" value="ad1">Update 1
    <input type="text" name="ad1[]" value="">
    <
    input type="text" name="ad1[]" value="">
    <
    input type="checkbox" name="update[]" value="ad2">Update 2
    <input type="text" name="ad2[]" value="">
    <
    input type="text" name="ad2[]" value=""
    Is that helpful?
    Erh

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2000
    Location
    Washington, DC
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm away from my coding computer so I'll have to post an example and not the actual code.

    Here's some code off the fly that is more or less the system I'm working on:
    PHP Code:
    <form method="post" action="processpage.php">
    <table>
    <tr>
       <td>Product Name</td>
       <td>Ad Redirect URL</td>
       <td>Other Ad Specific Data</td>
       <td>Update?</td>
    </tr>
       
    <?
      sql 
    query("SELECT id, name FROM products");
      while(
    $row fetchArray($sql)){
       
    $prodid $row['id'];
       
    $prodname $row['name'];
      echo 
    "
     <tr>
        <td>
    $prodname</td>
        <td><input type='text' name='url' class='form' size='3'></td>
        <td><input type='text' name='morevars' class='form' size='3'></td>
        <td>
             <input type='hidden' name='pid' class='form' value='
    $prodid'>
             <input type='checkbox' name='update[]' value='1'>
        </td>
     </tr>
      "
    ;
      }
     <
    tr>
        <
    td colspan='4'>
          <
    input type='submit' name='Submit' value='update'>
         <
    td>
     </
    tr>
    ?>
    </table>
    </form>
    processpage.php should recognize that the checkbox was checked and then add the other data associated with that checkbox.

  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // The modified loop
    while ($row mysql_fetch_assoc($sql)){
        
    $prodid $row['id'];
        
    $prodname $row['name'];
        echo 
    "
            <tr>
                <td>
    $prodname</td>
                <td><input type='text' name='url[
    $prodid]' class='form' size='3'></td>
                <td><input type='text' name='morevars[
    $prodid]' class='form' size='3'></td>
                <td><input type='checkbox' name='update[]' value='
    $prodid'></td>
            </tr>
        "
    ;
    }

    // Proccessing
    if (isset($_POST['update']) && is_array($_POST['update'])) {
        
    $msg = array();
        foreach (
    $_POST['update'] as $prodid) {
            if (
    is_numeric($prodid)) {
                
    $url = @$_POST['url'][$prodid];
                
    $morevars = @$_POST['morevars'][$prodid];
                
    $sql "UPDATE 
                            products 
                        SET 
                              `url` = '
    $url'
                            , `morevars` = '
    $morevars'
                        WHERE
                            `id` = 
    $prodid;";
                
    $result mysql_query($sql); or die(mysql_error());
                if (
    mysql_affected_rows()) {
                    
    $msg[] = "Product ($prodid) updated";
                }
            }
        }
        if (
    size($msg)) {
            echo 
    '<h3>'.size($msg).' products updated</h3>';
            echo 
    '<ul>';
            foreach (
    $msg as $message)
                echo 
    '<li>'.$message.'</li>';
            echo 
    '</ul>';
        }


  6. #6
    SitePoint Zealot
    Join Date
    Aug 2000
    Location
    Washington, DC
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh.. yes! That looks good. I'll give that a shot as soon as I get home - thanks for your time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •