从sql查询结果创建动态html表,用于phpmailer的邮件列表

时间:2016-09-01 21:57:11

标签: php database loops logic phpmailer

跟随我的original question,我试着改写它以使其更清晰:

我有two tables这是两个查询的结果;

这些表有第一列共同点,但第一列中的每一行在第二列中可以有多个对应关系。

我需要遍历第一个表中的每一行,并创建一个动态html表,其中包含来自第二个表的所有相应行。

这是我尝试的代码,但它只给出了第二个表中第一行的结果:

$stmt2用于table1,$stmt用于table2     

$row2 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC);
foreach ($row2 as $fornitori) {
    $fornitore = $row2['FOURNI'];

    //Intestazione della tabella uguale per tutti

    echo "<table><tr><th>ODL</th><th>INV</th><th>APP.</th><th>SERIALE</th><th>MODELLO</th><th>MARCA</th></tr>";
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        if ($fornitore == $row['FOURNI']) {
                echo "<tr><td>".$row['NU_INT']."</td><td>".$row['NU_IMM']."</td><td>".$row['NOM_EQP']."</td><td>".$row['N_SERI']."</td><td>".$row['TYP_MOD']."</td><td>".$row['MARQUE']."</td></tr>";
            }
        }
    }
    echo "</table>";
?>

这是两个问题:

1 - $sql转到$stmt

$sql = "SELECT [NU_INT],[NU_IMM],[N_SERI],[NOM_EQP],[TYP_MOD],[MARQUE],NOM_UF],[NOM_ETAB],[DA_AP],[OBSERV],[OBSERV2],[LIB_STATUT], A.FOURNI, AD_EMAIL    
FROM [INPROGRESS_WO_VIEW] A    
LEFT JOIN    
FOURNIS2 F    
ON A.FOURNI = F.FOURNI    
WHERE A.FOURNI <> 'NULL'    
ORDER BY A.FOURNI ASC";

2 - $sql2转到stmt2

$sql2 = "SELECT DISTINCT A.FOURNI
FROM FOURNIS2 A
LEFT JOIN 
[INPROGRESS_WO_VIEW] B
ON A.FOURNI = B.FOURNI
WHERE [NU_INT] <> 'NULL'
ORDER BY A.FOURNI";

我需要这个,因为我需要向第一个表中的每一行发送一封电子邮件,其中包含第二个用phpmailer的信息

我希望这次我更加精确和清晰:)

谢谢

1 个答案:

答案 0 :(得分:0)

我会回答我自己的问题,因为经过几个小时的尝试,我找出了逻辑和错误。

这就是我能够连接这两个表的方法,基本上我必须在第二个查询中插入一个存储第一个结果的变量,如下所示:

