473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Looping through arrays

41
Hello

I have changed the process code abit so it receives
the data from the form and ensures the data in array format.
This has eliminated my previous error.

The problem I am experiencing is the looping is not
displaying the all contents of the arrays.

Do you have any idea what the problem is and how to fix the problem?



[html]
<html>
<head></head>

<body>
<!-----------------------form processor---------------------------->
<form action="../common_list_process.php" method="post">
<table>
<tr>
<td> <input type="submit" name="fee_button" value="Submit"
style="color: #ff6600;font-weight:bold; margin-right: 5;"/> </td>
</tr>

</table>

<?php
display();//display form selection and input boxes
?>

</form>
</body>
</html>

[/html]

[php]

<?php

/***------------display function------------**/
//display form selection and input boxes

function display()
{

$op = array();//create empty array

/****This form consist of multiple rows like this****/
echo "<table>\n";
echo "<tr height=\"10\">\n";
echo "<td width=\"9%\" bgcolor=\"#fff8dc\" align=\"\"><span class=\"style15\">
<input type=\"checkbox\" name=\"choice[]\" value=\"A1\">
<span class=\"style1\" >A1</span></span></td>
<td width=\"2%\" bgcolor=\"#fff8dc\" height=\"10\">
<input type=\"text\" name=\"unit[]\" size=\"1\" maxlength=\"2\" value =\"$a_unit\"/></td>
<td width=\"32%\" bgcolor=\"#ebeae0\" class=\"style11\">General</td>
<td width=\"2%\" bgcolor=\"#fff8dc\" height=\"10\">
<input type=\"text\" name=\"money[]\" size=\"1\" maxlength=\"2\" value =\"$money\"/></td>\n";

echo "<td width=\"9%\" bgcolor=\"#fff8dc\" align=\"\"><span class=\"style15\">
<input type=\"checkbox\" name=\"op[choice][]\" value=\"A7\">
<span class=\"style1\" >A7</span></span></td>
<td width=\"2%\" bgcolor=\"#ebeae0\" height=\"10\">
<input type=\"text\" name=\"op[unit][]\" size=\"1\" maxlength=\"2\" value =\"$a_unit\"/></td>
<td width=\"32%\" bgcolor=\"#ebeae0\" class=\"style11\">Intermediate</td>\n";
<td width=\"2%\" bgcolor=\"#fff8dc\" height=\"10\">
<input type=\"text\" name=\"money[]\" size=\"1\" maxlength=\"2\" value =\"$money\"/></td>\n";
echo "</tr>\n";
echo "</table>\n";

$all[] = choice;
$all[] = unit;
$all[] = money;

return $all;

}

list($choice, $unit, $money) = display(); //unpack array

?>

[/php]



[php]

/***********common_list_process.php*************/

$fee1_choice = $_POST['choice'];
if(is_array($fee1_choice ))
{
$fee1_choice = array_filter($fee1_choice );
}
else
{
$fee1_choice = array("$fee1_choice ");
$fee1_choice = array_filter($fee1_choice);
}
$fee1_unit = $_POST['unit'];
if(is_array($fee1_unit))
{
$fee1_unit = array_filter($fee1_unit);
}
else
{
$fee1_unit = array("$fee1_unit");
$fee1_unit = array_filter($fee1_unit);
}
$fee1_money = $_POST['fee_money'];
if(is_array($fee1_money))
{
$fee1_money = array_filter($fee1_money);
}
else
{
$fee1_money = array("$fee1_money");
$fee1_money = array_filter($fee1_money);
}

/*****This loops the arrays to display the array contents***/

$indices2 = array_keys($fee1_choice);
foreach($indices2 as $index2)
{
//individual value validation from 3 arrays
echo "|". $fee1_choice[$index2];
echo "|". $fee1_unit[$index2];
echo "|". $fee1_money[$index2] .'<br />';
}
[/php]


/*****---result of array contents--*****/
echo '<pre>',print_r ($_POST, TRUE), '</pre>';//check array values

This display the selected data in the arrays

[choice] => Array
(
[0] => A001
[1] => A004
[2] => A008
)

[unit] => Array
(
[0] => 1
[1] =>
[2] => 2
[3] =>
[4] => 3
[5] =>
[6] =>
[146] =>
)

