Jump to content

php if elseif else


facarroll

Recommended Posts

I have a data form which sends to a mysql database.

In the database I have 6 fields which are informed by check boxes and that dataevaluates to true("1") or false("0").

I am trying to write a short script to check whether one or more boxes were checked. I am new at this, but what I've written works well. The problem I have is that I don't know how to insert the script so that it allows the original script to continue if one of or more of the fields evaluates as true.

This is what I have. There is something wrong at the end, I think.

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Try something like:

 

// perform query
// assuming $data contains the row

$valid = false;

foreach ($data as $chkbox)
{
    if ($chkbox == true)
    {
        $valid = true;
        break;
    }
}

if ($valid)
{
    // do something here
}

Link to comment
Share on other sites

Sorry Mr Adam, don't understand. I'll post my script here and see if you can figure out what I'm doing wrong.

I've posted the working script with the part I cannot get to work is commented out. (lines 54-68)

If I uncomment the lines the script stops.

Any help?

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Sorry I think I misunderstood what you were asking before (thought you were asking to validate there was at least 1 check box ticked for an entry in the database).

 

Start again...

---

 

Right then rather than giving each check box a unique name, store them as an array:

 

<input type="checkbox" name="egroup[]" value="My check box" />

 

On the PHP side you can then spit out the error if the array is empty:

 

if (empty($_POST['egroup']))
{
    exit('You must select a group. No selection has been made');
}

 

The value for the check box is only posted if it's been checked.

Link to comment
Share on other sites

Got it with this.

 

$nogroup = ((!$egroup1) && (!$egroup2) && (!$egroup3) && (!$egroup4) && (!$egroup5) && (!$egroup6));

$noequip = (!$equip);

if ($nogroup){

$errorMsg .= "--- Student Group. (No data was forwarded to the database.)</font>";

} else if($noequip){

$errorMsg .= "--- Quiz Name. (No data was forwarded to the database.)</font>";

} else {

 

 

Thanks for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.