Current location: Hot Scripts Forums » Programming Languages » PHP » How: Create an array with variable keys


How: Create an array with variable keys

Reply
  #1 (permalink)  
Old 02-20-09, 04:48 PM
ginasuit ginasuit is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy How: Create an array with variable keys

How do I create an array using a variable as the keys?
This is an excerpt of the code I'm making for a form that both creates and processes itself.

$submission = "value submitted by User through $_POST"; ie: Joe
$lable = "value of variable created by script manager"; ie: Name

The following code is assembled inside a foreach statement. Which may be my entire problem . what I'm trying to do is have all my variables run through this form and strore all errors in an array and print them separately from the printing of the form even though they are both in the same foreach statement. I have gotten the form to print fine but whenever I try to print the $errorArray or $cssErrors array the array prints this Array ( [0] => ) three times inbetween my form elements. I would be happy to e-mail the source of my code to anyone willing to filter though, I comment very well but the code it'self is 160 lines in one file, but includes a blank html doc. and lots of other lines that can be ignored.



if ($submission) {
if ($howToValidate=01) {// validate General ie: name address city state
if (ereg ("<|>|\\\\|,|:|\^|\]|\[|;", $submission ) ){//(ereg ("string", $date))
$cssErrors[$lable]=$error;
$formErrors=$formError+1;
$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
"'s cannot contain the following characters: < > \ , : ^ ] [ ;";
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-20-09, 05:04 PM
ginasuit ginasuit is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy I decided to add all the source here to help

// this peice of crap won't let me post my code because it thinks there is a url in it // even though there isn't and I don't know why it thinks that but here is the php
Code:
<?php

if (!isset($_POST['Name@name%01-1'])	){
$_POST['Name@name%01-1']= "";
$_POST['E-MAIL@eMail%02-1']= "";
$_POST['Phone@phone%03-1'] = "";
}
$a = $_POST;
$numFormElements=sizeof($a);// number of loops for while
$REQ="* ";// Rquired symbol for form display ie: *E-mail --> meaning E-Mail is required field
if (isset ($_POST)	){
	$numFormDisplays=1;
}
$z=0;
$errorsArray[$lable]="";
foreach ($_POST as $post => $submission) {		//Breakup variables for processing
		$eval = $post;
		$eval2 = $post;
		$findLableMarker = 0;
		$eval = preg_replace('/[^0-9]/','', $eval)	;//delete all non numbers for decoding ending numbers
	$isRequired=( substr	($eval, 2, 1)		);//decide if entry is required
	$howToValidate=(	substr	($eval, 0,	2)	);// decide how to validate entry
	
		$findLableMarker = strrpos($eval2,"@", 0);
	$lable=( substr($eval2, 0, $findLableMarker)	);// find lable as in form <lable value="E-Mail"
		$findLableMarker = strlen($lable);
		$findLableMarker = $findLableMarker+1;
		$findforMarker = strrpos($eval2,"%");
		$findforMarker = $findforMarker - $findLableMarker;
	$for=( substr($eval2, $findLableMarker, $findforMarker)	);// find for as in form <input for="eMail"
			
			
		if (isRequired == 1 && empty ($submission)	){// check if field is required
			$cssErrors[$lable]=$lable;
			$formErrors=$formError+1;
			$errorsArray[$lable]="Your $lable is required to process your Request!";
		}
		
		
		if ($submission)	{
			if ($howToValidate=01) {//	validate General ie: name address city state
				if (ereg ("<|>|\\\\|,|:|\^|\]|\[|;", $submission	)	){//(ereg ("string", $date))
					$cssErrors[$lable]=$error;
					$formErrors=$formError+1;
					$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
					"'s cannot contain the following characters: < > \ , : ^ ] [ ;";
				}
			}
			if ($howToValidate=02){// validate as an e mail
				if (ereg("\(|\)|<|>|\\\\|,|:|\^|\]|\[|;", $submission)	) {
					$cssErrors[$lable]=$error;
					$formErrors=$formError+1;
					$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
					"'s cannot contain the following characters: < > \ ' , : ^ ] [ ;";
				}
				if ( !eregi (".*[.]*.+@.*\.{1}.*", $submission)		){
					$cssErrors[$lable]=$error;
					$formErrors=$formError+1;
					$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
					"'s must contain an @ and a . for them to be processed properly";
				}
		}
			if ($howToValidate=03){// Validate as a phone number
				$phone = $submission;
				$phone = preg_replace('/[^0-9]/','',$phone);
					if (strlen($phone)==10) { 
						$areacode = substr($phone, 0,3);
						$exch = substr($phone,3,3);
						$phoneEnd = substr($phone,6,4);
						$submission = "(" . $areacode . ") " . $exch . "-" . $phoneEnd;
			}
					if (strlen($phone)==11) { 
						$countryCode = substr($phone, 0, 0);
						$areacode = substr($phone, 1, 3);
						$exch = substr($phone,4,3);
						$phoneEnd = substr($phone,7,4);
						$submission = $countryCode. " "."(" . $areacode . ") " . $exch . "-" . $phoneEnd;
			}
			else {
				$cssErrors[$lable]=$error;
				$formErrors=$formError+1;
				$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
					"'s must either 10 or 11 digits ie: 555-555-5555 or 1 (555) 555-5555";
				}
		}
		
		}
if ( ( /*$formErrors*/1 > 0) || ( empty ($numFormDisplays) ) ){// display form with or without previously entered data
	/*if ( empty ($numFormDisplays) ){// empty error arrays if form hasn't been displayed
		unset ($cssErrors, $formErrors, $errorsArray);
	}*/
	if (isRequired == 0 ){
		$REQ="";
	}

$form="	<tr><td><label for=\"".$for."\">".$REQ.$lable."</label></td><td>"."<input name=\"".
$lable."@".$for."%".$howToValidate."-".$isRequired."\"".
"type=\"text\" id=\"$for\" value=\"".$submission."\" size=\"37\" /></td></tr>";

		if ($z < 1){// only print begining of the form once
	$beginForm="<form action=\"http://localhost/bkiser.net/public/Test.php\" method=\"post\"><table border=\"0\"". 
	"cellpadding=\"8\" cellspacing=\"8\" summary=\"feedback form\">";	
	}
	else $beginForm="";
	
	if ($z == ( ($numFormElements)-(1) )	){// only print end of the form once
	$endForm="<tr><td align=\"center\" colspan=\"2\"><input type=\"".
	"submit\" value=\"Send Feedback\" /></td></tr></table></form>";	
	}
	else $endForm="";

}
if (empty ($formErrors) ){// mail form
	$g=$g;
}
$z++;//used to keep $beginForm (the form tag itself) from printing twice		
echo $beginForm . $form . $endForm."<br/>";

if ($z == ( ($numFormElements)-(1) )	){
echo"<div id=apDiv1>";
//print_r(array_keys($cssErrors));
//echo("<br/>");

}
print_r(array_keys($errorsArray));
print_r($formErrors."<br/>");

if ($z == ( ($numFormElements)-(1) )	){
echo"</div>";
}
}//		if submission

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-20-09, 09:14 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Not tested.

PHP Code:

$aInputMap=array(

'numeric'=>array('regexp'=>'/\d*/','required'=>true,'label'=>'Numeric','length'=>3));

/* Print inputs, with labels */
foreach ($aInputMap as $k => $v)
  echo 
'<label>'.$v['label'].'</label><input type="text" name="'.$k.'" length="'.$v['length'].'" />';

/* Validation */
$bValid=true;
foreach (
$aInputMap as $k => $v)
{
  
/* If this is a required input, and there isn't a corresponding $_POST */
  
if (($v['required']) && !isset($_POST[$k]))
  { 
    
$bValid=false;
    break;
  }
  
/* Ensure input contents match regexp */
  
$aMatch=array();
  
preg_match($v['regexp'],$_POST[$k],$aMatch);
  if (
$aMatch[0][0] != $_POST[$k])
  {
    
$bValid=false;
    break;
  }

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] array and array keys xportal PHP 5 05-05-08 04:11 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM


All times are GMT -5. The time now is 07:11 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.