I am trying to send some data to a 'states.csv'. It sends over part of the data, but it also sends over a lot of html! I really don't understand. It's driving me crazy! Thanks. Here's my code.

<?php
if($nowdate!='')
{
header("Content-type: application/data");
header("Content-Disposition: attachment; filename=states.csv");
}
require("$DOCUMENT_ROOT/includes/var.php");
@qdbconn();

if($nowdate != '')
{
$qry = mysql_query("SELECT Name, SUM(Tot) as Tot FROM TestTime WHERE Dat BETWEEN date_format('$nowdate','%Y-%m-%d') AND date_format('$enddate','%Y-%m-%d') GROUP BY Name ORDER BY Name");

	WHILE($row = mysql_fetch_array($qry))
	{
	// Pull out your variables and manipulate them.
	$name = $row['Name'];
	$startTime = $row['Tot'];         //probably put Tot here

	$difference = $startTime;



	$nhours = floor($difference / 3600); // One hour has 3600 seconds
	$nseconds = $difference % 3600;
	$nminutes = round($nseconds / 60); // One minute has 60 seconds, duh!
	$nseconds = $nseconds % 60;

// echo $name;
print("<HTML><CENTER><FONT SIZE=+0><b>$name $name </FONT></b> - <FONT SIZE=+0><i><b>$nhours </FONT></i></b><FONT SIZE=+0> hour(s) </FONT><FONT SIZE=+0><i><b>$nminutes </FONT></i></b><FONT SIZE=+0> minute(s)<FONT></CENTER></HTML>");
}

//exit();

}
@printHead();
print("<H3 CLASS=\"pgtitle\" ALIGN=\"right\">Clock Totals</H3>");
//print("<H3 CLASS=\"pgtitle\" ALIGN=\"right\"><!-- the blue Bar document title --></H3><br><br>");
print("<title>Untitled Document</title>");
print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");

print("<form action=\"try.php\" method=\"post\" name=\"listing\" id=\"listing\">");

print("<table><TR><TD height=\"21\">&nbsp;</TD><TD height=\"21\">Start Date in YYYY-MM-DD:<input type=\"text\" name=\"nowdate\"> </TD></TR></table>");
print("<table><TR><TD height=\"21\">&nbsp;</TD><TD height=\"21\">End Date in YYYY-MM-DD:<input type=\"text\" name=\"enddate\"></TD></TR></table>");
print("<input type=\"Submit\"name=\"Total1\" value=\"Total1\">");
print("</form>");

@printFoot();