[fee_money] => Array
(
[0] => 17.75
[1] =>
[2] => 30.70
[3] =>
[4] => 10.25
[5] =>
[6] =>



/*****----result of loop-------*****/

|A001|1|17.75
|A004||
|A008|2|30.70
Apr 21 '07 #1
1 1828
assgar
41
Problem Solved.

Using a for loop to incement the array index syncronizes the array indexes.

I think using a two-dimentional array would be better but I don't how to do that yet.

This is the code that resolved the problem.


[html]
<html>
<head></head>

<body>
<!-----------------------form processor---------------------------->
<form action="../common_list_process.php" method="post">
<table>
<tr>
<td> <input type="submit" name="fee_button" value="Submit"
style="color: #ff6600;font-weight:bold; margin-right: 5;"/> </td>
</tr>

</table>

<?php
display();//display form selection and input boxes
?>

</form>
</body>
</html>

[/html]

[php]

<?php

/***------------display function------------**/
//display form selection and input boxes

function display()
{

$op = array();//create empty array

/****This form consist of multiple rows like this****/
echo "<table>\n";

for($i=0; $i < 4; $i++)
{

echo "<tr height=\"10\">\n";
echo "<td width=\"9%\" bgcolor=\"#fff8dc\" align=\"\"><span class=\"style15\">
<input type=\"checkbox\" name=\"choice[$i]\" value=\"A1\">
<span class=\"style1\" >A1</span></span></td>
<td width=\"2%\" bgcolor=\"#fff8dc\" height=\"10\">
<input type=\"text\" name=\"unit[$i]\" size=\"1\" maxlength=\"2\" value =\"$a_unit\"/></td>
<td width=\"32%\" bgcolor=\"#ebeae0\" class=\"style11\">General</td>
<td width=\"2%\" bgcolor=\"#fff8dc\" height=\"10\">
<input type=\"text\" name=\"money[$i]\" size=\"1\" maxlength=\"2\" value =\"$money\"/></td>\n";

echo "<td width=\"9%\" bgcolor=\"#fff8dc\" align=\"\"><span class=\"style15\">
<input type=\"checkbox\" name=\"choice[$i]\" value=\"A7\">
<span class=\"style1\" >A7</span></span></td>
<td width=\"2%\" bgcolor=\"#ebeae0\" height=\"10\">
<input type=\"text\" name=\"unit[$i]\" size=\"1\" maxlength=\"2\" value =\"$a_unit\"/></td>
<td width=\"32%\" bgcolor=\"#ebeae0\" class=\"style11\">Intermediate</td>\n";
<td width=\"2%\" bgcolor=\"#fff8dc\" height=\"10\">
<input type=\"text\" name=\"money[$i]\" size=\"1\" maxlength=\"2\" value =\"$money\"/></td>\n";
echo "</tr>\n";
}

echo "</table>\n";
$all = array();

$all[] = 'choice';
$all[] = 'unit';
$all[] = 'money';

return $all;

}

list($choice, $unit, $money) = display(); //unpack array

?>

[/php]



[php]

/***********common_list_process.php*************/

$fee1_choice = $_POST['choice'];
if(is_array($fee1_choice ))
{
$fee1_choice = array_filter($fee1_choice );
}
else
{
$fee1_choice = array("$fee1_choice ");
$fee1_choice = array_filter($fee1_choice);
}
$fee1_unit = $_POST['unit'];
if(is_array($fee1_unit))
{
$fee1_unit = array_filter($fee1_unit);
}
else
{
$fee1_unit = array("$fee1_unit");
$fee1_unit = array_filter($fee1_unit);
}
$fee1_money = $_POST['fee_money'];
if(is_array($fee1_money))
{
$fee1_money = array_filter($fee1_money);
}
else
{
$fee1_money = array("$fee1_money");
$fee1_money = array_filter($fee1_money);
}

/*****This loops the arrays to display the array contents***/

$indices2 = array_keys($fee1_choice);
foreach($indices2 as $index2)
{
//individual value validation from 3 arrays
echo "|". $fee1_choice[$index2];
echo "|". $fee1_unit[$index2];
echo "|". $fee1_money[$index2] .'<br />';
}
[/php]
Apr 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: paul | last post by:
Hi all, I have a 2D array, which I am trying to access, but I am having problems doing this: If, for instance, I use a nested for loop to go through the values, using something like echo...
4
by: David | last post by:
Hello. I am looking for advice on what is "best practice" regarding looping through a form to check its checkboxes and associated data fields. Here is what I am trying to do (Here is the page...
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
2
by: Amjad | last post by:
I want to copy 1-D arrays to columns of a 2-D array. Is there a way of doing that without using a loop that will copy element by element?
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
11
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
6
by: stefano.troiani | last post by:
Hi, i have two arrays and I would like to loop both of them one inside the other. The first one comes from a readdir and present an array with the document's names, the second one is an array...
4
by: nico3334 | last post by:
I'm having problems with my code concerning a mulitdimensional array. Basically, I'm trying to create an array (arrTotal) which contains all of the data from 2 arrays (arrOne & arrTwo). Basically,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.