Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_php thread: RE: Looping TWO dimension array HELP II


Message #1 by "jorge" <jorge@d...> on Tue, 29 Oct 2002 20:12:13 -0600
Thanks Nik
let me explain

in the previous form i have created the inputfields as follows

the client chosses how many months will take to sell his inventory of
houses of a development (Development_ID
then he will forecast his sales through that period some months are better
than other

for($i=0;$i<$how_many_months;$i++)
    {
    <tr><td>MONTH<input type="text" name
"sales_forecast[months][]"></td></tr>
<tr><td>YEAR<input type="text" name= "sales_forecast[year][]"></td></tr>
<tr><td>SALES<input type="text" name= "sales_forecast[sales][]"></td></tr>
<tr><td>Product ID<input type="text" name= "[development_id][]"></td></tr>
    }
....(submit)..(form)...bla...
//As you have recomended i am using arrays to organize every thing is fine
until i get this type of dynamic tables

in the next page i want to run a one to check for the input content (not
included) another one to trim strip an htmlspecialchars and to insert
this works perfecly in one dimensional array where the array arrive gruoped
as sales_forescasrt[amount], sales_forecast[month] etc
so i just use the function insertar_tabla whixh means insert_table
I have been using print_r and have seen how the array are,_POST[x][y]
they are listed indexed one array after the other, first all the elements of
array since i use as name of the box the exact name as per the table i save
a lot of typing
but i cant figure out how to do it so i can input them in a similar manner
as with the function i use for one dimensional arrays
Nik
//this function checks for content and cleans
function insertar_tabla($array_to_insert,$table)
 {
 //echo("<br>Estoy registrando");
 global $mete;
  foreach($array_to_insert AS $key => $value)
   {
    $value=strip_tags($value, $tags);
    $value=htmlspecialchars($value);
    $value=trim($value);
    //echo("<br>".$key." ".$value);
    $keys[]=$key;
    $values[]=$value;

   }

//comenzamos en el segundo elemento el auto increment no pasa => segundo
elemento fecha_reg
  for($i=1;$i<$cuantos;$i++)
   {
   $joe=($keys[$i]."="."'$values[$i]'");
   $tom=$tom.",".$joe;
   }
  //esto es para que el primer campo no tenga coma
  $jim=($keys[0]."="."'$values[0]'");
  //concatenamos en una sola variable
  $insert_str=$jim.$tom;
  echo("<br>who = ".$insert_str);
  //$fecha_doc=date("Y-m-d");
  if(!($qitable=mysql_query("INSERT INTO $table SET $insert_str")))
   {
   echo("<br>Error en al insertar en  ".$table." ".mysql_error());
   }else{$msg="Su información";}

   $mete=mysql_insert_id();
   return($mete);
  }//FIN DE LA FUNCION






----- Original Message -----
From: "Nikolai Devereaux" <yomama@u...>
To: "professional php" <pro_php@p...>
Sent: Tuesday, October 29, 2002 5:01 PM
Subject: [pro_php] RE: Looping through TWO dimension array HELP


>
> You're trying to overwrite the value of $_POST in first foreach() loop.
>
> Also, your loop leads me to believe that your $_POST array looks something
like
> this:
>
>
> _POST: Array
> (
>   [key1] =>  Array
>   (
>      [0]   =>  valueA
>      [1]   =>  valueB
>      [2]   =>  valueC
>   )
>   [key2] =>  Array
>   (
>      [0]   =>  valueD
>      [1]   =>  valueE
>      [2]   =>  valueF
>   )
> )
>
>
> The reason I say this is because you only extract the key part of the
key/value
> pair in the first level of nesting.  Therefore, I can only guess that the
keys
> in your nested arrays are unimportant, and therefore just numeric.
>
> If this is the case, you'll want this kind of loop:
>
> foreach($_POST as $key => $nested)
> {
>   foreach($nested as $value)
>   {
>      ...
>   }
> }
>
>
> Furthermore, since you're getting an error message in the 2nd foreach()
call,
> I'm led to believe that $_POST is no longer an array -- that is, when you
> overwrite $_POST in the outer foreach() loop, you're assigning a scalar
value.
> A scalar value just means "not an array", in this context.
>
>
> You should make heavy use of the print_r() function to fully understand
the
> structure of your $_POST array before attempting to traverse it looking
for
> specific key/value pairs.
>
>
> Hope this helps,
>
> Nik
>
>



  Return to Index