Jump to content

Help with for loop, checking for 2 array variables


coupe-r

Recommended Posts

Hello All,

 

Attached are 2 screenshots of the form and output for better reference.

 

What I am trying to do is select all or some checkboxes and for those boxes that are checked, echo out their Payment Method.

 

---If a checkbox is checked but the Payment Method box == select, give an error.

 

---If checkbox is checked and Payment Method != select, echo out the ID and selection from the drop down box.

 

Currently, I am echoing the correct ID(s) when certain boxes are checked, but I'm getting every drop down box selection when the corresponding box is not selected.

 

Any help would be greatly appreciated.

 

This is the HTML / PHP Form:

$results .= '<table align="center" width="70%" border="0" bgcolor="'.$bg.'" cellpadding="2" cellspacing="0" class="resultsUnderline">';
$results .= '<tr>';
$results .= '<td width="30%" align="left">'.'<input type="checkbox" name="checkbox[]" value="'.$app_pmt_id.'" id="checkbox" />'
.$full.'<FONT size="1" color="maroon"> ( '.$app_id.' )</FONT></td>';
$results .= '<td width="30%" align="center">'.$created.'</td>';
$results .= '<td width="20%" align="center">
<select name="pay_method[]" class="drop" id="pay_method">
<option value="select">-- Method --</option>
<option value="check">Check</option>
<option value="credit">Credit Card</option></td>';
$results .= '<td width="10%" align="center">'.$paid.'</td>';
$results .= '<td width="10%" align="right">'.$fee.'</td>';
$results .= '</tr>';
$results .= '</table>';

 

Here is the PHP getting the results:

if(isset($_POST['action_box']) && $_POST['action_box'] == 'mark_paid')
{
    $checked = array();
    $checked2 = array();
    
    if(!isset($_POST['checkbox']))
    {
        $val_error[] = 'You have not selected any applications.';
    }
        elseif($_POST['pay_method'] == 'select')
        {
            $val_error[] = 'Please select a Payment Method.';
        }
    else
    {
        foreach($_POST['checkbox'] as $value)
        {
            $checked[] = $value;
        }
        foreach($_POST['pay_method'] as $value2)
        {
            $checked2[] = $value2;
        }
            
        echo implode(",", $checked);
        echo implode(",", $checked2);

        //$result = mysql_query("UPDATE app_pmt SET paid = '1' WHERE app_pmt_id IN(" . implode(",", $checked) . ")");
        //header('Location: bal_due.php?id='.$_GET['id']);
    }
}

 

[attachment deleted by admin]

if(isset($_POST['action_box']) && $_POST['action_box'] == 'mark_paid')
{
    $checked = array();
    $checked2 = array();
    $err = false;

    if(!empty($_POST['checkbox']))
    {
        $val_error[] = 'You have not selected any applications.';
    }
    else
    {
        foreach($_POST['checkbox'] as $key => $value)
        {
            $checked[]  = $value;
            if ($_POST['pay_method'][$key] != 'select')
            {
               $checked2[] = $_POST['pay_method'][$key];
            }
            else
            {
               $err = true;
               $val_error[] = 'Payment method not updated for one or more selected application.';
               break;
            }
        }
            
        echo implode(",", $checked);
        echo implode(",", $checked2);

        if (!$err)
        {
           //$result = mysql_query("UPDATE app_pmt SET paid = '1' WHERE app_pmt_id IN(" . implode(",", $checked) . ")");
           //header('Location: bal_due.php?id='.$_GET['id']);
        }
    }
}

SWEET!!  thank you for taking the time to do that.  Last thing.  How should my query be to catch each of the IDs payment method?

 

Right now I have

$result = mysql_query("UPDATE app_pmt SET method = '".."', paid = '1' WHERE app_pmt_id IN(" . implode(",", $checked) . ")");

 

What should go in method=''?

 

Thanks again

The only way to do so accurately is run multiple query's in a for loop as the key's will correspond for each selected value. Other than that the only other way is only allowing them to be updated in seperate groups of the same payment method.

Had an idea. This should work:

if(isset($_POST['action_box']) && $_POST['action_box'] == 'mark_paid')
{
    $check  = array();
    $credit = array();
    $err    = false;

    if( empty($_POST['checkbox']) )
    {
        $val_error[] = 'You have not selected any applications.';
    }
    else
    {
        foreach($_POST['checkbox'] as $key => $value)
        {
            if ($_POST['pay_method'][$key] == 'check')
            {
               $check[] = $value;
            }
            elseif ($_POST['pay_method'][$key] == 'credit')
            {
               $credit[] = $value;
            }
            else
            {
               $err = true;
               $val_error[] = 'Payment method not updated for one or more selected application.';
               break;
            }
        }

        if (!$err)
        {
           mysql_query("UPDATE app_pmt SET method = 'check', paid = '1' WHERE app_pmt_id IN(" . implode(",", $check) . ")");
           mysql_query("UPDATE app_pmt SET method = 'credit', paid = '1' WHERE app_pmt_id IN(" . implode(",", $credit) . ")");
           header('Location: bal_due.php?id='.$_GET['id']);
        }
    }
}

 

P.S.

 

I think cheque is spelled like that. Not sure tho.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.