Jump to content

Reading Array to get Value


doubledee

Recommended Posts

I've never been very good with arrays, and am totally new to them in PHP...    :-\

 

If I have this array...

$concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)',
			'201105072'=>'May 7, 2011 (1:00pm - 4:00pm)',
			'201105141'=>'May 14, 2011 (9:00am - 12:00pm)');

 

...and I know the key, e.g.

 

$concertDate = '201105072'

 

 

Then how do I get the corresponding value, i.e.

 

May 7, 2011 (1:00pm - 4:00pm)'

 

...from the array??

 

 

Debbie

 

 

Link to comment
Share on other sites

What does this mean???

 

Notice: Uninitialized string offset: 201105072 in /Users/user1/Documents/DEV/++htdocs/00_MyWebSite/payment_form4_auth4.php on line 394

 

$concertDate = $concertDates = '';
$orderDescription = '';

 

 

Line 394

// Create Order Description.
$orderDescription = "Rockin' with Beethoven, ";
$orderDescription .= "$concertDates[$concertDate] ";
$orderDescription .= "Tempe High School, Tempe, AZ";

 

 

 

Debbie

 

 

Link to comment
Share on other sites

It means that $concertDates is a string, not an array.

 

Is that because I initially said...

 

concertDates = '';

 

 

Because it clearly is an array...

 

$concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)',
			'201105072'=>'May 7, 2011 (1:00pm - 4:00pm)',
			'201105141'=>'May 14, 2011 (9:00am - 12:00pm)');

 

 

 

Debbie

 

 

Link to comment
Share on other sites

Yeah, when you do the $concertDates = ''; it reinitializes it as an empty string instead of an array. If the array is defined and you comment out the offending line, it should work.

 

But I initialized the array - obviously - before I populated it?!

 

How do you declare and initialize an empty array in PHP?

 

And what is the proper way to initialize variables in general??  :shrug:

 

I always get errors in my programs about "undefined/undeclared variable" so I just start every script now like this...

 

		$firstName = $lastName = '';
		$address1 = $address2 = $fullAddress = '';
		$city = $state = $zip = '';

 

 

BTW, these are arrays...

 

$months = $years = '';

 

...and I didn't have any issues with them when I did this...

 

$months = array(1=>'01','02','03','04','05','06','07','08','09','10','11','12');
$years = range(2011, 2020);

 

 

 

Debbie

 

 

Link to comment
Share on other sites

At the top of my script I changed things to...

 

		$concertDate = '';
		$concertDates = array();

 

And farther down I still have...

 

			// Create Order Description.
			$orderDescription = "Rockin Beethoven, ";
			$orderDescription .= $concertDates[$concertDate] . " ";
			$orderDescription .= "Our High School, Anytown, USA";

 

 

But I am still getting an error...

Notice: Undefined index: 201105141 in /Users/user1/Documents/DEV/++htdocs/00_MyWebSite/payment_form4_auth4.php on line 397

 

 

(Line 397 is the middle one above.)

 

 

 

Debbie

 

 

Link to comment
Share on other sites

Well, let's see what's in the array then. It may not be populating as you think it is.

 

$orderDescription = "Rockin Beethoven, ";
$orderDescription .= $concertDates[$concertDate] . " ";
$orderDescription .= "Our High School, Anytown, USA";
echo '$concertDates:<br><pre>'; print_r($concertDates); echo '</pre>'; // <--- ADD THIS LINE

Link to comment
Share on other sites

Well, let's see what's in the array then. It may not be populating as you think it is.

 

$orderDescription = "Rockin Beethoven, ";
$orderDescription .= $concertDates[$concertDate] . " ";
$orderDescription .= "Our High School, Anytown, USA";
echo '$concertDates:<br><pre>'; print_r($concertDates); echo '</pre>'; // <--- ADD THIS LINE

 

 

I get this...

 

$seminarDates:

 

Array

(

)

 

 

