Forum Moderators: coopster

Message Too Old, No Replies

check for empty field

         

ploppy

9:19 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



hi

i have a script that creates a link in a member area when there are files for them to download. what i am trying to do is if there are no files, then instead of the 'Download File' link there is a text messag like 'No files available'. I have tried various things but keep getting erroring. the latest was this piece of code that works in that it displays the message but dosen't put it in place of the download link.

if ($file ==''){$file="sorry I got nothing for you"; }
here is the code and if someone could cast an eye over it and tell me where i am going wrong i would be grateful. The code works fine and does what i need it to but i need this function so that if there are no files the link isn't visible.

function ShowMyFiles() {

// vars global configuration
global $dbConn, $theme_path;

// vars messages
global $msg;

// vars template
global $error_msg, $username, $status, $owner, $files,$test;

if ($err) {
$error_msg = $msg['20191'];
}


//$links_obj = new clsLink;

// get file listing
$query = "select * from idx_files where username = '".$_COOKIE[COOKIE_USERNAME]."'";
//$links_obj->query = $query;
//$links_obj->table_name = "idx_users";
// $links_obj->date_format = $msg["10151"];
//$links_obj->max_rows = 100;
//$files = $links_obj->Display();

$query = "select * from idx_files where username = '".$_COOKIE[COOKIE_USERNAME]."'";
$result = $dbConn->Execute($query);
//$files = $result->Fields("url");
// $owner = $result->Fields("owner");
// $date = $result->Fields("date");

// $files = explode ( " ", $files );

$test = '';
for($i = 0;$i < $result->RecordCount(); $i++){
$file = $result->Fields("url");
if ($file ==''){
$file="sorry I got nothing for you";
}
$date = $result->Fields("date");
$messages = $result->Fields("messages");
$test .= '<table width="100%" border="0">
<tr>
<td>Latest files and Messages</td>
</tr>
</table>

<table cellpadding="2" cellspacing="1" border="0" align="center" width="100%" class="tbl_border_mem">
<tr class="tbl_caption_mem">
<td width="16%"><strong>Files</strong></td>
<td width="13%"><strong>Date</strong></td>
<td width="71%"><strong>Messages</strong></td>
</tr>
<tr class="tbl_caption_mem">
if ($file==''){
<td valign="top">[ sorry no files ]</td>
}
else {
<td valign="top">[ <a href="'.$file.'">Download File</a> ]</td>
<td valign="top">'.$date.'</td>
<td valign="top">'.$messages.'</td>
</tr>
</table><br />';

$result->movenext();
}
$files = $test;
DisplayTemplate($theme_path . "cp/myfiles.html", "\$files,\$error_msg");
}

many thanks for help.

ploppy

9:42 am on Oct 4, 2007 (gmt 0)

10+ Year Member



bump

Habtom

9:47 am on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$file = $result->Fields("url");
if ($file ==''){
$file="sorry I got nothing for you";
}

I think you are donig it well. What is the error you are getting?

One comment though, once you run the if condition, don't repeat it back again. if file was initially empty, at the first condition then it won't be empty anymore as it is being asigned to some value.

If you are not getting the result you wanted, you need to check if $file = $result->Fields("url"); is getting you any output or not.

Post back with the specific problem you are facing.

Habtom

ploppy

11:35 am on Oct 4, 2007 (gmt 0)

10+ Year Member



basically habtom. what i am trying to achieve is if there are no files to download (url field is empty) then remove the line that says 'Download Files' and replace with 'no files available'. What is happening is that the entry

 $file = $result->Fields("url");
if ($file ==''){
$file="sorry I got nothing for you";
}

is becoming part of the url. ie, [domain.com...] I got nothing for you. this is not what i want.

many thanks

PHP_Chimp

11:42 am on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are changing the $file variable to your error message. As you are using the $file to make your url you are getting the error in the url.
Use another variable like
$error = 'got nothing';
Then check if that variable is set and echo your error message i.e.

if ($file ==''){
$error="sorry I got nothing for you";
}
if (isset($error)) {
echo $error;
}

You may be able to add that to the global errors that you already have.

[edited by: PHP_Chimp at 11:43 am (utc) on Oct. 4, 2007]

ploppy

11:47 am on Oct 4, 2007 (gmt 0)

10+ Year Member



thanks php_chimp. although it displays the error msg, it still leaves the 'Download File' there in the table. thanks

PHP_Chimp

12:06 pm on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry need some sleep/coffee :) I hate lack of indents in the code here.

$test = '';
for($i = 0;$i < $result->RecordCount(); $i++){
$file = $result->Fields("url");
if ($file ==''){
$file="sorry I got nothing for you";
}
Are you missing } here? To close the for statment.
$date = $result->Fields("date");
$messages = $result->Fields("messages");
$test .= '<table width="100%" border="0">
<tr>
<td>Latest files and Messages</td>
</tr>
</table>

<table cellpadding="2" cellspacing="1" border="0" align="center" width="100%" class="tbl_border_mem">
<tr class="tbl_caption_mem">
<td width="16%"><strong>Files</strong></td>
<td width="13%"><strong>Date</strong></td>
<td width="71%"><strong>Messages</strong></td>
</tr>
<tr class="tbl_caption_mem">

if ($file==''){
<td valign="top">[ sorry no files ]</td>
}

else {
<td valign="top">[ <a href="'.$file.'">Download File</a> ]</td>
<td valign="top">'.$date.'</td>
<td valign="top">'.$messages.'</td>
</tr>
</table><br />';

You have checked for a lack of $file twice. The second one adds your error message to the table, so isnt this what you wanted? Cant you get rid of the first check?

[edited by: PHP_Chimp at 12:09 pm (utc) on Oct. 4, 2007]

ploppy

12:30 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



thanks php_chimp. that did the trick. now go get some caffeine :-) many thanks