Jump to content

[SOLVED] Array problem


S1acker

Recommended Posts

<?php
include("inc/inc.sql.php");
$sql = new sql;
$sql->connect("localhost", "root", "root", "v2");


class Check {
var $username;
var $password1;
var $password2;
var $email;

function check_user($username) {
	$user_errors = array();
	// Check if taken
	$taken = mysql_query("SELECT * FROM users WHERE username = '$username'");
	if (mysql_num_rows($taken)) $user_errors[] = "Username already exists";
	// Check if it is the proper length
	if ((strlen($username) > 12) || (strlen($username) < 6)) $user_errors[] = "Username must be between 6 and 12 characters";
	// Return the errors
	return $user_errors;
}

function check_pass($password1, $password2) {
	$pass_errors = array();
	// Check if they match
	if (!$password1 == $password2) $pass_errors[] = "Passwords do not match";
	// Check if it is proper length
	if ((strlen($password1) > 12) || (strlen($password1) < 6)) $pass_errors[] = "Password must be between 6 and 12 characters";
	// Return the errors
	return $pass_errors;
}

function check_email($email) {
	$email_errors = array();
	// Check if it is the right format
	$regex = '/\b([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+[a-zA-Z]{2,4})\b/';
	if (!preg_match($regex, $email)) $email_errors[] = "Email is the wrong format";
	// Return the errors
	return $email_errors;
}
}



if ($_POST['username']) {
$errors = array();
if (!$_POST['password1'] || !$_POST['password2']) $errors[] = "You did not enter a password";
if (!$_POST['email']) $errors[] = "You did not enter a email";
$check = new Check;
$user_errors[] = $check->check_user($_POST['username']);
$pass_errors[] = $check->check_pass($_POST['password1'], $_POST['password2']);
$email_errors[] = $check->check_email($_POST['email']);
$errors = array_merge($errors, $user_errors);
$errors = array_merge($errors, $pass_errors);
$errors = array_merge($errors, $email_errors);

if ($errors) {
	echo "<h3>The following errors occured</h3><br>";
	foreach ($errors as $e) {
		echo $e;
	}
} else {
	echo "You've successfully been added to the db";
}			
} else {

?>
<h2>Register</h2>
<p>
<form method="post" action="register.php">
<ul>
<li>Username:</li><input name="username" type="text" class="test" />
<a href="#" class="hintanchor" onMouseover="showhint('Enter your desired username. Must be between 6 and 12 letters long.', this, event, '200px')">[?]</a><br />
<li>Password:</li><input name="password1" type="password" class="test" />
<a href="#" class="hintanchor" onMouseover="showhint('Enter your desired passwrd. Must be between 6 and 12 letters long.', this, event, '200px')">[?]</a><br />
<li>Verify:</li><input name="password2" type="password" class="test" />
<a href="#" class="hintanchor" onMouseover="showhint('Verify your password. Must match the feild above.', this, event, '200px')">[?]</a><br />
<li>Email:</li><input name="email" type="text" class="test" />
<a href="#" class="hintanchor" onMouseover="showhint('Enter your email. Must be valid, you will be validated with it.', this, event, '200px')">[?]</a><br />
<br /><input name="submit" type="submit" value="Submit" style="border-style: double; border-color: rgb(204, 204, 204) rgb(153, 153, 153) rgb(153, 153, 153) rgb(204, 204, 204); border-width: 3px; padding: 0.25em; width: auto; background-color: rgb(238, 238, 238); background-image: url(http://www.macromedia.com/images/master/background_form_element.gif); background-repeat: repeat-x; color: rgb(51, 51, 51); font-size: 100%; font-weight: bold; font-family: Verdana,Helvetica,Arial,sans-serif;" />
</ul>
</form>
</p>
<?php
}
?>

 

Something is wrong with the arrays when the errors get echoed. I'm trying a new way of displaying errors but it's not going so well.

Link to comment
Share on other sites

Something is wrong with the arrays when the errors get echoed. I'm trying a new way of displaying errors but it's not going so well.

 

Mind describing the actual problem you're having when you try displaying the errors?  Are they being output to the screen?  Are they in the correct order?

Link to comment
Share on other sites

you have an array in an array..

 

so

function check_user($username) {
	$user_errors = array();
	// Check if taken
	$taken = mysql_query("SELECT * FROM users WHERE username = '$username'");
	if (mysql_num_rows($taken)) $user_errors[] = "Username already exists";
	// Check if it is the proper length
	if ((strlen($username) > 12) || (strlen($username) < 6)) $user_errors[] = "Username must be between 6 and 12 characters";
	// Return the errors
	return $user_errors;
}

 

should be

function check_user($username) {
	$user_errors = "";
	// Check if taken
	$taken = mysql_query("SELECT * FROM users WHERE username = '$username'");
	if (mysql_num_rows($taken)) $user_errors = "Username already exists";
	// Check if it is the proper length
	if ((strlen($username) > 12) || (strlen($username) < 6)) $user_errors[] = "Username must be between 6 and 12 characters";
	// Return the errors
	return $user_errors;
}

Link to comment
Share on other sites

It returns:

 

The following errors occured

ArrayArrayArray

 

Why not just append the arrays?

<?php
        $check = new Check;
$user_errors[] = $check->check_user($_POST['username']);
$pass_errors[] = $check->check_pass($_POST['password1'], $_POST['password2']);
$email_errors[] = $check->check_email($_POST['email']);
$errors = $user_errors[] + $pass_errors[] + $email_errors[];

if ($errors) {
	echo "<h3>The following errors occured</h3><br>";
	foreach ($errors as $e) {
		echo $e;
	}
} else {
	echo "You've successfully been added to the db";
}
?>

Link to comment
Share on other sites

It returns:

 

The following errors occured

ArrayArrayArray

 

Why not just append the arrays?

<?php
        $check = new Check;
$user_errors[] = $check->check_user($_POST['username']);
$pass_errors[] = $check->check_pass($_POST['password1'], $_POST['password2']);
$email_errors[] = $check->check_email($_POST['email']);
$errors = $user_errors[] + $pass_errors[] + $email_errors[];

if ($errors) {
	echo "<h3>The following errors occured</h3><br>";
	foreach ($errors as $e) {
		echo $e;
	}
} else {
	echo "You've successfully been added to the db";
}
?>

 

Tried that and it returned

 

The following errors occured

Array

:(

Link to comment
Share on other sites

sorry one more thing  :(

 

	if ($errors) {
	echo "<h3>The following error(s) occured</h3><br>";
	foreach ($errors as $err) {
		foreach ($err as $e) {
			echo "<li>$e</li><br>\n";
		}
	}
} else {
	echo "You've successfully been added to the db";
}	

 

It displays "The following error(s) occured"

 

Even when the form gets filled out correctly.

Link to comment
Share on other sites

this may work, (can't read back, my head is killing me)

 

if ( count($errors[0])>0 && count($errors[1])>0 && count($errors[2])>0 ) {

 

the proble because you have 3 arrays and each have been set to the $errors array has 3 values

$user_errors[], $pass_errors[] & $email_errors[], and each of them have values (even if they blank)

the above code should work, if the 3 arrays are empty (not blank but empty)

 

personally i would only use 1 array and put all error in that..

i hope this helps

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.