So why is my array empty???

 

 

Debbie

 

 

Link to comment
Share on other sites

Without seeing the code that populates it, there's no way to answer that question.

 

But I already gave you the code snippet.  (I can't post all of my code here due to size and the fact that it have a lot of sensitive info in it like Payment Gateway account info.)

 

This should be enough to help me figure this out...

 

Starting from the top down...

 

<body>
<?php
	// Initialize variables.
	$attendeeName = $venue = '';
	$venues = array();
	$concertDate = '';
	$concertDates = array();
	$invoiceNumber = $orderDescription = '';
	$merchantContact = $receiptMessage = '';

	$errors = array();


	// *********************************************************************
	// HANDLE FORM.
	// *********************************************************************
	if (isset($_POST['submitted'])){
		// Trim all incoming data.
		$trimmed = array_map('trim', $_POST);

		// ******************************
		// CHECK CONCERT INFORMATION.		*
		// ******************************

		// Check Concert Date.
		if (!empty($_POST['concertDate'])){
			if (preg_match('#^(201105071|201105072|201105141)$#', $_POST['concertDate'])){
				$concertDate = $_POST['concertDate'];
			}else{
				$errors['concertDate'] = 'Choose a Date from the drop-down list.';
			}
		}else{
			$errors['concertDate'] = 'Please select a Concert Date.';
		}

 

 

Then later on my form....

 

<!-- Concert Date -->
<li>
<label for="concertDate">Concert Date/Time:</label>
<select id="concertDate" name="concertDate">
	<option value=""></option>
	<!-- Create concert Date Select List -->
	<?php
		$concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)',
													'201105072'=>'May 7, 2011 (1:00pm - 4:00pm)',
													'201105141'=>'May 14, 2011 (9:00am - 12:00pm)');
		foreach($concertDates as $key => $value){
				echo '<option value="' . $key . '" ';
				echo (!empty($_POST['concertDate']) && $_POST['concertDate']==$key) ? 'selected="selected"' : '';
				echo '>' . $value . '</option>\n';
		}
	?>
</select>
<?php
	if (!empty($errors['concertDate'])){
		echo '<span class="error">' . $errors['concertDate'] . '</span>';
	}
?>
</li>

 

The rest of this form/script works fine..  :'(

 

 

 

Debbie

 

 

Link to comment
Share on other sites

I put test echos in my code.

 

// Check Concert Date.
if (!empty($_POST['concertDate'])){
if (preg_match('#^(201105071|201105072|201105141)$#', $_POST['concertDate'])){
	$concertDate = $_POST['concertDate'];
}else{
	$errors['concertDate'] = 'Choose a Date from the drop-down list.';
}
}else{
$errors['concertDate'] = 'Please select a concert Date.';
}

echo "concertDate = " . $concertDate;

 

Yields...

 

concertDate = 201105072

 

...after I select the 2nd option in the drop-down.

 

 

 

<!-- Concert Date -->
<li>
<label for="concertDate">Concert Date/Time:</label>
<select id="concertDate" name="concertDate">
	<option value=""></option>
	<!-- Create concert Date Select List -->
	<?php
		$concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)',
													'201105072'=>'May 7, 2011 (1:00pm - 4:00pm)',
													'201105141'=>'May 14, 2011 (9:00am - 12:00pm)');


echo '$concertDates:<br><pre>'; print_r($concertDates); echo '</pre>'; // <--- ADD THIS LINE


		foreach($concertDates as $key => $value){
				echo '<option value="' . $key . '" ';
				echo (!empty($_POST['concertDate']) && $_POST['concertDate']==$key) ? 'selected="selected"' : '';
				echo '>' . $value . '</option>\n';
		}
	?>
</select>
<?php
	if (!empty($errors['concertDate'])){
		echo '<span class="error">' . $errors['concertDate'] . '</span>';
	}
?>
</li>

 

 

Yields...

 

