adman4054 Posted December 16, 2013 Share Posted December 16, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/ Share on other sites More sharing options...
Barand Posted December 16, 2013 Share Posted December 16, 2013 function fieldExists($fieldValue){ $trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue))); return empty($trimvalue) ? 'No' : $trimvalue; } Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462449 Share on other sites More sharing options...
scootstah Posted December 16, 2013 Share Posted December 16, 2013 Using strings for "no" or "yes" really make no sense at all. This is exactly what booleans are for (true/false). $fieldExists = (empty($trimValue) ? false : $trimValue); Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462450 Share on other sites More sharing options...
adman4054 Posted December 16, 2013 Author Share Posted December 16, 2013 function fieldExists($fieldValue){ $trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue))); return empty($trimvalue) ? 'No' : $trimvalue; } Thank you Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462456 Share on other sites More sharing options...
Barand Posted December 16, 2013 Share Posted December 16, 2013 I agree with Scootstah though, it would be better to have return empty($trimvalue) ? false : $trimvalue; Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462460 Share on other sites More sharing options...
adman4054 Posted December 17, 2013 Author Share Posted December 17, 2013 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"; Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462515 Share on other sites More sharing options...
Barand Posted December 17, 2013 Share Posted December 17, 2013 $xml .= "\t<soundPath>www.some url.com/somefolder/" . fieldExists($row['soundPath']) . "</soundPath>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462522 Share on other sites More sharing options...
adman4054 Posted December 17, 2013 Author Share Posted December 17, 2013 $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!! Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462536 Share on other sites More sharing options...
Ch0cu3r Posted December 17, 2013 Share Posted December 17, 2013 If $row['soundPath'] contains the actual path then use $xml .= "\t<soundPath>" . fieldExists($row['soundPath']) . "</soundPath>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462537 Share on other sites More sharing options...
adman4054 Posted December 17, 2013 Author Share Posted December 17, 2013 Hi -- <soundPath> only contains the file name, so Im trying to add the url when the file(row) exists, if it doesnt exist it would just show "<soundPath>no</soundPath>" Thank you Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462541 Share on other sites More sharing options...
Ch0cu3r Posted December 17, 2013 Share Posted December 17, 2013 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"; Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462544 Share on other sites More sharing options...
Barand Posted December 17, 2013 Share Posted December 17, 2013 (edited) 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"; Edited December 17, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462547 Share on other sites More sharing options...
adman4054 Posted December 18, 2013 Author Share Posted December 18, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462634 Share on other sites More sharing options...
Barand Posted December 18, 2013 Share Posted December 18, 2013 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"; Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462635 Share on other sites More sharing options...
adman4054 Posted December 18, 2013 Author Share Posted December 18, 2013 Thanks, I need to add: $xml .= in order for it to show on export, where does would that be placed? Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462636 Share on other sites More sharing options...
Ch0cu3r Posted December 18, 2013 Share Posted December 18, 2013 Then change echo to $xml .= Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462654 Share on other sites More sharing options...
adman4054 Posted December 19, 2013 Author Share Posted December 19, 2013 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! 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"; Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462711 Share on other sites More sharing options...
Barand Posted December 19, 2013 Share Posted December 19, 2013 (edited) 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> Edited December 19, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462720 Share on other sites More sharing options...
adman4054 Posted December 19, 2013 Author Share Posted December 19, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462725 Share on other sites More sharing options...
Barand Posted December 19, 2013 Share Posted December 19, 2013 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"; Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462726 Share on other sites More sharing options...
adman4054 Posted December 19, 2013 Author Share Posted December 19, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462727 Share on other sites More sharing options...
Barand Posted December 19, 2013 Share Posted December 19, 2013 Same thing. You are outputting $row['file1title'] and $f - which are the same if the title contains data. Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462734 Share on other sites More sharing options...
Solution adman4054 Posted December 19, 2013 Author Solution Share Posted December 19, 2013 Everything is working GREAT! Appreciate your help and your patience. I learned a great deal from you. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/284796-if-field-exists/#findComment-1462742 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.