By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

testing for no data in a string

P: n/a
How do I test for no data in a string? I tried if !(isset($data)) and
$data =="" and neither one returns the message when there are no
records found. Or is it a better idea to do the test at the query
level and not let the user get a worksheet with no records? The thing
is a user could click the button for excel output even if there are no
records on the web page, so I would think in that case they would
expect a blank worksheet anyways.

$data .= $header;
if ($data == "") {
$data = "\n(0) Records Found!\n";}
header("Content-type: application/xmsdownload");
header("Content-Disposition: attachment; filename=".
$file_name.=now().".xls");
header("Pragma: no-cache");
header("Expires; 0");
print "$header\n$data";
the other question is how do I get it to print the date on the
filename in excel?

tia,

------------

this is the function for $data

function makexcldata($xclfields, $result){

$str1 = '';
for($i=0; $i < sizeof($xclfields); $i++){
$str1 .= $xclfields[$i];
$str1 .= "\t";
}
$str1 .= "\n";

while($row = mysql_fetch_row($result)) {
$line = '';
foreach($row as $value)
{
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}

$data = str_replace("\r","",$data);
$data .= $str1;

return $data;
}
Sep 18 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
AqD
On Sep 18, 8:56*am, JRough <jlro...@yahoo.comwrote:
How do I test for no data in a string? *I tried if !(isset($data)) and
$data =="" *and neither one returns the message when there are no
records found. *Or is it a better idea to do the test at the query
level and not let the user get a worksheet with no records? *The thing
is a user could click the button for excel output even if there are no
records on the web page, so I would think in that case they would
expect a blank worksheet anyways.
$data === "" or strlen($data) == 0 should work for you; isset is only
to check if the variable exists (set by you).

But there seems to be a problem in your design. If the page queries
and displays records from DB and provides a button to export excel,
usually the export action should do the queries again (un-optimized
method).
>
$data .= *$header;
* * * * if ($data == "") {
* * * * * * * * * * * * $data = "\n(0) Records Found!\n";}
* * * * header("Content-type: application/xmsdownload");
* * * * header("Content-Disposition: attachment; filename=".
$file_name.=now().".xls");
* * * * header("Pragma: no-cache");
* * * * header("Expires; 0");
* * * * print "$header\n$data";

the other question is how do I get it to print the date on the
filename in excel?
you need to use date() with custom format. Don't rely on the default
date/time format, even if it works for now.
>
tia,

------------

this is the function for $data

function makexcldata($xclfields, $result){

* * * * * *$str1 = '';
* * * * * * * * for($i=0; $i < sizeof($xclfields); $i++){
* * * * * * * * * * * * $str1 .= $xclfields[$i];
* * * * * * * * * * * * $str1 .= "\t";
* * * * * * * * * * * * }
* * * * $str1 .= "\n";

while($row = mysql_fetch_row($result)) {
* * $line = '';
* * foreach($row as $value)
{
* * * * if ((!isset($value)) OR ($value == "")) {
* * * * * * $value = "\t";
* * * * } else {
* * * * * * $value = str_replace('"', '""', $value);
* * * * * * $value = '"' . $value . '"' . "\t";
* * * * }
* * * * $line .= $value;
* * }
* * $data .= trim($line)."\n";

}

$data = str_replace("\r","",$data);
$data .= $str1;

return $data;

}
Sep 18 '08 #2

P: n/a
JRough wrote:
How do I test for no data in a string? I tried if !(isset($data)) and
$data =="" and neither one returns the message when there are no
records found. Or is it a better idea to do the test at the query
level and not let the user get a worksheet with no records? The thing
is a user could click the button for excel output even if there are no
records on the web page, so I would think in that case they would
expect a blank worksheet anyways.

$data .= $header;
if ($data == "") {
Just

if (!$data) {

will suffice. It's better to test for empty vars specifically with:

empty($var);
$data = "\n(0) Records Found!\n";}
header("Content-type: application/xmsdownload");
header("Content-Disposition: attachment; filename=".
$file_name.=now().".xls");
header("Pragma: no-cache");
header("Expires; 0");
print "$header\n$data";
the other question is how do I get it to print the date on the
filename in excel?

tia,

------------

this is the function for $data

function makexcldata($xclfields, $result){
At this point, check how many records exist in result set, if any:

if ( !mysql_num_rows ) {
return false;
}
$str1 = '';
for($i=0; $i < sizeof($xclfields); $i++){
$str1 .= $xclfields[$i];
$str1 .= "\t";
}
$str1 .= "\n";

while($row = mysql_fetch_row($result)) {
$line = '';
foreach($row as $value)
{
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}

$data = str_replace("\r","",$data);
$data .= $str1;

return $data;
}
--
Curtis
Sep 18 '08 #3

P: n/a
Curtis wrote:
JRough wrote:
>How do I test for no data in a string? I tried if !(isset($data)) and
$data =="" and neither one returns the message when there are no
records found. Or is it a better idea to do the test at the query
level and not let the user get a worksheet with no records? The thing
is a user could click the button for excel output even if there are no
records on the web page, so I would think in that case they would
expect a blank worksheet anyways.

$data .= $header;
if ($data == "") {

Just

if (!$data) {

will suffice. It's better to test for empty vars specifically with:

empty($var);
> $data = "\n(0) Records Found!\n";}
header("Content-type: application/xmsdownload");
header("Content-Disposition: attachment; filename=".
$file_name.=now().".xls");
header("Pragma: no-cache");
header("Expires; 0");
print "$header\n$data";
the other question is how do I get it to print the date on the
filename in excel?

tia,

------------

this is the function for $data

function makexcldata($xclfields, $result){

At this point, check how many records exist in result set, if any:

if ( !mysql_num_rows ) {
return false;
}
Sorry, brain fart.

if ( !mysql_num_rows($result) ) {
return false;
}
> $str1 = '';
for($i=0; $i < sizeof($xclfields); $i++){
$str1 .= $xclfields[$i];
$str1 .= "\t";
}
$str1 .= "\n";

while($row = mysql_fetch_row($result)) {
$line = '';
foreach($row as $value)
{
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}

$data = str_replace("\r","",$data);
$data .= $str1;

return $data;
}

--
Curtis
Sep 18 '08 #4

P: n/a
On Sep 17, 6:39*pm, Curtis <dye...@gmail.comwrote:
Curtis wrote:
JRough wrote:
How do I test for no data in a string? *I tried if !(isset($data)) and
$data =="" *and neither one returns the message when there are no
records found. *Or is it a better idea to do the test at the query
level and not let the user get a worksheet with no records? *The thing
is a user could click the button for excel output even if there are no
records on the web page, so I would think in that case they would
expect a blank worksheet anyways.
$data .= *$header;
* * if ($data == "") {
Just
if (!$data) {
will suffice. It's better to test for empty vars specifically with:
empty($var);
* * * * * * $data = "\n(0) Records Found!\n";}
* * header("Content-type: application/xmsdownload");
* * header("Content-Disposition: attachment; filename=".
$file_name.=now().".xls");
* * header("Pragma: no-cache");
* * header("Expires; 0");
* * print "$header\n$data";
the other question is how do I get it to print the date on the
filename in excel?
tia,
------------
this is the function for $data
function makexcldata($xclfields, $result){
At this point, check how many records exist in result set, if any:
if ( !mysql_num_rows ) {
* *return false;
}

Sorry, brain fart.

if ( !mysql_num_rows($result) ) {
* * return false;

}
* * * *$str1 = '';
* * * * for($i=0; $i < sizeof($xclfields); $i++){
* * * * * * $str1 .= $xclfields[$i];
* * * * * * $str1 .= "\t";
* * * * * * }
* * $str1 .= "\n";
while($row = mysql_fetch_row($result)) {
* * $line = '';
* * foreach($row as $value)
{
* * * * if ((!isset($value)) OR ($value == "")) {
* * * * * * $value = "\t";
* * * * } else {
* * * * * * $value = str_replace('"', '""', $value);
* * * * * * $value = '"' . $value . '"' . "\t";
* * * * }
* * * * $line .= $value;
* * }
* * $data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
$data .= $str1;
return $data;
}

--
Curtis
thanks for your answers. Are you saying check for the empty variable
and if it is empty then check if (!mysql_num_rows) is false
Doing both or only one or the other?

Sep 18 '08 #5

P: n/a
On Sep 17, 6:39*pm, Curtis <dye...@gmail.comwrote:
Curtis wrote:
JRough wrote:
How do I test for no data in a string? *I tried if !(isset($data)) and
$data =="" *and neither one returns the message when there are no
records found. *Or is it a better idea to do the test at the query
level and not let the user get a worksheet with no records? *The thing
is a user could click the button for excel output even if there are no
records on the web page, so I would think in that case they would
expect a blank worksheet anyways.
$data .= *$header;
* * if ($data == "") {
Just
if (!$data) {
will suffice. It's better to test for empty vars specifically with:
empty($var);
* * * * * * $data = "\n(0) Records Found!\n";}
* * header("Content-type: application/xmsdownload");
* * header("Content-Disposition: attachment; filename=".
$file_name.=now().".xls");
* * header("Pragma: no-cache");
* * header("Expires; 0");
* * print "$header\n$data";
the other question is how do I get it to print the date on the
filename in excel?
tia,
------------
this is the function for $data
function makexcldata($xclfields, $result){
At this point, check how many records exist in result set, if any:
if ( !mysql_num_rows ) {
* *return false;
}

Sorry, brain fart.

if ( !mysql_num_rows($result) ) {
* * return false;

}
* * * *$str1 = '';
* * * * for($i=0; $i < sizeof($xclfields); $i++){
* * * * * * $str1 .= $xclfields[$i];
* * * * * * $str1 .= "\t";
* * * * * * }
* * $str1 .= "\n";
while($row = mysql_fetch_row($result)) {
* * $line = '';
* * foreach($row as $value)
{
* * * * if ((!isset($value)) OR ($value == "")) {
* * * * * * $value = "\t";
* * * * } else {
* * * * * * $value = str_replace('"', '""', $value);
* * * * * * $value = '"' . $value . '"' . "\t";
* * * * }
* * * * $line .= $value;
* * }
* * $data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
$data .= $str1;
return $data;
}

--
Curtis
thanks for your answers it was helpful.
By the way, I did the queries twice once for the html part and once
for the part where the excel button is sent.
the code was really long so I didn't include it.
Are you saying check for the empty variable empty($var);
and if it is empty then do the check if (!mysql_num_rows) is false?
Doing both or only one or the other? Or is that mysql_num_rows check
for if I redid the queries? tia,
Sep 18 '08 #6

P: n/a
JRough wrote:
On Sep 17, 6:39 pm, Curtis <dye...@gmail.comwrote:
>Curtis wrote:
>>JRough wrote:
How do I test for no data in a string? I tried if !(isset($data)) and
$data =="" and neither one returns the message when there are no
records found. Or is it a better idea to do the test at the query
level and not let the user get a worksheet with no records? The thing
is a user could click the button for excel output even if there are no
records on the web page, so I would think in that case they would
expect a blank worksheet anyways.
$data .= $header;
if ($data == "") {
Just
if (!$data) {
will suffice. It's better to test for empty vars specifically with:
empty($var);
$data = "\n(0) Records Found!\n";}
header("Content-type: application/xmsdownload");
header("Content-Disposition: attachment; filename=".
$file_name.=now().".xls");
header("Pragma: no-cache");
header("Expires; 0");
print "$header\n$data";
the other question is how do I get it to print the date on the
filename in excel?
tia,
------------
this is the function for $data
function makexcldata($xclfields, $result){
At this point, check how many records exist in result set, if any:
if ( !mysql_num_rows ) {
return false;
}
Sorry, brain fart.

if ( !mysql_num_rows($result) ) {
return false;

}
>>> $str1 = '';
for($i=0; $i < sizeof($xclfields); $i++){
$str1 .= $xclfields[$i];
$str1 .= "\t";
}
$str1 .= "\n";
while($row = mysql_fetch_row($result)) {
$line = '';
foreach($row as $value)
{
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
$data .= $str1;
return $data;
}
--
Curtis
thanks for your answers it was helpful.
By the way, I did the queries twice once for the html part and once
for the part where the excel button is sent.
the code was really long so I didn't include it.
Are you saying check for the empty variable empty($var);
and if it is empty then do the check if (!mysql_num_rows) is false?
Doing both or only one or the other? Or is that mysql_num_rows check
for if I redid the queries? tia,
I was just explaining what empty did, it's not really necessary for
this specific case. mysql_num_rows() should be used in your function
that returns $headers. If the amount of rows in the result set from
your query is 0, or it returned false, then we just return false in
the function. You should test $headers, and if it evaluates to false,
then you don't have any results.

--
Curtis
Sep 18 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.