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
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]
Link to comment
Share on other sites

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




Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.