spfoonnewb Posted January 3, 2007 Share Posted January 3, 2007 Is there a way to make this for each statement run for each field, but only if the field is not empty?Basically what it does is if $l is equal to say 5, it displays 5 fields (All options) on the previous page. Then those fields are inputted into this statement. (All in all it echo's one for each)What I want is it to echo one for each, but then if there are any left over, I don't want it to echo those.. ?I tried making an if statement before it, but then it didn't do anything at all..[code]<?php for ($l = 1; $l <= $_POST['l']; ++$l) { $xmldoc .= " <doc>\n"; $xmldoc .= " <title>".$_POST["title$l"]."</title>\n"; $xmldoc .= " <info>".$_POST["info$l"]."</info>\n"; $xmldoc .= " <creator>".$_POST["creator$l"]."</creator>\n"; $xmldoc .= " </doc\n"; }?>[/code] Link to comment https://forums.phpfreaks.com/topic/32700-solved-php-if-not-empty-help/ Share on other sites More sharing options...
papaface Posted January 3, 2007 Share Posted January 3, 2007 This should work:[code]foreach ($_POST as $key => $value){if ($key != empty($value)) { code here }}[/code] Link to comment https://forums.phpfreaks.com/topic/32700-solved-php-if-not-empty-help/#findComment-152177 Share on other sites More sharing options...
spfoonnewb Posted January 3, 2007 Author Share Posted January 3, 2007 Same result, displays nothing, just because ONE of the fields is empty.. but I want it to display the ones that aren't empty first.. Link to comment https://forums.phpfreaks.com/topic/32700-solved-php-if-not-empty-help/#findComment-152189 Share on other sites More sharing options...
trq Posted January 3, 2007 Share Posted January 3, 2007 Post what you have tried. Link to comment https://forums.phpfreaks.com/topic/32700-solved-php-if-not-empty-help/#findComment-152190 Share on other sites More sharing options...
craygo Posted January 3, 2007 Share Posted January 3, 2007 [code]<?php$xmldoc = "<doc>\n";foreach($_POST as $key => $value){ if(!empty($value)){ $xmldoc .= "<$key>$value</$key>\n"; }}$xmldoc .= "</doc>\n";echo $xmldoc;?>[/code]Ray Link to comment https://forums.phpfreaks.com/topic/32700-solved-php-if-not-empty-help/#findComment-152192 Share on other sites More sharing options...
spfoonnewb Posted January 3, 2007 Author Share Posted January 3, 2007 Is it possible to make it work with my for each statement... The values arent like that.. they are like (title1, creator1, info1 | title2, creator2, info2) and there is multiple of them (There is no max..). I could not get either version of those to work the same way.. Link to comment https://forums.phpfreaks.com/topic/32700-solved-php-if-not-empty-help/#findComment-152548 Share on other sites More sharing options...
spfoonnewb Posted January 4, 2007 Author Share Posted January 4, 2007 Ok, this is frustrating... I have remade the code.. please help me make it work.. Each field has a name in order field1, field2, field3, field4, field5, and so on... So I want this script to check if one of them is empty, and if it is skip it, if not post it..[code]<?phpfor ($l = 1; $l <= $_POST['l']; ++$l) { if(!empty($_POST['field$l'])) { $xmldoc .= " <title>".$_POST['field$l']."</title>\n";}}?>[/code] Link to comment https://forums.phpfreaks.com/topic/32700-solved-php-if-not-empty-help/#findComment-153036 Share on other sites More sharing options...
kenrbnsn Posted January 4, 2007 Share Posted January 4, 2007 Variables between single quotes are not expanded.Try:[code]<?phpfor ($l = 1; $l <= $_POST['l']; ++$l) if(!empty($_POST["field$l"])) { $xmldoc .= " <title><![CDATA[".$_POST["field$l"]."]]></title>\n";?>[/code]Your code would be much simpler if you could make the names in the form into arrays:[code]<?phpfor ($l = 1; $l <= $_POST['l']; ++$l) if(!empty($_POST['field'][$l])) { $xmldoc .= " <title><![CDATA[".$_POST['field'][$l]."]]></title>\n";?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/32700-solved-php-if-not-empty-help/#findComment-153039 Share on other sites More sharing options...
spfoonnewb Posted January 4, 2007 Author Share Posted January 4, 2007 Yes, that did work, thanks.. one last question.. is it possible to mix them together like this?(It errors out unexpected } )[code]for ($l = 1; $l <= $_POST['l']; ++$l) { if(!empty($_POST["field$l"])) { if(!empty($_POST["type$l"])) { if(!empty($_POST["location$l"])) { $xmldoc .= " <xml>\n"; $xmldoc .= " <field><![CDATA[".$_POST["field$l"]."]]></field>\n"; $xmldoc .= " <type><![CDATA[".$_POST["type$l"]."]]></type>\n"; $xmldoc .= " <location><![CDATA[".$_POST["location$l"]."]]></location>\n"; $xmldoc .= " </xml>\n"}}}}[/code] Link to comment https://forums.phpfreaks.com/topic/32700-solved-php-if-not-empty-help/#findComment-153055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.