Looping through arrays

This is a discussion on "Looping through arrays" within the Starting Out section. This forum, and the thread "Looping through arrays are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Starting Out

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Apr 21st, 2007, 02:21
New Member
Join Date: Apr 2007
Location: CA
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Cool Looping through arrays

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: 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>
PHP: 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
?>
PHP: 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
Reply With Quote

  #2 (permalink)  
Old Apr 22nd, 2007, 18:43
New Member
Join Date: Apr 2007
Location: CA
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Looping through arrays

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: 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>
PHP: 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
?>
PHP: 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 />';
      } 
Reply With Quote
Reply

Tags
php

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problems with arrays Scream JavaScript Forum 2 Jan 10th, 2008 16:15
PHP Looping script peterboy PHP Forum 3 Mar 10th, 2007 00:04
Unique Array of Arrays ktsirig Other Programming Languages 1 Jan 27th, 2006 16:30
arrays? macupryk ASP.NET Forum 1 Sep 30th, 2003 12:44
looping background music gwx03 Flash & Multimedia Forum 6 Sep 7th, 2003 03:07


All times are GMT. The time now is 12:16.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43