Looping through 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

Post Reply
assgar
Forum Commoner
Posts: 46
Joined: Fri Apr 20, 2007 9:00 pm

Looping through arrays

Post by assgar »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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?

Code: Select all

<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>

Code: Select all

<?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

?>

Code: Select all

/***********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 />';
      }

/*****---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


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Turn error_reporting(E_ALL); as you have a couple problems with your code that you may not be aware of.

Code: Select all

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

  return $all;
What are you expecting this to do? Are those supposed to be strings? Again, you'll get a notice for not intializing your array.. it should either be:

Code: Select all

$all = array();
$all[] = 'choice';
$all[] = 'unit';
$all[] = 'money';

//or 

$all = array('choice', 'unit', 'money');
We'll start from here, and move forward after we've resolved this.
assgar
Forum Commoner
Posts: 46
Joined: Fri Apr 20, 2007 9:00 pm

Looping through arrays

Post by assgar »

Fantastic, thanks your suggestion worked.

:D
assgar
Forum Commoner
Posts: 46
Joined: Fri Apr 20, 2007 9:00 pm

Looping through arrays

Post by assgar »

Hi

I was mistaken the problem appeared solved and then returned.

I ran error_reporting(E_ALL); the result can be seen below:


|A001|7|17.75
|A004
Notice: Undefined offset: 1 in D:\server\webroot\common_list_process.php on line 643
|
Notice: Undefined offset: 1 in D:\server\webroot\common_list_process.php on line 644
|
|A008|4|30.70
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I don't know why I hadn't moved this previously.

Moved to PHP-Code.
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 »

So what's on lines 643 and 644
assgar
Forum Commoner
Posts: 46
Joined: Fri Apr 20, 2007 9:00 pm

Post by assgar »

Thanks for the suggestions.

I have conducted further investigation of my problem to narrow down the problem to:

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

|A001|1|17.75
|A004||
|A008|2|30.70


/***** result should be *******/

|A001|1|17.75
|A004|2|30.70
|A008|3|10.25
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

aaronhall wrote:So what's on lines 643 and 644
Well?
assgar
Forum Commoner
Posts: 46
Joined: Fri Apr 20, 2007 9:00 pm

solved

Post by assgar »

Hi

Thanks for responding.

The two line refer to the looping.

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.

Code: Select all

<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>

Code: Select all

<?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

?>

Code: Select all

/***********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 />';
      }
Post Reply