Jump to content

If field exists


adman4054

Recommended Posts

I want to change the returned value in this php code

function fieldExists($fieldValue){
	$trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue)));
	$fieldExists = (empty($trimValue) ? "No" : "Yes");

I still want to return a value of "No" if there isnt anything in the row, but instead of displaying "yes" I'd like to display the value of the item.

 

Thank you in advance :)

Link to comment
Share on other sites

Maybe thats a better way. What I'm trying to do is to present the value of the row if it exists:

 

<soundPath> www.someurl.com/somefolder/57.mp3</soundPath>

 

If there isnt anything in the row

 

<soundPath>no</soundPath>

 

This is the code, but it isnt working. Would I use the fieldexists within the code or is there a better way. Thanks

$xml .= "\t<soundPath>www.some url.com/somefolder/"($row['soundPath']).  "</soundPath>\n"; 
Link to comment
Share on other sites

$xml .= "\t<soundPath>www.some url.com/somefolder/" . fieldExists($row['soundPath']) . "</soundPath>\n";

 

Appreciate you taking the time.

 

When I use the original code you created for me

function fieldExists($fieldValue){
    $trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue)));
    return empty($trimvalue) ? 'No' : $trimvalue;
}

And the newest part of the code you created

$xml .= "\t<soundPath>www.some url.com/somefolder/" . fieldExists($row['soundPath']) . "</soundPath>\n";

It produced "No" when I ran the script againsy a row that contained data. And I'm looking for just "<soundPath>no</soundPath>" without the url and the folder if there isnt data in the row, is that possible?

 

Thanks Again!!

Link to comment
Share on other sites

Ok, then use

function setValue($fieldValue, $joinValue = ''){
    $trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue)));
    return empty($trimvalue) ? 'No' : $joinValue . $trimvalue;
}

$xml .= "\t<soundPath>" . setValue($row['soundPath'], 'www.some url.com/somefolder/'). "</soundPath>\n";
Link to comment
Share on other sites

Sorry, uppercase"V". Change to

return empty($trimValue) ? 'No' : $trimValue;

If you use the version that returns false

return empty($trimValue) ? false : $trimValue;

you can then

$f = fieldExists($row['soundPath']);
echo "\t<soundPath>" . ($f ? "www.some url.com/somefolder/$f" : "No") . "</soundPath>\n";

Link to comment
Share on other sites

function fieldExists($fieldValue){
	$trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue)));
	//$fieldExists = (empty($trimValue) ? "No" : "Yes");
   return empty($trimValue) ? 'No' : $trimValue;
	//return $fieldExists;
}



function setValue($fieldValue, $joinValue = ''){
    $trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue)));
    return empty($trimValue) ? 'No' : $joinValue . $trimValue;
}

$f = fieldExists($row['soundPath']);
$xml .=  "\t<soundPath>" . ($f ? "www.someurl.com/somefolder/$f" : "No") . "</soundPath>\n";

Really appreciate the assistance and its almost working. I had to remove the "echo" or I couldn't find the right place for it. If the field is populated, it works perfect. If there isn't anything in the row, it gives me the URL, which I don't want, followed by "No". Again, appreciate the help and as you can see fairly new to PHP.

Link to comment
Share on other sites

Hope I'm not pushing my luck here. You guys have been very helpful and the earlier solutions work great when using a URL.

 

I'm receiving errors when I use the same code when just pulling the row (no URL), in the first example and I dont think I have the syntax right in the second. The second should show www.someurl.com/Download.php?listID=1234&no=1

 

Thanks again for all the help! :happy-04:

First Example
$f = fieldExists($row['file1Title']);
    $xml .= "\t<file1Title>" . ($f ? "($row['file1Title'])$f" : "No") . "</file1Title>\n";

Second Example	
	$f = fieldExists($row['file1Path']);
$xml .= "\t<file1Path>" . ($f ? "www.someurl.com/Download.php?listID= . ($row['listingID']) . "&no=1 $f"  : "No") . "</file1Path>\n";
Link to comment
Share on other sites

you missed a closing " after ListID= and it looks like you don't need the second $f

$xml .= "\t<file1Path>" . ($f ? "www.someurl.com/Download.php?listID=" . ($row['listingID']) . "&no=1 $f"  : "No") . "</file1Path>\n";
                                                                     ^                                ^
                                                                     |                                |
                                                                     add                            remove

It looks to me from your column names that your db table contains multiple columns like File1Path, File2Path,..., FileNPath and similarly with FIle1Tile etc.

 

DON'T!

 

Normalize the data. Store the data for the file in a separate table, one row per file. Only store where the file exists. That completely removes this problem you are having to check whether each field contains data or not.

 

The XML also shouldn't have numbered tags (FIle1Path etc), better to have something like

<Files>
  <File seq="1">
    <Name>xxx</Name>
    <Title>yyy</Title>
  </File>
  <File seq="2">
    <Name>www</Name>
    <Title>zzz</Title>
  </File>
</Files>
Link to comment
Share on other sites

Thanks for the reply. Agree with changing the field names and I'll do that when I get this running. The first example still wont run and the second, when I make the changes you suggest, returns a value of "No" when there is data present, any idea what I'm doing wrong? Thanks again!

 

Also, something else came up last night during testing. This YouTube embed code returns the value twice. The extra <![CDATA[  and extra ] is required by the cms we are importing into

$f = fieldExists($row['youTubeEmbed']);
$xml .= "\t<youTubeEmbed>" . ($f ? "<![CDATA[" . ($row['youTubeEmbed']) . "]]>$f" : "No") . "</youTubeEmbed>\n";

Appreciate you helping me out, I'm learning a great deal and hopefully someone else will with this thread!

Link to comment
Share on other sites

When I emulate your second example with

function fieldExists($fieldValue){
    $trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue)));
    return empty($trimValue) ? false : $trimValue;
}

$xml = '';
$row= array (
    'file1Path' => 'something',
    'listingID' => '1234'
    );  
      
$f = fieldExists($row['file1Path']);
$xml .= "\t<file1Path>" . ($f ? "www.someurl.com/Download.php?listID=" . ($row['listingID']) . "&no=1"  : "No") . "</file1Path>\n";

I get

<file1Path>www.someurl.com/Download.php?listID=1234&no=1</file1Path> 

As for your latest UTube problem, $f will contain $row['youTubeEmbed'] so you are outputting it twice. You need

$f = fieldExists($row['youTubeEmbed']);
$xml .= "\t<youTubeEmbed>" . ($f ? "<![CDATA[\"$f\"]]>" : "No") . "</youTubeEmbed>\n";
Link to comment
Share on other sites

Perfect, thank you, I had a field name wrong on the second example. Both solutions work perfectly. I have been using your examples to try and figure out what is wrong with my example #1, if you have any time left, can you look at example #1 and tell me what Ive done wrong? So much thank you's :)

Link to comment
Share on other sites

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.