PHP作为表中的一个整行

时间:2016-09-27 13:56:57

标签: php html

我花了太多时间在我确定的简单修复上。我希望通过从行的每个单元格中的数据库中的prod列中提取特定的分类,将以下代码打印为表格中的整行。我已经尝试过它作为一个while循环和每个。任何帮助将不胜感激,谢谢。

$sql = 'SELECT * FROM drawings2';
$stmt = $dbCon->prepare($sql);
$stmt->execute();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
echo "<tr>";

if ($row[prod] == '') {echo "<td></td><td width='10'></td>";}   
elseif ($row[prod] == 'amphe-309' && $row[des] == 'IP44') {echo "<td align='left' valign='top'><div align='left'><font color='#0083ca'><strong>IP44</strong></font></div></td><td width='10'></td>";}
elseif ($row[prod] == 'amphe-309' && $row[des] == 'IP67') {echo "<td align='left' valign='top'><div align='left'><font color='#0083ca'><strong>IP67</strong></font></div></td><td width='10'></td>";}
elseif ($row[prod] == 'amphe-309' && $row[des] == 'Accessories') {echo "<td align='left' valign='top'><div align='left'><font color='#0083ca'><strong>Accessories</strong></font></div></td><td width='10'></td>";}
    elseif ($row[prod] == 'amphe-309') 
        {echo "<td align='left' valign='top'><div align='left'><a href='/$row[file]' target='_blank'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'AOGT_3D_Models_Amphe-309', '<?php echo "$row[des]"?>']);"><? echo "$row[des]</a></div></td>
    <td width='10'></td>";} 

if ($row[prod] == '') {echo "<td></td><td width='10'></td>";}           
elseif ($row[prod] == 'amphe-ex') 
        {echo "<td align='left' valign='top'><div align='left'><a href='/$row[file]' target='_blank'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'AOGT_3D_Models_Amphe-EX', '<?php echo "$row[des]"?>']);"><? echo "$row[des]</a></div></td>
    <td width='10'></td>";}     

if ($row[prod] == '') {echo "<td></td><td width='10'></td>";}           
elseif ($row[prod] == 'ex309') 
        {echo "<td align='left' valign='top'><div align='left'><a href='/$row[file]' target='_blank'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'AOGT_3D_Models_Amphe-EX', '<?php echo "$row[des]"?>']);"><? echo "$row[des]</a></div></td>
    <td width='10'></td>";} 

if ($row[prod] == '') {echo "<td></td><td width='10'></td>";}           
elseif ($row[prod] == 'ktk') 
        {echo "<td align='left' valign='top'><div align='left'><a href='/$row[file]' target='_blank'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'AOGT_3D_Models_Amphe-EX', '<?php echo "$row[des]"?>']);"><? echo "$row[des]</a></div></td>
    <td width='10'></td>";} 

if ($row[prod] == '') {echo "<td></td><td width='10'></td>";}           
elseif ($row[prod] == 'star-line') 
        {echo "<td align='left' valign='top'><div align='left'><a href='/$row[file]' target='_blank'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'AOGT_3D_Models_Amphe-EX', '<?php echo "$row[des]"?>']);"><? echo "$row[des]</a></div></td>
    <td width='10'></td>";} 

if ($row[prod] == '') {echo "<td></td><td width='10'></td>";}           
elseif ($row[prod] == 'star-line-ex') 
        {echo "<td align='left' valign='top'><div align='left'><a href='/$row[file]' target='_blank'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'AOGT_3D_Models_Amphe-EX', '<?php echo "$row[des]"?>']);"><? echo "$row[des]</a></div></td>
    <td width='10'></td>";}

echo "</tr>";

}

echo "</table>"; 
$dbCon = null;
?>

1 个答案:

答案 0 :(得分:0)

所以我有点解决了我的问题,但它仍然不是最好的方法。我使用了<p></p>标记来正确显示它,但是当我尝试格式化时会导致问题,因为所有内容都应该在<tr></tr>的行中。

<?
$head = array(array('amphe-309','Amphe-309','/images/datasheets/IDS-33 Amphe-309.pdf','Amphe-309 Data Sheet'), array('amphe-ex','Amphe-EX','/images/catalogs/Amphe-EX.pdf'
,'Amphe-EX Catalog'), array('ex309','EX309','/images/datasheets/IDS-92 EX309.pdf','EX309 Data Sheet'), array('ktk','KTK','/images/datasheets/IDS-35 KTK High Pressure Downhole Connector.pdf','KTK Data Sheet'), array('star-line','Star-Line','/images/catalogs/Star-Line.pdf','Star-Line Catalog'),array('star-ex','Star-Line EX','/images/catalogs/Star-Line.pdf','Star-Line EX Catalog'));

echo "<table width='100%' border='0' cellpadding='10' cellspacing='0'>".PHP_EOL;
echo "<tr>".PHP_EOL;
foreach ($head as $value) {
    $sql = "SELECT * FROM drawings WHERE prod = '$value[0]'";
    $stmt = $dbCon->prepare($sql);
    $stmt->execute();

echo "<td valign='top'><div align='left'><p><a href='$value[2]' target='_blank'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'AOGT_3D_Models', '<?php echo "$value[3]"?>']);"><? echo "<strong>$value[1]</strong></a></p></div>".PHP_EOL; //Title


    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 

        if ($row[des] == 'IP44') {
            echo "<div align='left'><p><font color='#0083ca'><strong>$row[des]</strong></font></p></div>".PHP_EOL;
            }
        elseif ($row[des] == 'IP67') {
            echo "<div align='left'><p><font color='#0083ca'><strong>$row[des]</strong></font></p></div>".PHP_EOL;
            }
        elseif ($row[des] == 'Accessories') {
            echo "<div align='left'><p><font color='#0083ca'><strong>$row[des]</strong></font></p></div>".PHP_EOL;
            }

        else {
        echo "<p><a href='/$row[file]' target='_blank'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', '<?php echo "$row[eventlabel]"?>', '<?php                                     
        echo "$row[des]"?>']);"><? echo "$row[des]</a></p>".PHP_EOL;
        }

    }
    echo "</td>";
}
echo "</tr></table>".PHP_EOL; 

$dbCon = null;
?>