douwen4401 2016-09-01 21:57
浏览 47
已采纳

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

following my original question, I try to rephrase it to make it more clear:

I have this two tables that are the result of two queries ;

The tables have the first column in common but each row in the first table can have more than one correspondence in the second.

I need to iterate through each row in the first table and create a dynamic html table with all the correspondent row(s) from the second.

This is the code I tried but it only gives as result the first row from the second table:

$stmt2 is for table1 and $stmt is for 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>";
?>

These are the two queries:

1 - $sql that goes to $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 that goes to 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";

I need to to this because I need to send an email to every row in the first table with the information of the second with phpmailer

I hope I've been more precise and clear this time :)

Thank you

  • 写回答

1条回答 默认 最新

  • drby30217 2016-09-02 16:54
    关注

    I will answer my own question because after hours of trying I figured out the logic and what was wrong.

    This is how I was able to connect the two tables, basically I had to insert in the second query a variable that stored the result from the first, like this:

    // 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";
    

    After that I figured how to send an email for every element in table1 with the correspondent result from table2:

    <?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';
                        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行