Jump to content

Put query on row 2 for Excel while looping


carval23

Recommended Posts

Hey guys. Just need some help on how I can add  a query on row 2 if let's say a field is populated (Ex. spouse != null).

Here is the code but having a hard time trying to put that 2nd row while it's looping. Any help would be appreciated. Thanks!

 

        $data = '';

        while ( $row = mysql_fetch_row($export))

        {

                $line = '';

                foreach ( $row as $value )

                {

                        if ( ( !isset($value) ) OR ( $value == "" ) )

                        {

                                $value = "\t";

                        }

                        else

                        {

                                $value = str_replace('"', '""', $value);

                        $value = '="' . $value . '"' . "\t";

//        ^

//        Added an equal sign

                        }

                        $line .= $value;

                }

                $data .= trim($line)."\n";

        }

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

 

        // return message if query returns no data

        if ( $data == "" )

        {

                $data = "\n(0) Records Found!\n";

        }

 

        // setup headers with no caching

        header("Content-type: application/octet-stream");

        header("Content-Disposition: attachment; filename=data.xls");

        header("Pragma: no-cache");

        header("Expires: 0");

        print "$header\n$data";

} else {

 

Link to comment
Share on other sites

OK, I am assuming that if spouse != null you are wanting to get additional data for the record  (presumably spouse info) to display on a supporting row below the main row for the record.

 

If that is the case, then you don't want to check the spouse record for each record and run an additional query. Instead, you want to modify your original query to get that information if it exists. Then run just that one query and use the PHHP logic to create that additional row if warranted.

Link to comment
Share on other sites

Thanks for the reply. Sorry if the question wasn't clear. Not really good at technical terms especially with programming. Anyways, if let's say on a SQL table, I have name, address, tel no. spouse name, spouse address, spouse tel no. and the Spouse fields are populated, I would like to get the spouse field to move into the 2nd row of the excel sheet when extracted. I actually wrote two queries. Example: $select = sidname AS 'Name'  is for the students name then the other query $select2 = spouse1 AS 'Spouse' is for spouse name. How do I put the $select2 to move into the 2nd row? Hope this is clear. If not, I can attach the script I am working on here so you can have an idea. Thanks again!

Link to comment
Share on other sites

OK, I am assuming that if spouse != null you are wanting to get additional data for the record  (presumably spouse info) to display on a supporting row below the main row for the record.

 

If that is the case, then you don't want to check the spouse record for each record and run an additional query. Instead, you want to modify your original query to get that information if it exists. Then run just that one query and use the PHHP logic to create that additional row if warranted.

Yes, this is exactly what I'm trying to achieve. Unfortunately, I have no idea where to start except the fact that I know that I have to use the if statement and $row2 as to put the data onto the 2nd row.

Link to comment
Share on other sites

Here is the full code for your reference:

 

if($qry == "ok"){
        // get db connection
        include("/*/*/opendb.php");

        $select =       "SELECT 'A' AS 'Transaction Type', student AS 'Student Name', address AS 'Student Address', telephone AS 'Telephone number', spouse AS 'Spouse Name', spouse address AS 'Spouse Address', spouse telephone AS 'Spouse Telephone'
FROM intenrollment
WHERE confirm = 'Y' and amountpaid != '0';

        if ( $smon != "None" and $sday != "None" and $syear != "None" and $emon != "None" and $eday != "None" and $eyear != "None" )
        {
                $startdate = strtotime("$smon $sday, $syear");
                $enddate = strtotime("$emon $eday, $eyear");
                $select .= "AND applydate BETWEEN $startdate AND $enddate ";
        } elseif ( $smon != "None" and $sday != "None" and $syear != "None" ) {
                $startdate = strtotime("$smon $sday, $syear");
                $select .= "AND applydate > $startdate ";
        } elseif ( $emon != "None" and $eday != "None" and $eyear != "None" ) {
                $enddate = strtotime("$emon $eday, $eyear");
                $select .= "AND applydate < $enddate ";
        }

        $select .= "ORDER BY school ASC, lname ASC";

        // run query
        $export = mysql_query($select) or die(mysql_error());
        $fields = mysql_num_fields($export);
        $export2 = mysql_query($select2) or die(mysql_error());

        // get field info for headers
        $header = '';

        for ( $i = 0; $i < $fields; $i++ )
        {
                $header .= mysql_field_name($export, $i) . "\t";
        }

        // extract data and convert into excel readable format
        $data = '';
        while (( $row = mysql_fetch_row($export)))
        {
                $line = '';
                foreach ( $row as $value )
                {
                        if ( ( !isset($value) ) OR ( $value == "" ) )
                        {
                                $value = "\t";
                        }
                        else
                        {
                                $value = str_replace('"', '""', $value);
                        $value = '="' . $value . '"' . "\t";
//        ^
//        Added an equal sign
                        }
                        $line .= $value;
                }
                $data .= trim($line)."\n";
        }
        $data = str_replace("\r", "", $data);

        // return message if query returns no data
        if ( $data == "" )
        {
                $data = "\n(0) Records Found!\n";
        }

        // setup headers with no caching
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=data.xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        print "$header\n$data";
} else {

?>
<style>
body,td,table {font-family:Arial; font-size:10px;}
select {font-family:Arial; font-size:12px;}
</style>
<table align="center">
<tr><td align=center><a href="index.php"><font size=2>Back to Menu</font></a></td></tr>
<tr><td>
<form action="extracttest.php" method="post">
Start:
<select name='smon' size='1'>
<option value="None">Month</option>
<option value='January'>January</option>
<option value='February'>February</option>
<option value='March'>March</option>
<option value='April'>April</option>
<option value='May'>May</option>
<option value='June'>June</option>
<option value='July'>July</option>
<option value='August'>August</option>
<option value='September'>September</option>
<option value='October'>October</option>
<option value='November'>November</option>
<option value='December'>December</option>
</select> 

<select name="sday">
<option value="None">Day</option>
<?
for($ctr=1; $ctr<=31;++$ctr){
  if($row[sday] == $ctr){
    print("<option value='$ctr' selected>$ctr</option>");
  }
  else{
    print("<option value='$ctr'>$ctr</option>");
  }
}
echo "</select>&nbsp";

echo "<select name='syear' size='1'>";
  $today = getdate();
  $yr = $today['year'];
?>

<option value="None">Year</option>";
<option value='2006'>2006</option>";
<option value='2007'>2007</option>";
<option value='2008'>2008</option>";
<option value='2009'>2009</option>";
<option value='2010'>2010</option>";
<option value='2011'>2011</option>";
</select> - 
End:
<select name='emon' size='1'>
<option value="None">Month</option>
<option value='January'>January</option>
<option value='February'>February</option>
<option value='March'>March</option>
<option value='April'>April</option>
<option value='May'>May</option>
<option value='June'>June</option>
<option value='July'>July</option>
<option value='August'>August</option>
<option value='September'>September</option>
<option value='October'>October</option>
<option value='November'>November</option>
<option value='December'>December</option>
</select> 

<select name="eday">
<option value="None">Day</option>
<?
for($ctr=1; $ctr<=31;++$ctr){
  if($row[eday] == $ctr){
    print("<option value='$ctr' selected>$ctr</option>");
  }
  else{
    print("<option value='$ctr'>$ctr</option>");
  }
}
echo "</select>&nbsp";

echo "<select name='eyear' size='1'>";
  $today = getdate();
  $yr = $today['year'];
?>
<option value="None">Year</option>";

<option value='2006'>2006</option>";
<option value='2007'>2007</option>";
<option value='2008'>2008</option>";
<option value='2009'>2009</option>";
<option value='2010'>2010</option>";
<option value='2011'>2011</option>";
</select> 
<input type="submit" value="Select">
<input type="hidden" name="qry" value="ok">
</tr>
<tr><td align=center style="font-famiy:Tahoma; font-size:12px; color:777777;">If you do not enter a date, all records will be displayed.</td></tr>
</form>
</table><p>

<?
}
?>

 

This is considering that let's say spouse != null, I would like it to go to the 2nd row and so on until there are no data to extract.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.