// TABLE 1 QUERY
$sql2 = "SELECT DISTINCT A.FOURNI
FROM FOURNIS2 A
LEFT JOIN 
[INPROGRESS_WO_VIEW] B
ON A.FOURNI = B.FOURNI
WHERE [NU_INT] <> 'NULL'
ORDER BY A.FOURNI";
// WE MAKE THE RESOURCE FOR TABLE2
$stmt2 = sqlsrv_query($conn, $sql2);
if( $stmt2 === false ) {
     die( print_r( sqlsrv_errors(), true));
}
// INIZIAMO IL WHILE A PARTIRE DALLO $STMT2
while ($row2 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {            
            $sql = "SELECT [NU_INT],[NU_IMM],[N_SERI],[NOM_EQP],[TYP_MOD],[MARQUE],[NOM_UF],
            [NOM_ETAB],[DA_AP],[OBSERV],[OBSERV2],[LIB_STATUT], A.FOURNI, AD_EMAIL
            FROM [INPROGRESS_WO_VIEW] A
            LEFT JOIN
            FOURNIS2 F
            ON A.FOURNI = F.FOURNI
            WHERE A.FOURNI = " . "'" . $row2["FOURNI"] . "'" . // HERE WE ASSIGN THE VARIABLE FROM TABLE1, SO THE TWO HAVE A CORRESPONDENCE
            " ORDER BY A.FOURNI ASC";

之后我想出了如何使用table2中的对应结果发送table1中每个元素的电子邮件:

<?php

error_reporting(E_STRICT | E_ALL);

date_default_timezone_set('Etc/UTC');

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;



$mail->isSMTP();
$mail->Host = '*********';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->SMTPSecure = "ssl"; //This is important, I forgot this parameter the first time and it didn't send any email, just stuck in a loop
$mail->Port = 465;
$mail->Username = '*******';
$mail->Password = '*******';
$mail->setFrom('******');
$mail->addReplyTo('******');




// DATI CONNESSIONE DATABASE
$serverName = "******"; //serverName\instanceName
$connectionInfo = array( "Database"=>"******", "UID"=>"******", "PWD"=>"******");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
// CONTROLLA SE LA CONNESSIONE AVVIENE CON SUCCESSO
if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

// QUERY CON ELENCO FORNITORI CHE HANNO ATTIVO UNO O PIU' INTERVENTI IN CORSO
$sql2 = "SELECT DISTINCT A.FOURNI
FROM FOURNIS2 A
LEFT JOIN 
[INPROGRESS_WO_VIEW] B
ON A.FOURNI = B.FOURNI
WHERE [NU_INT] <> 'NULL'
ORDER BY A.FOURNI";
// CREIAMO LA RISORSA A CUI POI ATTINGERA' IL PRIMO WHILE
$stmt2 = sqlsrv_query($conn, $sql2);
if( $stmt2 === false ) {
     die( print_r( sqlsrv_errors(), true));
}
// INIZIAMO IL WHILE A PARTIRE DALLO $STMT2
while ($row2 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {
            // IMPOSTIAMO LA SECONDA QUERY $SQL ED ASSEGNIAMO IL FORNITORE A QUELLO CORRISPONDENTE NEL PRIMO WHILE
            $sql = "SELECT [NU_INT],[NU_IMM],[N_SERI],[NOM_EQP],[TYP_MOD],[MARQUE],[NOM_UF],
            [NOM_ETAB],[DA_AP],[OBSERV],[OBSERV2],[LIB_STATUT], A.FOURNI, AD_EMAIL
            FROM [INPROGRESS_WO_VIEW] A
            LEFT JOIN
            FOURNIS2 F
            ON A.FOURNI = F.FOURNI
            WHERE A.FOURNI = " . "'" . $row2["FOURNI"] . "'" . //ASSEGNIAMO LA VARIBILE FORNITORE
            " ORDER BY A.FOURNI ASC";

            $stmt = sqlsrv_query($conn, $sql);
            $mail->Subject = "URGENTISSIMO: SOLLECITO INVIO RT CHIUSURA INTERVENTI APERTI - " . $row2["FOURNI"];

            // HEADER TABELLA UGUALE PER TUTTI
            $body = "<html><head><style>
                     table, tr, td, th {
                        border: solid 1px;
                     }
                     th {
                        background-color: yellow;
                     }
                     </style>
                     </head>
                     <body>                      
                     <table><tr><th>ODL</th><th>INV</th><th>APP.</th><th>SERIALE</th><th>MODELLO</th><th>MARCA</th><th>PROBL.</th><th>JOB</th><th>DATA RICH.</th><th>REPARTO</th><th>PRESIDIO</th></tr>";

            while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {

                $body .= "<tr><td>".$row["NU_INT"]."</td><td>".$row["NU_IMM"]."</td><td>".$row["NOM_EQP"]."</td><td>".$row["N_SERI"]."</td><td>".$row["TYP_MOD"]."</td><td>".$row["MARQUE"]."</td><td>".$row["OBSERV"]."</td><td>".$row["OBSERV2"] . "</td><td>".$row["DA_AP"]. "</td><td>".$row["NOM_UF"]. "</td><td>".$row["NOM_ETAB"]."</td></tr>";
                $mail->addAddress($row["AD_EMAIL"]);
            }

            $body .= "</table>                        
                      </body>";

            $mail->msgHTML($body);

            if(!$mail->send()) {
                echo 'Message could not be sent. <br />';
                echo 'Mailer Error: ' . $mail->ErrorInfo . "<br />";
                } else {
                    echo 'Message has been sent';
                    }
}

?>