<? echo "<table border=1>\n";
echo " <tr><td width ='20'>№</td><td width='80'>Дата</td><td width='100'>Оборудование</td>
<td width='200'>Проделанная работа</td> <td width='80'>Сумма</td></tr>\n";
$i=0;$totalPrice = 0; $tehas= mysql_query("SELECT id,model FROM tech WHERE user='$myrow[id]'");// вся техника пользователя
if($techus=mysql_fetch_array($tehas)){
do{ $tech_id=$techus['id'];
$tech_model=$techus['model'];
$prodelprint = mysql_query("SELECT *, DATE_FORMAT(data,'%d.%m.%Y') As data FROM remont_tech WHERE tech_id='$tech_id ' and status != 'delete' and TO_DAYS(NOW()) - TO_DAYS(data) <= 30 ORDER BY data" );
if($prints=mysql_fetch_array($prodelprint)){
$techs_id=$prints['tech_id'];
$techs= mysql_query("SELECT * FROM tech WHERE id='$techs_id'");
$tech=mysql_fetch_array($techs);
$id_marka=$tech['marka'];
$marks=mysql_query("SELECT name FROM marka WHERE id='$id_marka' ");
$marka = mysql_fetch_array($marks);
$id_model=$tech['model'];
$models=mysql_query("SELECT name FROM model WHERE id='$id_model' ");
$model = mysql_fetch_array($models);
do{$i++;$totalPrice+=$prints['sum'];
echo "<tr><td>$i</td><td>$prints[data]</td><td>$marka[name] $model[name]($tech[serial])</td><td>$prints[work_id]</td><td>$prints[sum] Руб.</td><tr>";
} while($prints=mysql_fetch_array($prodelprint));
} } while($techus=mysql_fetch_array($tehas));
}
echo"<tr><td COLSPAN=1><b>ИТОГО</b></td><td></td><td></td><td></td><td><b>$totalPrice Руб.</b></td></tr>";
echo "</table>\n"; ?>
<?php
// Эта строчка — где-то в другом файле — где в данный момент инициализируется соединение с БД
$db = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'username', 'password');
$sth = $db->prepare("
SELECT
t.`id`,
DATE_FORMAT(rt.`data`, '%d.%m.%Y') remont_date,
ma.`name` marka,
mo.`name` model,
t.`serial`,
rt.`work_id`,
rt.`sum`
FROM `tech` t
LEFT JOIN `remont_tech` rt ON rt.`tech_id` = t.`id` AND rt.`status` <> 'delete'
LEFT JOIN `marka` ma ON ma.`marka` = t.`marka`
LEFT JOIN `model` mo ON mo.`id` = t.`model`
WHERE
t.`user` = ? AND
TIMESTAMPDIFF(DAY, rt.`data`, NOW()) BETWEEN 0 AND 30
ORDER BY rt.`data` DESC
");
$sth->execute(array($myrow['id']));
$result = $sth->fetchAll();
?>
<table>
<tr>
<th>№</th>
<th>Дата</th>
<th>Оборудование</th>
<th>Проделанная работа</th>
<th>Сумма</th>
</tr>
<?php if (!count($result)) : ?>
<tr>
<td>0</td>
<td colspan="4">Ничего не найдено.</td>
</tr>
<?php else : ?>
<?php $summ = 0; ?>
<?php foreach ($result as $key => $row) : ?>
<tr>
<td><?= $key+1 ?></td>
<td><?= $row['remont_date'] ?></td>
<td><?= sprintf('%s %s(%s)', $row['marka'], $row['model'], $row['serial']) ?></td>
<td><?= $row['work_id'] ?></td>
<td><?= number_format($row['sum'], 0, ',', ' ') ?> руб.</td>
</tr>
<?php $summ += $row['sum']; ?>
<?php endforeach; ?>
<tr>
<td colspan="3"></td>
<td><b>ИТОГО:</b></td>
<td><b><?= number_format($summ, 0, ',', ' ') ?> руб.</b></td>
</tr>
<?php endif; ?>
</table>
SELECT `rt`.`sum` AS `sum`, `rt`.`work_id` AS `work_id`, `t`.`serial` AS serial, `mk`.`name` AS `marka`,
`md`.`name` AS `model`, DATE_FORMAT(`rt`.`data`, '%d.%m.%Y') AS `data`
FROM `remont_tech` AS `rt`
LEFT JOIN `tech` AS `t` ON `rt`.`tech_id` = `t`.`id`
LEFT JOIN `marka` AS `mk` ON `t`.`marka` = `mk`.`id`
LEFT JOIN `model` AS `md` ON `t`.`model` = `md`.`id`
WHERE `rt`.`tech_id` = '$techs_id'
AND `rt`.`status` != 'delete'
AND TO_DAYS(NOW()) - TO_DAYS(`rt`.`data`) <= 30
ORDER BY `rt`.`data`