Jump to content

for foreach each or ...? *SOLVED*


enkidu72

Recommended Posts

Hello all ,
I have several if like those :
if(!isset($_POST['criterio_1'])){$_POST['criterio_1'] = ""; }
if(!isset($_POST['corrispondenza_1'])){ $_POST['corrispondenza_1'] = ""; }
if(!isset($_POST['criterio_2'])){$_POST['criterio_2'] = ""; }
if(!isset($_POST['criterio_2'])){$table="";}

I'd like to change them      for something like :


$array  = array(
$_POST['criterio_1'] ,
$_POST['corrispondenza_1'],
$_POST['criterio_2']

);

$var=each($array);
if (!isset($var)){ $var="";}


But I have no success :D

Someone could help ?

Thx in advance

David
Link to comment
https://forums.phpfreaks.com/topic/22157-for-foreach-each-or-solved/
Share on other sites

This will create variable $criterio_1,  $corrispondenza_1 and  $criterio_2
[code]<?php
$array  = array(
      'criterio_1' ,
      'corrispondenza_1',
      'criterio_2'     
  );
 
foreach ($array as $item) {
    if (!isset($_POST[$item]))
        $$item = '';
    else
        $$item = $_POST[$item];
}
?>[/code]
It's possible that :


if (!isset($_POST[$item]))
        $$item = '';
       
has to  be :

if (!isset($_POST[$item])){$_POST[$item]=''}  ?

anyway seems that i have some problem with

else
        $$item = $_POST[$item];


because I always have a notice of undefined variable  ...




Maybe someone is interested in this ...
Finally I figured out that has to be :

[code]
<?

$array  = array(
      'criterio_1' ,
      'corrispondenza_1',
      'criterio_2'     
  );
 
foreach ($array as $item) {
    if (!isset($_POST[$item])){$_POST[$item] = ' ';$$item=' '
    }else{ $$item = $_POST[$item];
    }
}

?>



[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.