我有下一个代码:
<?php
session_start();
include("config.php");
global $kon;
ob_start();
//Uzimamo trenutni datum i vrijeme da mozemo spremiti u orders tabelu
$dt = new DateTime();
$trenutniDatum = $dt->format('Y-m-d');
$trenutniDatumiVrijeme = $dt->format('Y-m-d H:m:s');
$leeggoed = " ";
$rezWeek = mysqli_query($kon, "SELECT * FROM weekelijks");
$brRez = mysqli_num_rows($rezWeek);
$korpa = "<table width=\"80%\">
<tr align=\"center\">
<th bgcolor=\"#E5E5E5\" style=\"padding:3px;\">Aantal</th>
<th bgcolor=\"#E5E5E5\" style=\"padding:3px;\">Product</th>
</tr>";
if($brRez > 0){
while($redWeek = mysqli_fetch_assoc($rezWeek)){
$user_id = $redWeek["user_id"];
//Uzimamo podatke za korisnika
$rezKor = mysqli_query($kon, "SELECT * FROM korisnici WHERE id = ". $user_id ." LIMIT 1");
$redKor = mysqli_fetch_assoc($rezKor);
$korEmail = $redKor["email"];
$naam = $redKor["ime"] . $redKor["prezime"];
//Sad moeamo uzeti cutoff id
$rezCutoff = mysqli_query($kon, "SELECT cutoffs.id as cutoff_id, cutoffs.datetime as cutoffTime, regios.id as regioID, regios.naam as regioNaam, regio_deelgemeente_tt.id, deelgemeentes.regio_postcode
FROM cutoffs
INNER JOIN regios ON regios.id = cutoffs.regio_id
INNER JOIN regio_deelgemeente_tt ON regios.id = regio_deelgemeente_tt.regio_id
INNER JOIN deelgemeentes ON regio_deelgemeente_tt.deelgemeente_id = deelgemeentes.id
WHERE deelgemeentes.regio_postcode = ". $redKor["postcode"] ." GROUP BY cutoffs.regio_id");
$redCutoff = mysqli_fetch_assoc($rezCutoff);
//Otpakujemo datum da imamo lijep prikaz
$datum_query = mysqli_query($kon, "SELECT EXTRACT(DAY FROM datetime) AS dan, EXTRACT(MONTH FROM datetime) AS mjesec,
EXTRACT(YEAR FROM datetime) AS godina, EXTRACT(HOUR FROM datetime) AS sat, EXTRACT(MINUTE FROM datetime) AS minute, EXTRACT(SECOND FROM datetime) AS sekunde FROM cutoffs WHERE id=". $redCutoff["cutoff_id"] ." ");
$datum = mysqli_fetch_assoc($datum_query);
$dateCutOff = $datum["godina"] . "-" . $datum["mjesec"] . "-" . $datum["dan"];
$trenutniDatum = strtotime($trenutniDatum);
$cutOffDatum = strtotime($dateCutOff);
//Uzimamo produkt po product_shop_id
$rezProdShop = mysqli_query($kon, "SELECT producten.*, producten.naam as prodNaam, shops.*, shops.id as shopId, shops.naam as shopNaam, product_shop_tt.*
FROM product_shop_tt INNER JOIN producten ON producten.id = product_shop_tt.product_id
INNER JOIN shops ON shops.id = product_shop_tt.shop_id
WHERE product_shop_tt.id = ". $redWeek["product_shop_tt_id"] ."");
$redProdShop = mysqli_fetch_assoc($rezProdShop);
$korpa .= "<tr align=\"center\">
<td style=\"padding:3px;\">". $redWeek["aantal"] ."</td>
<td style=\"padding:3px;\">". $redProdShop["prodNaam"] ."</td>
</tr>";
if($trenutniDatum == $cutOffDatum){
$onsdMail = "info@boodschappen.be";
$from = "Uwboodschappen - aankoop reminder";
$to = $korEmail;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $from . "\r\n" .
'Reply-To: ' . $onsdMail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = "Aankoop reminder";
$message = "Dag ". $naam .", <br /><br />";
$message .= "". $korpa ."";
$message .= "Met vriendelijke groeten, <br /><br />";
$message .= "<a href=\"http://www.uwboodschappen.be\">Uw boodschappen</a><br />";
$message .= "info@uwboodschappen.be";
}
}
echo "</table>";
}else{
exit();
}
ob_flush();
?>
我有一个数据库表 weekelijks ( id,user_id,product_shop_tt_id,aantal )。 我想制作一个我将制作cron的脚本。我想我知道该怎么做。 我的表weekelijks是:
我希望我的代码使用该表中的所有内容并将其发送给用户。 现在代码读取所有数据,所有数据都将发送给所有用户。我想要的是读取数据,并通过user_id将数据发送给与这些产品相关的用户。 现在我得到
有什么想法吗?
提前致谢。
答案 0 :(得分:1)
粗暴地敲了敲,没有经过测试,但希望能给你这个想法。
以下代码循环显示主表中的记录。对于每一个,它将细节传递给对象。该对象负责在用户id更改时执行输出,并在调用析构函数时执行输出。
我已将主表的sql与其合并以获取电子邮件详细信息。我滥用GROUP BY来获取电子邮件的单个记录,但这可以(也可能应该)清理。
<?php
session_start();
include("config.php");
global $kon;
ob_start();
//Uzimamo trenutni datum i vrijeme da mozemo spremiti u orders tabelu
$dt = new DateTime();
$trenutniDatum = strtotime($dt->format('Y-m-d'));
$trenutniDatumiVrijeme = $dt->format('Y-m-d H:m:s');
$leeggoed = " ";
$rezWeek = mysqli_query($kon, "SELECT a.id,
a.user_id,
a.product_shop_tt_id,
a.aantal,
b.email,
b.ime,
b.prezime,
b.postcode
FROM weekelijks a
INNER JOIN korisnici b
ON a.user_id = b.id
GROUP BY a.id,
a.user_id,
a.product_shop_tt_id
a.aantal
ORDER BY a.user_id");
$brRez = mysqli_num_rows($rezWeek);
if($brRez > 0)
{
$output_details = new output_details($trenutniDatum);
while($redWeek = mysqli_fetch_assoc($rezWeek))
{
//Uzimamo podatke za korisnika
$korEmail = $brRez["email"];
$naam = $brRez["ime"] . $brRez["prezime"];
//Sad moeamo uzeti cutoff id
$rezCutoff = mysqli_query($kon, "SELECT cutoffs.id as cutoff_id, cutoffs.datetime as cutoffTime, regios.id as regioID, regios.naam as regioNaam, regio_deelgemeente_tt.id, deelgemeentes.regio_postcode
FROM cutoffs
INNER JOIN regios ON regios.id = cutoffs.regio_id
INNER JOIN regio_deelgemeente_tt ON regios.id = regio_deelgemeente_tt.regio_id
INNER JOIN deelgemeentes ON regio_deelgemeente_tt.deelgemeente_id = deelgemeentes.id
WHERE deelgemeentes.regio_postcode = ". $brRez["postcode"] ." GROUP BY cutoffs.regio_id");
$redCutoff = mysqli_fetch_assoc($rezCutoff);
//Otpakujemo datum da imamo lijep prikaz
$datum_query = mysqli_query($kon, "SELECT EXTRACT(DAY FROM datetime) AS dan, EXTRACT(MONTH FROM datetime) AS mjesec,
EXTRACT(YEAR FROM datetime) AS godina, EXTRACT(HOUR FROM datetime) AS sat, EXTRACT(MINUTE FROM datetime) AS minute, EXTRACT(SECOND FROM datetime) AS sekunde FROM cutoffs WHERE id=". $redCutoff["cutoff_id"] ." ");
$datum = mysqli_fetch_assoc($datum_query);
$dateCutOff = $datum["godina"] . "-" . $datum["mjesec"] . "-" . $datum["dan"];
$cutOffDatum = strtotime($dateCutOff);
//Uzimamo produkt po product_shop_id
$rezProdShop = mysqli_query($kon, "SELECT producten.*, producten.naam as prodNaam, shops.*, shops.id as shopId, shops.naam as shopNaam, product_shop_tt.*
FROM product_shop_tt INNER JOIN producten ON producten.id = product_shop_tt.product_id
INNER JOIN shops ON shops.id = product_shop_tt.shop_id
WHERE product_shop_tt.id = ". $redWeek["product_shop_tt_id"] ."");
$redProdShop = mysqli_fetch_assoc($rezProdShop);
$korpa = "<tr align=\"center\">
<td style=\"padding:3px;\">". $redWeek["aantal"] ."</td>
<td style=\"padding:3px;\">". $redProdShop["prodNaam"] ."</td>
</tr>";
$output_details->add_output($redWeek, $korpa, $cutOffDatum);
}
unset ($output_details);
}
else
{
exit();
}
ob_flush();
class output_details
{
private $current_user = 0;
private $naam = '';
private $korpa = '';
private $trenutniDatum = '';
function __construct($trenutniDatum)
{
$this->trenutniDatum = $trenutniDatum;
}
function __destruct()
{
$this->do_output();
}
function add_output($redWeek, $korpa, $cutOffDatum)
{
if ($redWeek['user_id'] != $this->current_user)
{
$this->do_output();
$this->current_user = $redWeek['user_id'];
$this->korpa = "<table width=\"80%\">
<tr align=\"center\">
<th bgcolor=\"#E5E5E5\" style=\"padding:3px;\">Aantal</th>
<th bgcolor=\"#E5E5E5\" style=\"padding:3px;\">Product</th>
</tr>";
}
if(this->trenutniDatum == $cutOffDatum)
{
$this->korpa .= $korpa;
}
}
function do_output()
{
if ($this->current_user != 0)
{
$onsdMail = "info@boodschappen.be";
$from = "Uwboodschappen - aankoop reminder";
$to = $korEmail;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $from . "\r\n" .
'Reply-To: ' . $onsdMail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = "Aankoop reminder";
$message = "Dag ". $this->naam .", <br /><br />";
$message .= "". $this->korpa ."</table>";
$message .= "Met vriendelijke groeten, <br /><br />";
$message .= "<a href=\"http://www.uwboodschappen.be\">Uw boodschappen</a><br />";
$message .= "info@uwboodschappen.be";
}
}
}
?>
答案 1 :(得分:0)
<?php
session_start();
include("config.php");
global $kon;
ob_start();
//Uzimamo trenutni datum i vrijeme da mozemo spremiti u orders tabelu
$dt = new DateTime();
$trenutniDatum = $dt->format('Y-m-d');
$trenutniDatumiVrijeme = $dt->format('Y-m-d H:m:s');
$leeggoed = " ";
$rezWeek = mysqli_query($kon, "SELECT * FROM weekelijks GROUP BY user_id");
$brRez = mysqli_num_rows($rezWeek);
if($brRez > 0){
while($redWeek = mysqli_fetch_assoc($rezWeek)){
$user_id = $redWeek["user_id"];
//Uzimamo podatke za korisnika
$rezKor = mysqli_query($kon, "SELECT * FROM korisnici WHERE id = ". $user_id ." LIMIT 1");
$redKor = mysqli_fetch_assoc($rezKor);
$korEmail = $redKor["email"];
$naam = $redKor["ime"] . " " . $redKor["prezime"];
$message = "Dag ". $naam .", <br /><br />";
$message .= "Ovdje ide poruka <br /><br />";
$message .= "<table style=\"max-width:100%;\">
<tr align=\"center\">
<th bgcolor=\"#E5E5E5\" style=\"padding:3px;\">Aantal</th>
<th bgcolor=\"#E5E5E5\" style=\"padding:3px;\">Product</th>
<th bgcolor=\"#E5E5E5\" style=\"padding:3px;\">Winkel</th>
</tr>";
/****************Listanje produkata za ovog usera**********************/
$rezTabela = mysqli_query($kon, "SELECT * FROM weekelijks WHERE user_id = ". $redWeek["user_id"] ."");
$brRezKor = mysqli_num_rows($rezTabela);
//Ovdje listamo proizvode za ovog klanta
while($redTabela = mysqli_fetch_assoc($rezTabela)){
//Uzimamo produkt po product_shop_id
$rezProdShop = mysqli_query($kon, "SELECT producten.*, producten.naam as prodNaam, shops.*, shops.id as shopId, shops.naam as shopNaam, product_shop_tt.*
FROM product_shop_tt INNER JOIN producten ON producten.id = product_shop_tt.product_id
INNER JOIN shops ON shops.id = product_shop_tt.shop_id
WHERE product_shop_tt.id = ". $redTabela["product_shop_tt_id"] ."");
$redProdShop = mysqli_fetch_assoc($rezProdShop);
$message .= "<tr align=\"center\">
<td style=\"padding:3px;\">". $redTabela["aantal"] ."</td>
<td style=\"padding:3px;\">". $redProdShop["prodNaam"] ."</td>
<td style=\"padding:3px;\">
<a href=\"http://www.uwboodschappen.be/winkel-details.php?id=". $redProdShop["shopId"] ."\" title=\"Bezoek deze winkel\">
<img src=\"upload/shops-logos/". $redProdShop["shopId"] .".jpg\" style=\"max-height:50px;\" alt=\"". $redProdShop["shopNaam"] ."\" title=\"". $redProdShop["shopNaam"] ."\">
</a>
</td>
</tr><br />";
}
/*****************************Kraj listanja produkata**************************************/
$message .= "</table><br /><br />";
$message .= "Met vriendelijke groeten,<br /><br />";
$message .= "<a href=\"http://www.uwboodschappen.be\">Uwboodschappen.be</a><br />";
$message .= "info@uwboodschappen.be";
echo $message . "<br /><br /><br />";
}
}else{
exit();
}
ob_flush();
?>