Joel
Joel

Reputation: 77

MySQL; PHP Loop error

So, I am trying to pull a first name, last name and donortype from 2 tables. Basically, a person has a primary "donortype". Meaning if they are two different donortypes then in the Donor table you put the one with the highest rank. In the DonorandType table you put all the remaining. So if they are a board member, alumni and parent you would see board member in the donor table and then alumni and parent in the other table.

I end up getting all the people in the donorandtype table(I only put 4 people in there for testing each with a different number of types) showing up continually. However, if I choose 2011(last years fiscal year) they don't repeat. However, in 2012(this fiscal year) they do. If you would prefer I show all my code and not just the piece about the sql statement and while loop I can. However, I don't think it is an issue with my date constraint.

I also have some date constraints in my sql statement but I don't think that should make a difference. It basically just checks the fiscal year and if they haven't donated that year it doesn't show them. I have tried it with and without that part of the SQL statement and it doesn't seem to make a difference. So, I believe that part can be ignored.

Donor Table:

DonorType Table:

DonorandType Table:

Donation Table:

Ok, so I want to pull their first name & last name from donor table. Then pull all their donortype's from the DonorandType table.

// SQL query to interact with info from our database
$sql = mysql_query("
    SELECT donor.FirstName, donor.LastName, donorandtype.DonorType
    FROM donor, donortype, donorandtype, donations
    WHERE donor.DonorID = donorandtype.DonorID
      and donortype.DonorType = donorandtype.DonorType
      and donations.Date_Received BETWEEN '" . $startdate . "' and '" . $enddate . "'
") or die (mysql_error()); 

// Establish the output variable
echo "<table border=\"1\" cellpadding=\"10\" >
        <tr>
    <th id='name'>Donor Name</th>
    <th id'dontype'>Donor Type</th>
  </tr>";
while($row = mysql_fetch_array($sql)){ 

    $first_name = $row["FirstName"];
    $last_name = $row["LastName"];
    $donor_type = $row["DonorType"];

    echo "
    <tr>
    <td> $first_name $last_name </td>
    <td>$donor_type </td>
    </tr>";


}
echo '</table>';

This part actually displays in a table...does that need to be said? I think it is clear from the code above but whatever.

DONOR'S TYPES
Donor Name  Donor Type
Henry Hunt  Alumni
Henry Hunt  Board member
Paul Gates  Past Parent
Winifred Gardner    Parent
....
</trim>

Upvotes: 0

Views: 179

Answers (1)

Luke Stevenson
Luke Stevenson

Reputation: 10341

<?php
// SQL query to interact with info from our database
$sqlTpl = 'SELECT `d`.`FirstName`, `d`.`LastName`, `t`.`DonorType`
           FROM
             `donorandtype` AS `j`
             LEFT JOIN `donor` AS `d` ON ( `j`.`DonorID` = `d`.`DonorID` )
             LEFT JOIN `donortype` AS `t` ON ( `j`.`DonorType` = `t`.`DonorTypeID` )
             LEFT JOIN `donations ` AS `m` ON ( `d`.`DonorID` = `m`.`DonorID` )
           WHERE
             `m`.`Date_Received` BETWEEN "%s" AND "%s"
           ORDER BY
             `d`.`DonorID` , `t`.`Rank`';
$sqlStr = sprintf( $sqlTpl ,
            date( 'Y-m-d h:i:s' , strtotime( $startdate ) ) ,
            date( 'Y-m-d h:i:s' , strtotime( $enddate ) ) );
$sqlRes = mysql_query( $sqlStr );

if( !$sqlRes ){
  echo 'SQL Error Occurred:';
  echo 'But I am not going to show the error messages publicly.';
}else{
?>
<table border="1" cellpadding="0">
  <thead>
    <tr>
      <th id="name">Donor Name</th>
      <th id="type">Donor Type</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th colspan="2"><?php echo mysql_num_rows( $sqlRes ); ?> Donors Found</th>
    </tr>
  </tfoot>
  <tbody>
<?php
  if( mysql_num_rows( $sqlRes ) ){
    while( $r = mysql_fetch_array( $sqlRes ) ){
?>
    <tr>
      <td><?php echo $r['FirstName'].' '.$r['LastName']; ?></td>
      <td><?php echo $r['DonorType']; ?></td>
    </tr>
<?php
    }
  }else{
?>
    <tr>
      <td colspan="2">No Records Returned</td>
    </tr>
<?php
  }
?>
  </tbody>
</table>
<?php
}
?>

Upvotes: 1

Related Questions