2
\$\begingroup\$

This essentially checks if any of these if statements are true, then echos a statement.

Unfortunately, wrapping the error message with a div tag is impossible since I set error to 1 after I would have the div tag show up. I assume I could use a function for this but I don't understand how they work.

Is there a better way to check all these parameters then return a string that I can check if something exists in that string or not to post?

if ($error == '1'){ echo '<div class="error_message">';}
if(strlen($display)<4)
{
echo "Display name is too short. <br>";
$error = '1';
}
if ($email == '')
{
echo "You did not put a valid email adress. We require this to verify your account. <br>";
$error = '1';
}   
if(mysql_num_rows($check_display) != 0)
{
echo "Display name is already in use. <br>";
$error = '1';
}

if(mysql_num_rows($check_user) != 0)
{
echo "Username is already in use. <br>";
$error = '1';
}

if(mysql_num_rows($check_mail) != 0)
{
echo "Email is already in use. <br>";
$error = '1';
}

if ($pass != $pass2) 
{ 
echo 'Passwords do not match. <br>' ;  
$error = '1';
}
if ($error == '1'){ echo '</div>';}
\$\endgroup\$

migrated from stackoverflow.com Sep 29 '13 at 1:09

This question came from our site for professional and enthusiast programmers.

2
\$\begingroup\$

Like so: Here you can see that is easier to have errors as an array, use alternative control statments to separate php from html:

 <?php
    $messages = array();
    if(strlen($display)<4)
    {
        $messages[] = 'Display name is too short';
    }
    if (empty($email))
    {
        $messages[] ='You did not put a valid email address. We require this to verify your account.';
    }   
    if(mysql_num_rows($check_display) != 0)
    {
        $messages[] = 'Display name is already in use';
    }

    if(mysql_num_rows($check_user) != 0)
    {
        $messages[] = 'Username is already in use.';
    }

    if(mysql_num_rows($check_mail) != 0)
    {
        $messages[] = 'Email is already in use.';
    }

    if ($pass != $pass2) 
    { 
        echo 'Passwords do not match.';  
    }
    ?>
    <div class="error_message">
        <?php if(!empty($messages):?>
            <ol>
            <?php foreach($messages as $message):?>
                <li><?php echo $message;?></li>
            <?php endforeach;?>
            </ol>
        <?php endif;?>
    </div>
\$\endgroup\$
1
\$\begingroup\$

The solution KA_lin gave is a good and clean one. I would however make a small change when displaying it. You should only display the error box when there are some errors to display otherwise you will have an empty error box in your page. It also uses less rendering time and bandwidth when no errors are to be displayed.

Put everything inside the if statement.

<?php if(!empty($messages):?>
    <div class="error_message">
        <ol>
        <?php foreach($messages as $message):?>
            <li><?php echo $message;?></li>
        <?php endforeach;?>
        </ol>
    </div>
<?php endif;?>
\$\endgroup\$
  • \$\begingroup\$ maybe ka_lin purposely put the box there with some styling.. but i got your point \$\endgroup\$ – 4 Leave Cover Dec 29 '16 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.