testing for no data in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JRough

    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($xc lfields, $result){

    $str1 = '';
    for($i=0; $i < sizeof($xclfiel ds); $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;
    }
  • AqD

    #2
    Re: testing for no data in a string

    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($xc lfields, $result){
    >
               $str1 = '';
                    for($i=0; $i < sizeof($xclfiel ds); $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;
    >
    >
    >
    }

    Comment

    • Curtis

      #3
      Re: testing for no data in a string

      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($xc lfields, $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($xclfiel ds); $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

      Comment

      • Curtis

        #4
        Re: testing for no data in a string

        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.=no w().".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($xc lfields, $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($xclfiel ds); $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

        Comment

        • JRough

          #5
          Re: testing for no data in a string

          On Sep 17, 6:39 pm, Curtis <dye...@gmail.c omwrote:
          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($xc lfields, $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($xclfiel ds); $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_row s) is false
          Doing both or only one or the other?

          Comment

          • JRough

            #6
            Re: testing for no data in a string

            On Sep 17, 6:39 pm, Curtis <dye...@gmail.c omwrote:
            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($xc lfields, $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($xclfiel ds); $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_row s) is false?
            Doing both or only one or the other? Or is that mysql_num_rows check
            for if I redid the queries? tia,


            Comment

            • Curtis

              #7
              Re: testing for no data in a string

              JRough wrote:
              On Sep 17, 6:39 pm, Curtis <dye...@gmail.c omwrote:
              >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($xc lfields, $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($xclfiel ds); $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_row s) 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

              Comment

              Working...