values in arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

values in arrays

Post by me! »

what is the proper way to do this:

Code: Select all

$var[key] = "value";




:?:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

There's a wonderful online manual telling you (almost) all the basic stuff you need to know about php.
see http://www.php.net/manual/en/language.types.array.php
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post by me! »

I started at that link, that is how I came up with the code in the first post. But I am getting
Notice Use of undefined constant key - assumed 'value'
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

put key in single quotes as

Code: Select all

$var['key']='value';
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

There's a whole paragraph about Why is $foo[bar] wrong? on that man page.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post by me! »

:oops: Ok, I feel stupid now I totally missed that!

Thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

that's why we're here... :D
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Everah wrote:that's why we're here... :D
To make people feel stupid, that is :D
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post by me! »

Ok I still don't know what I am doing wrong.... :cry:

Also If anyone knows a better way to do this let me know...

Items are set as follows:

Code: Select all

// start  arrays 
$weeks = array();
$days = array();
$rate = array();
						

//get system settings from db
$results = mysql_query("SELECT * FROM settings");
	while ($mysql = mysql_fetch_array($results)) 
		{
		 // week 1 THIS IS REPEATED FOR ALL 10 WEEKS
		if ($week_1 == '1') {	$days['w1'] = 'Full Day, Full Week';
						$rate['r1'] = ($mysql[fd_mf]); 
						$weeks['week_1'] = ($mysql[week_1]);}
		if ($week_1 == '2') {	$days['w1'] = 'Full Day, Monday, Wednesday, Friday';
						$rate['r1'] = ($mysql[fd_mwf]);
						$weeks['week_1'] = ($mysql[week_1]) ; }
		if ($week_1 == '3') {	$days['w1'] = 'Full Day, Tuesday & Thursday';
						$rate['r1'] = ($mysql[fd_tt]);
						$weeks['week_1'] = ($mysql[week_1]) ; }
		if ($week_1 == '4') {	$days['w1'] = '<b>Half Day</b>, Full Week';
						$rate['r1'] = ($mysql[hd_mf]);
						$weeks['week_1'] = ($mysql[week_1]) ; }
		if ($week_1 == '5') {	$days['w1'] = '<b>Half Day</b>, Monday, Wednesday, Friday';
						$rate['r1'] = ($mysql[hd_mwf]); 
						$weeks['week_1'] = ($mysql[week_1]) ;}
		// You get the idea...
                }		

And this is the code that is sending the "notice Undefined index" warnings in the email:

Code: Select all

//Turn on output buffering
ob_start(); 

						
// show weeks selected
if ($week_1 != '0') echo ''.$weeks['week_1'].', '.$days['w1'].' - $'.$rate['r1'].' ';	
if ($week_2 != '0') echo ''.$weeks['week_2'].', '.$days['w2'].' - $'.$rate['r2'].' ';
if ($week_3 != '0') echo ''.$weeks['week_3'].', '.$days['w3'].' - $'.$rate['r3'].' ';
if ($week_4 != '0') echo ''.$weeks['week_4'].', '.$days['w4'].' - $'.$rate['r4'].' ';
if ($week_5 != '0') echo ''.$weeks['week_5'].', '.$days['w5'].' - $'.$rate['r5'].' ';
		
							
//copy current buffer contents into $message variable and delete current output buffer
$selections = ob_get_clean();	
						
													
// send E-mail to the person that registered	
$to  = "$email";							                  
$subject = 'Registration';						
$message = 'Some stuff plus '.$selections.'';
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

viewtopic.php?p=362999#362999

Do not double post, topic locked.
Locked