Jump to content


Photo

for foreach each or ...? *SOLVED*


  • Please log in to reply
4 replies to this topic

#1 enkidu72

enkidu72
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 26 September 2006 - 08:31 PM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 26 September 2006 - 08:51 PM

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 enkidu72

enkidu72
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 26 September 2006 - 10:28 PM

Thx Barand !

#4 enkidu72

enkidu72
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 27 September 2006 - 11:23 AM

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






#5 enkidu72

enkidu72
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 03 October 2006 - 07:21 AM

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

<?

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

?>








0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users