Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping using Arrays 1

Status
Not open for further replies.

jollydonkey

Technical User
Sep 5, 2008
23
CA
Hello -

I'm hoping someone can help me as I'm relatively new to PHP.

In a nutshell, I would like to know the PHP version of the javascript code below (using arrays and looping). Essentially, I'm trying to capture related values from a form (there are 3 - APPLE, BEAR, and CAR), create a string by appending each related value separated by a comma (dropping the ending comma), and assigning the result to new variables.

I'm hoping there's a very elegant PHP solution - I just don't know what it would be and how to go about it. A working PHP solution would be greatly appreciated.

Thanks!
Jolly Donkey

Code:
// written in javascript

var N   = new Array();
var LOI = new Array();
var IR  = new Array();


APPLE[0] = frmOpt1.opt1A1.value;
APPLE[1] = frmOpt1.opt1A2.value;
APPLE[2] = frmOpt1.opt1A3.value;
APPLE[3] = frmOpt1.opt1A4.value;
APPLE[4] = frmOpt1.opt1A5.value;

BEAR[0] = frmOpt1.opt1B1.value;
BEAR[1] = frmOpt1.opt1B2.value;
BEAR[2] = frmOpt1.opt1B3.value;
BEAR[3] = frmOpt1.opt1B4.value;
BEAR[4] = frmOpt1.opt1B5.value;

CAR[0] = frmOpt1.opt1C1.value;
CAR[1] = frmOpt1.opt1C2.value;
CAR[2] = frmOpt1.opt1C3.value;
CAR[3] = frmOpt1.opt1C4.value;
CAR[4] = frmOpt1.opt1C5.value;


var APPLEOutput   = "";
var BEAROutput = "";
var CAROutput  = "";


for (var counter = 0; counter < 5; counter++){
  if (APPLE[counter] == ""){
    APPLEOutput = APPLEOutput + APPLE[counter];
  }
  else {
    APPLEOutput = APPLEOutput + APPLE[counter]   + ", ";		
  }
	
  if (BEAR[counter] == ""){
    BEAROutput = BEAROutput + BEAR[counter];
  }
  else {
    BEAROutput = BEAROutput + BEAR[counter] + ", ";
	}
  
	if (CAR[counter] == ""){
    CAROutput = CAROutput + CAR[counter];
  }
  else {
    CAROutput = CAROutput + CAR[counter] + ", ";
  }	
}


var APPLEFinal   = APPLEOutput.substr(0,(APPLEOutput.length-2));
var BEARFinal = BEAROutput.substr(0,(BEAROutput.length-2));
var CARFinal  = CAROutput.substr(0,(CAROutput.length-2));
 
The PHP code can only be used after the data is POSTed. PHP is a server side language only. You could do something like this...

Code:
<?php
//Loop through the $_POST array on the receiving page
foreach($i=1;$i<=5;$i++){
//Store the values in temp arrays
	$a[$i] = $_POST["opt1A$i"];
	$b[$i] = $_POST["opt1B$i"];
	$c[$i] = $_POST["opt1C$i"];
}
//Implode the arrays into comma separated values
$apple = implode(',', $a);
$bear = implode(',', $b);
$car = implode(',', $c);

//View the output
echo "$apple\n$bear\n$car"
?>

Mark
 
Oops. The previous post doesn't loop the $_POST array. It counts from 1 to 5. That can only be used for a finite list.

This will work for an infinite list...

Code:
<?php

$tmparr = array();

//Loop through $_POST array
foreach($_POST as $k=>$v){

//Get A,B, or C from the variable name
	$newk = substr($k,4,1);
//Get the digit from the end of the variable name
	$newi = substr($k,5,1);

//Store in a temp array
	$tmparr[$newk][$newi] = $v;
}
//implode to comma separated list
$apple = implode(',', $tmparr['A']);
$bear = implode(',', $tmparr['B']);
$car = implode(',', $tmparr['C']);

//Show output
echo "$apple\n$bear\n$car\n"
?>

It can get more complex. If there are more variables being passed in the $_POST, you'll need an IF statement to make sure you're creating the proper tmp array.

Mark
 
Thanks so much - it seems that PHP offers much more functionality when it comes to arrays - a little overwhelming for a beginner :)

You've put me in the right direction - I'm going to follow your example and see if I can get this to work for me.

Again, thanks!

Cheers,
Dipesh.
jollydonkey.
 
There are some great examples of using a function called array_fill to do something like this at php .net.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top