SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    remove empty values from an array

    I have been using this instructional from the Wrox Book PHP Programing as the basis for my own much larger form. This first part generates 3 forms with first and last names fields as arrays. My form does the same except has about 30 or so rows and 5 array fields.


    ?>
    <FORM ACTION="submit.php" METHOD=POST>
    <? for($i = 0; $i <= $names; $i++): ?>

    First name:
    <INPUT NAME= "first[]" TYPE =TEXT><BR>

    Last name:
    <INPUT NAME= "last[]" TYPE = TEXT><br>

    <? endfor ?>

    <BR><INPUT TYPE=SUBMIT>
    </FORM>

    The submit page code is below


    PHP Code:
    <?php
        $num_rows 
    count ($first);
        for(
    $i 0$i <= $names$i++) {
        
    $sql "INSERT INTO Names ('First',  'Last')" .
        
    "VALUES ('$first[$i]', '$last[$i]')";

    //insert statement here
    ?>
    The problem is when I iterate through my form using the above code, every row including the ones that were not filled in also get inserted into the database

    I assume that the same thing is going to happen with the above example that I am trying to use to learn from. What if only the first form is filled in, is it then okay to have two extra empty rows added to the database?

    there must be a simple solution for this? someway to discard empty rows.


    Thanks for your help

  2. #2
    As the name suggests... trickie's Avatar
    Join Date
    Jul 2002
    Location
    Melbourne, Australia
    Posts
    678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    if((isset($_POST['first'][$i]) && isset($_POST['last'][$i])) &&  ($_POST['first'][$i] != '' && $_POST['last'][$i] != ''))
    {
    //then do insert query
    }
    ?>

  3. #3
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks trickie, I will give that a go

  4. #4
    As the name suggests... trickie's Avatar
    Join Date
    Jul 2002
    Location
    Melbourne, Australia
    Posts
    678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code is assuming that both values must be present in order for that iteration through the array to be entered into the db.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I gathered that. In my form located here
    http://www.mantaproductions.com/arro.../testtable.php
    each row has a checkbox which must be checked if any of the fields on that row will have a value. Empty values in the row is cool, I just want to avoid all those empty rows where the checkbox has not been checked. I am trying to use that code you posted on the checkbox variable only but Im getting mixed up on where exactly I should put it. Here is the code the way I have it now but it still inserts every row even the ones the checkbox has not been checked on



    cheers
    PHP Code:
    <?php
        $svol
    $_POST['savol'];
        
    $scom$_POST['sacom'];
        
    $ucst$_POST['unitcst'];
        
    $ovol$_POST['opvol'];
        
    $ocom$_POST['opcom'];
        
        for(
    $i 0$i 40 ; ++$i) {
        if(isset(
    $_POST['uo'][$i]) && ($_POST['uo'][$i] != '')) {
        
        
    $sql"INSERT INTO workorder SET
        unitid = '
    $uo[$i]',
        salesvol = '
    $svol[$i]',
        salescom = '
    $scom[$i]',
        unitcost = '
    $ucst[$i]',
        opervol = '
    $ovol[$i]',
        opercom = '
    $ocom[$i]' ";
        }
         
        if(@
    mysql_query($sql)) {
        echo(
    "Data Inserted");
        }
    }
    ?>
    Last edited by whiterabbit; May 26, 2003 at 21:51.

  6. #6
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh gees...never mind, I see that bracket needs to go down below the mysql_query.

    sheesh. Thanks a mill!! its working like its supposed to now.

  7. #7
    SitePoint Enthusiast Powerlord's Avatar
    Join Date
    May 2003
    Location
    Mason, MI, USA
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to consider running a while/each or foreach query on one of the arrays, then using the key from that to reference the other arrays value(s).

    For reference, a while/each loop for a one-dimensional array ($array['key'] = 'value') will look like this:
    PHP Code:
    while (list($key$value) = each($array)) {
        
    // Perform other checks and do things here

    Update: For reference, a foreach loop (including the key value) for one-dimensional array ($array['key'] = 'value') will look like this:

    PHP Code:
    foreach ($array as $key => $value) {
        
    // Perform other checks and do things here

    Ross Bemrose,
    Independant MySQL/Perl/PHP Developer

  8. #8
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Powerlord, I am going to study your code. I think the code I am using will work for now (translated: I dont have time to keep working on this detail ) but I have to learn these options so when I have time I will experiment with this.

    One thing I was wondering if there was a way to insert the entire array in one single query, though not sure if that would be better anyway. This is not a heavy load form. Only one person writing to it at a time and max 40 times per day.


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
  •