473,486 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

testing for no data in a string

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
6 1314
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5787
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
3
2279
by: Andrew | last post by:
Hi, I am thinking about ways for efficient techniques for isolation testing. Here is the problem as I see it: Complex OO systems are designed with massive number of dependencies between modules...
4
510
by: Nathon Dalton | last post by:
I need to know what the appropriate way to test for nothing in several different types of data types is. Here are some examples For string I use this If Not abc.text Is Nothing AndAlso Not...
6
3219
by: Shabam | last post by:
A web application of mine developed using C# + MS SQL runs fine normally. However when I stress test it with a load testing software (using about 60 simultaneous users) some instances start...
0
377
by: Mairead O' Donovan | last post by:
From: davidkeaveny@gmail.com (CtrlAltDel) Newsgroups: microsoft.public.dotnet.datatools Subject: Unit-testing applications that use the Microsoft.Patterns.EnterpriseLibrary.Data Date: 15 Apr 2005...
7
2137
by: Filips Benoit | last post by:
Hi, TBL_CONTACT_PERSON CNTP_ID (auto) CNTP_LAST_NAME (required = yes) CNTP_FUNCTION (required = no) CNTP_..... (all required = no) FRM_CONTACT_PERSON_ADD_NEW Property DATA ENTRY = YES
5
2715
by: Brian | last post by:
Hello all.. Am working on an Air Hockey game... have an table loaded into a picture box. The borders of the table are slightly slanted. Am using hit testing lines with GDI+ to manipulate the...
72
5152
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
1
1361
by: iLL | last post by:
If I have a collection of polymorphic objects, is there a way to test exactly what data type an element of that collection is? I’ve though of two workarounds: 1. Type casting and testing...
2
1663
by: Luc The Perverse | last post by:
Hello! I am trying to find a way to deal with Windows' case insensitivity without just forcing everything lowercase. While I am open to criticism on my method - my question relates to the...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.