Jump to content

[SOLVED] PHP if not empty help..


spfoonnewb

Recommended Posts

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

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..
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]<?php

for ($l = 1; $l <= $_POST['l']; ++$l) {

  if(!empty($_POST['field$l'])) {

$xmldoc .= " <title>".$_POST['field$l']."</title>\n";

}

}

?>[/code]
Variables between single quotes are not expanded.

Try:
[code]<?php
for ($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]<?php
for ($l = 1; $l <= $_POST['l']; ++$l)
  if(!empty($_POST['field'][$l])) {
$xmldoc .= " <title><![CDATA[".$_POST['field'][$l]."]]></title>\n";
?>[/code]

Ken
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]

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.