0

Okay, so all my code is working, I just want to know if there's a simpler way to do it. The code:

if($_POST['A'] == ""){echo "A";}
if($_POST['B'] == ""){echo "B";}
if($_POST['C'] == ""){echo "C";}
if($_POST['A'] == "" && $_Post['B'] == ""){echo "A and B";}
if($_POST['A'] == "" && $_Post['C'] == ""){echo "A and C";}
if($_POST['B'] == "" && $_Post['C'] == ""){echo "B and C";}

Now, what I want to know is rather simple, is there any way to make this simpler? It works fairly well with three variables, but if I were to (for example) circle the whole alphabet, if my memory regarding statistics is correct, it'd be 26! which is equal to 4.0329146e+26. Hell, even circling the first 7 letters, 7! is 5040 possible combinations I'd have to code.

What I'm looking for is something that doesn't exactly have to echo "A and B", but if A is the only thing posted to echo A; if A and B are posted to echo "A" and "B" and to echo an "and" between them; and if A, B, and C are posted to echo "A", "B" and "C" and to echo a "," between the first two and to echo an "and" between the second to last and the last.

I don't know if I'm wanting a function, or a class, or something else. This is probably a simple question in the end run, if so, please forgive my ignorance.

3
  • 1
    Something like this ? if($_POST){echo implode(' and ', $_POST);} Commented Apr 2, 2013 at 20:54
  • 2
    a foreach() loop would work Commented Apr 2, 2013 at 20:54
  • Keep the last comma: i.imgur.com/fycHx.jpg Commented Apr 2, 2013 at 20:59

3 Answers 3

2

For a simple message like "A and B", the following would do the job:

$variables = range('A', 'Z');
echo implode(' and ', $variables);

If you have a $_POST you could do the following:

if($_POST){
    echo implode(' and ', array_keys($_POST));
}
Sign up to request clarification or add additional context in comments.

2 Comments

That echo's "and"'s between each value, I'd prefer commas and then "and" at the end. Second, that echo's for each post regardless of whether user inputted data or not, my problem, as I included a typo, == should be !=. Third, that just echo's the variable names, not any more-user-friendly terms. The third issue is not really import at the moment.
@WitoldKowelski Are you using a POST variable or is that just an example ?
1
function format_list( $items )
{
    if ( count( $items ) == 0 )
        return '';
    if ( count( $items ) == 1 )
        return $items[0];

    $last_item = array_pop( $items );
    $list_text = join( ', ', $items ) . ' and ' . $last_item;

    return $list_text;
}

$items = array();
$keys = array( 'A', 'B', 'C' );
foreach ( $keys as $key )
{
    if ( !empty( $_POST[$key] ) )
        $items[] = $key;
}
echo format_list( $items );

1 Comment

This does echo the names of the post input fields, I'd like it to echo something more meaningful to the user, but that's for later.In my code though, I had a typo (I should have just copy/pasted the code instead of re-typing it). But one question, if I may, to correct my typo, how would one have it echo the post names that have not been entered? For example, code should have read like: if($_POST['A'] =! ""){echo "A";} etc.?
0
$count = count($_POST);
$a = 1;
$str = '';
foreach ( $_POST as $key => $value )
{
     // if u wanna use values just replace $key to $value
     if ( $count > $a++ )
         $str .= $key . ', ';
     else
     {
         $str = rtrim( $str, ', ' );
         $str .= ' and ' . $key;
         $str = ltrim( $str, ' and ' );
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.