Jump to content

Insert information into a loop


pianoman993

Recommended Posts

I am having a heck a of a time trying to insert an array into an array. To illustrate this, I am using an array called $errors and trying to insert the values from $errors into an array called $problems like so: array( *errors array values here* );

 

AHH!! heelp! much thanks

- mark

Link to comment
Share on other sites

Thanks for the help so far, I hope this code can make things more specific:

Here's one page's code:

 

	if (empty($_POST['username']))
	{
		$errors = 'Please enter your username';
	} else {
		$username = $_POST['username'];
		global $username;
	}

	if ( (empty($_POST['password'])) || (empty($_POST['conf_password'])) )
	{
		$errors = 'Please enter your password';
	} else {
		$password = $_POST['password'];
		global $password;
	}

		if ( (empty($_POST['email'])) || (empty($_POST['conf_email'])) )
	{
		$errors = 'Please enter your email';
	} else {
		$email = $_POST['email'];
		global $email;
	}

	if ($errors)
	{

		$data['problems'] = array('One','Two');
		$this->template->load('register/register_view',$data);
	} else {
		echo 'Huge Success!';
	}

 

The second page's code using information from the first page:

 

<?php 
if (isset($problems))
{
	foreach($problems as $message) echo "$message<br />";
}
?>

<b>Registration Page</b>

<div id="registration"><table>
<?=form_open('register/handle')?>
    <tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
    <tr><td>Password:</td><td><input type="password" name="password" /></td></tr>
    <tr><td>Confirm Password:</td><td><input type="password" name="conf_password" /></td></tr>
    <tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
    <tr><td>Confirm Email:</td><td><input type="text" name="conf_email" /></td></tr>
    <tr><td colspan="2"><input type="submit" name="submit" value="Register" /></td></tr>
</table></div><!--Registration-->

 

My goal is to somehow get out all of the values from the $errors array and insert them... $data['problems'] = new array( *right here* )

Link to comment
Share on other sites

I tried your method however I got an error message:

 

Here is what I used as my code: Message: implode() [function.implode]: Invalid arguments passed

 

$pointer = implode(",","$errors");
$data['problems'] = array($pointer);

 

It seemed I was closest when I used  print_r

 

 

Link to comment
Share on other sites

Here's how I would code your pages:

 

<?php
session_start();
$errors = array();
if (empty($_POST['username']))
	{
		$errors[] = 'Please enter your username';
	} else {
		$_SESSION['username'] = $_POST['username'];
	}

	if ( (empty($_POST['password'])) || (empty($_POST['conf_password'])) )
	{
		$errors[] = 'Please enter your password';
	} else {
		$_SESSION['password'] = $_POST['password'];
	}

		if ( (empty($_POST['email'])) || (empty($_POST['conf_email'])) )
	{
		$errors[] = 'Please enter your email';
	} else {
		$_SESSION['email'] = $_POST['email'];
	}

	if (!empty($errors))
	{
                $_SESSION['errors']  = $errors;
		$data['problems'] = true;
		$this->template->load('register/register_view',$data);
	} else {
		echo 'Huge Success!';
	}
?>

 

And

<?php
session_start();
if ($data['problems'])
{
	echo implode("<br>\n",$_SESSION['errors']) . "<br>\n";
}
?>

<b>Registration Page</b>

<div id="registration"><table>
<?=form_open('register/handle')?>
    <tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
    <tr><td>Password:</td><td><input type="password" name="password" /></td></tr>
    <tr><td>Confirm Password:</td><td><input type="password" name="conf_password" /></td></tr>
    <tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
    <tr><td>Confirm Email:</td><td><input type="text" name="conf_email" /></td></tr>
    <tr><td colspan="2"><input type="submit" name="submit" value="Register" /></td></tr>
</table></div><!--Registration-->

 

I replaced the lines of the type "global $variable" with "$_SESSION['variable'] = $_POST['variable'], since the "global" keyword is meaningless in this context.

 

Ken

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.