?>

    I'm assuming that you want to only send the stuff in your if (!$nowdate != "") sections. If that is the case then the problem is that you have commented out the call to [man]exit/man.

    Also when you are posting code to this forum, it makes it much easier for others to read if you use the [ php ] [ /php ] tags (no spaces inside the brackets - read this for help on bbcode).

    See how pretty it makes your code:

    <?php 
    if($nowdate!='') 
    { 
    header("Content-type: application/data"); 
    header("Content-Disposition: attachment; filename=states.csv"); 
    } 
    require("$DOCUMENT_ROOT/includes/var.php"); 
    @qdbconn(); 
    
    
    if($nowdate != '') 
    { 
    $qry = mysql_query("SELECT Name, SUM(Tot) as Tot FROM TestTime WHERE Dat BETWEEN date_format('$nowdate','%Y-%m-%d') AND date_format('$enddate','%Y-%m-%d') GROUP BY Name ORDER BY Name"); 
    
    WHILE($row = mysql_fetch_array($qry)) 
    { 
    // Pull out your variables and manipulate them. 
    $name = $row['Name']; 
    $startTime = $row['Tot']; //probably put Tot here 
    
    $difference = $startTime; 
    
    
    
    $nhours = floor($difference / 3600); // One hour has 3600 seconds 
    $nseconds = $difference % 3600; 
    $nminutes = round($nseconds / 60); // One minute has 60 seconds, duh! 
    $nseconds = $nseconds % 60; 
    
    
    // echo $name; 
    print("<HTML><CENTER><FONT SIZE=+0><b>$name $name </FONT></b> - <FONT SIZE=+0><i><b>$nhours </FONT></i></b><FONT SIZE=+0> hour(s) </FONT><FONT SIZE=+0><i><b>$nminutes </FONT></i></b><FONT SIZE=+0> minute(s)<FONT></CENTER></HTML>"); 
    } 
    
    //exit(); 
    } 
    @printHead(); 
    print("<H3 CLASS=\"pgtitle\" ALIGN=\"right\">Clock Totals</H3>"); 
    //print("<H3 CLASS=\"pgtitle\" ALIGN=\"right\"><!-- the blue Bar document title --></H3><br><br>"); 
    print("<title>Untitled Document</title>"); 
    print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"); 
    
    print("<form action=\"try.php\" method=\"post\" name=\"listing\" id=\"listing\">"); 
    
    print("<table><TR><TD height=\"21\"> </TD><TD height=\"21\">Start Date in YYYY-MM-DD:<input type=\"text\" name=\"nowdate\"> </TD></TR></table>"); 
    print("<table><TR><TD height=\"21\"> </TD><TD height=\"21\">End Date in YYYY-MM-DD:<input type=\"text\" name=\"enddate\"></TD></TR></table>"); 
    print("<input type=\"Submit\"name=\"Total1\" value=\"Total1\">"); 
    print("</form>"); 
    
    @printFoot(); 
    ?>
    

      haha, thanks for reply. I'm sorry, i forgot to put the code tags. I usally do. And yes, the exit() is what I needed, but my loop still messes up with the exit() in it. It only loops through once, then exits out. I know it's stupid, it's missing something, but i've been working on this all day, and any last minute help is well appreciated.

       <?php	
      if($nowdate!='')
      {
      header("Content-type: application/data");
      header("Content-Disposition: attachment; filename=states.csv");
      }
      require("$DOCUMENT_ROOT/includes/var.php");		
      @qdbconn();
      
      //{	
      		$qry = mysql_query("SELECT Name, SUM(Tot) as Tot FROM TestTime WHERE Dat BETWEEN date_format('$nowdate','%Y-%m-%d') AND date_format('$enddate','%Y-%m-%d') GROUP BY Name ORDER BY Name");
      
      	WHILE($row = mysql_fetch_array($qry))
      	{
      		// Pull out your variables and manipulate them.
      		$name = $row['Name'];
        		$startTime = $row['Tot'];         //probably put Tot here
      
      		$difference = $startTime;
      
      		$nhours = floor($difference / 3600); // One hour has 3600 seconds
      		$nseconds = $difference % 3600;
      		$nminutes = round($nseconds / 60); // One minute has 60 seconds, duh!
      		$nseconds = $nseconds % 60;
      
      		print("Name");
      		print(",");
      		print("Hrs");
      		print(",");
      		print("Mins");
      		print("\n");
      		print($name.",".$nhours.",".$nminutes."\n");
      
      	exit();
      }
      
      @printHead();
      print("<H3 CLASS=\"pgtitle\" ALIGN=\"right\">Clock Totals</H3>");
      print("<title>Untitled Document</title>");
      print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
      
      print("<form action=\"try.php\" method=\"post\" name=\"listing\" id=\"listing\">");
      
      print("<table><TR><TD height=\"21\">&nbsp;</TD><TD height=\"21\">Start Date in YYYY-MM-DD:<input type=\"text\" name=\"nowdate\"> </TD></TR></table>");
      print("<table><TR><TD height=\"21\">&nbsp;</TD><TD height=\"21\">End Date in YYYY-MM-DD:<input type=\"text\" name=\"enddate\"></TD></TR></table>");
      print("<input type=\"Submit\"name=\"Total1\" value=\"Total1\">");
      print("</form>");
      
      @printFoot();
      ?>
      
      

        Hi,

        move the exit() call out of the while loop, e.g.

        ...
        } 
        exit(); 
        
        @printHead(); 
        

        By the way ... you could also remove the stuff that comes after the while loop.

        Thomas

          Your code looks different this time. In this set of code you have removed your second if ($nowdate != "") and have placed your exit() inside your while loop. Ofcourse this is going to exit after looping only once, because the exit is part of the while. You want this structure:

          if ($nowdate != "")
          {
             // do stuff
          
             while ($row = mysql_fetch_array($qry))
             {
                // do stuff
             }
          
             // Now we can exit since we've finished our loop
             exit();
          }
          

            works great, but everytime I refresh the page it the script doesn't function. Any ideas?

              Mu guess would be that your $nowdate variable isn't getting set on a refresh. But other than that I have no idea.

                Yeah, I think your right eoghain. I only put that variable there so I don't access the headers when it's not time too. Any suggestions?

                  Write a Reply...