Notice: Undefined index: 201105072 in /Users/user1/Documents/DEV/++htdocs/00_MyWebSite/payment_form4_auth4.php on line 399

Call Stack

# Time Memory Function Location

1 0.0038 184488 {main}( ) ../payment_form4_auth4.php:0

$concertDates:

 

Array

(

)

 

...after submitting the form

 

 

I don't see how the array ($concertDates) can be empty and yet the variable ($concertDate) has a $key value from the array?!

 

 

 

Debbie

 

Link to comment
Share on other sites

<?php
		$concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)',
													'201105072'=>'May 7, 2011 (1:00pm - 4:00pm)',
													'201105141'=>'May 14, 2011 (9:00am - 12:00pm)');

 

This is the actual code that populates the array? The array doesn't come from the result of a database query?

Link to comment
Share on other sites

<?php
		$concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)',
													'201105072'=>'May 7, 2011 (1:00pm - 4:00pm)',
													'201105141'=>'May 14, 2011 (9:00am - 12:00pm)');

 

This is the actual code that populates the array? The array doesn't come from the result of a database query?

 

 

That's it!!

 

(There are only 3 dates and I was supposed to have this done today.  No time for a database.  If I ever survive this project, then I can look into making it more robust.)

 

 

 

Debbie

 

 

Link to comment
Share on other sites

If that's all the code you can post, all I can suggest is to print_r() the array at different points in the script until you figure out at which point it loses its values.

 

I initialize at the top of my script...

 

<body>
	<?php
		// Initialize variables.
		$concertDate = '';
		$concertDates = array();


		// *********************************************************************
		// HANDLE FORM.
		// *********************************************************************
		if (isset($_POST['submitted'])){
			// Trim all incoming data.
			$trimmed = array_map('trim', $_POST);

			// ******************************
			// CHECK CONCERT INFORMATION.	*
			// ******************************

			// Check Concert Date.
			if (!empty($_POST['concertDate'])){
				if (preg_match('#^(201105071|201105072|201105141)$#', $_POST['concertDate'])){
					$concertDate = $_POST['concertDate'];
				}else{
					$errors['concertDate'] = 'Choose a Date from the drop-down list.';
				}
			}else{
				$errors['concertDate'] = 'Please select a Concert Date.';
			}

echo "concertDate = " . $concertDate;



		}// End of HANDLE FORM.
	?>

	<!-- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -->
	<!-- HTML PAYMENT FORM -->
	<form id="payment" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

 

 

It looks like I may be erasing out all of my variables every time I re-sumit the form onto itself...

 

 

Any ideas???

 

 

 

Debbie

 

 

Link to comment
Share on other sites

Instead of doing this

<?php
$concertDates = array();
?>

at the start of your script, initialize the array with the values and remove all other lines that populate the array:

<?php
$concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)',
    '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)',
    '201105141'=>'May 14, 2011 (9:00am - 12:00pm)');
?>

 

Ken

Link to comment
Share on other sites

Instead of doing this

<?php
$concertDates = array();
?>

at the start of your script, initialize the array with the values and remove all other lines that populate the array:

<?php
$concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)',
    '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)',
    '201105141'=>'May 14, 2011 (9:00am - 12:00pm)');
?>

 

Ken

 

My problem was I *thought* the array variable stayed alive when I re-submitted the form.

 

It doesn't.

 

(Wasted 8 hours on this!)

 

Am now frantically trying to re-learn Sessions to help me out...

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

Debbie,

 

I would take a step back before looking into sessions to solve your problem.  When someone clicks Submit on your form are they going to another page in your app / site?  If so then the array needs to be used on the second page.  If not and you are just refreshing the page on submit then the array should still be "alive". 

 

Wherever you have your Array.  Use print_r().

 

<?php

$concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)',
    '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)',
    '201105141'=>'May 14, 2011 (9:00am - 12:00pm)');

print_r($concertDates);
exit;

?>

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.