Greetings,

I have been wrestling with displaying some output from a form I made for a client. The form must take a checkbox array and display it in a specific part of a sentence in the output.

The form data displays properly but it displays like this:

The answers are answer1,answer2

OR

The answers are answer1,answer2, answer3

If there are two answers there needs to be an AND in between answer1 AND answer2 instead of a comma.

The answers are answer1 AND answer2

OR

The answers are answer1,answer2 AND answer3

OR

The answers are answer1 AND answer2 AND answer3

OR

The answers are answer1 AND answer2 AND answer3,

Here is the Code I use in the form:

<div class="controlset field">
		
<div class="controlset-fields">
<input name="CriticalToSuccess[]" id="CriticalToSuccess" value="Time Savings" type="checkbox" />  
<label for="CriticalToSuccess">Time Savings</label><br />
					
<input name="CriticalToSuccess[]" id="CriticalToSuccess" value="Cost Savings" type="checkbox" />  
<label for="CriticalToSuccess">Cost Savings</label><br />
	
<input name="CriticalToSuccess[]" id="CriticalToSuccess" value="Quality" type="checkbox" /> 
<label for="CriticalToSuccess">Quality</label><br />

<input name="CriticalToSuccess[]" id="CriticalToSuccess" value="Risk Management" type="checkbox" /> 
<label for="CriticalToSuccess">Risk Management</label><br />

<input name="CriticalToSuccess[]" id="CriticalToSuccess" value="Process and Procedure" type="checkbox" /> 
<label for="CriticalToSuccess">Process and Procedure</label><br />										
</div>
</div>

How do you add the AND appropriately?

Thank you in advance!

AceOfJames

Let PHP create the string with checkboxes and echo it.

// array of checkboxes
$cbData = array('Time Savings', 'Cost Savings', 'Quality', 'Risk Management', 'Process and Procedure');

// initialize string for checkboxes
$cbString = '';

// Get the length of the array then you just loop through the array and in each loop check whether it is one before last element (to add AND) or the last element (to add space). In other cases you add comma.

// number of items in the array
$arrayLength = count($cbData);

// loop through elements
foreach($cbData as $key => $cbValue) {

  // construct and ID for your checkbox (add current array key to a string)
  $cbId = 'CriticalToSuccess-' . $key;

  // prepare a string for one checkbox
  $cbString .= "<input name=\"CriticalToSuccess[]\" id=\"$cbId\" value=\"$cbValue\" type=\"checkbox\">";

  // add label
  $cbString .= "<label for=\"$cbId\">$cbValue</label>";

  // check whether it is the last element
  if($key == ($arrayLength - 1)) {

    // if it is last element add only space
    $cbString .= ' ';

  // check whether it is one before the last element
  } elseif($key == ($arrayLength - 2)) {

    // if it is one before last element add string AND
    $cbString .= ' AND ';

  } else {

    // else just add comma
    $cbString .= ', ';
  }
}

// echo the string with checkboxes
echo $cbString;

There is also an error in your form. IDs for checkboxes should not be the same. They should differ. See the code above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.