使用foreach()和strpos()在php中创建html表

时间:2014-10-30 03:07:05

标签: javascript php

正在构建一个表,其中添加了每个产品/品牌/条件/数量并发送到我的电子邮件。但是,如果将其中一个留空,则会破坏表格格式。我尝试了if / else语句,isset /!空,以及我能想到的任何其他内容。如果留空,我想添加空白列/行。

  • 由于我在每个添加的项目中都使用了计数器(product_1,product_2)并且没有限制,因此我使用strpos 查找并“快速”添加到表中。

- 我无法附上照片,这是我的第一篇文章。 -

以下是我现在拥有的内容,并且在没有任何字段留空时效果很好:

$content .= "<table rules='all' style='border-color: #666; width: 800px; border: 1px solid black; border-collapse: collapse; margin: 0 auto;'>\r\n";    

foreach($_POST as $key => $value) {

    $partqty = strpos($key, 'Part_Quantity');
    $partcond = strpos($key, 'Part_Condition');
    $partbrand = strpos($key, 'Part_Brand');
    $partmodel = strpos($key, 'Part_Model');
        if ($partqty === 0) {
        $content .= "<tr>\r\n";         
        $content .= "   <td style='width:15%;'><input value='" . $value . "' style='border: none; width:100%;'></td>\r\n";
        }           
        if ($partcond === 0) {      
        $content .= "   <td style='width:65%;'><input value='" . $value . "' style='border: none; width:100%;'></td>\r\n";
        }   
        if ($partbrand === 0) {
        $content .= "   <td style='width:10%;'><input value='" . $value . "' style='border: none; width:100%;'></td>\r\n";
        }
        if ($partmodel === 0) {
        $content .= "   <td style='width:10%;'><input value='" . $value . "' style='border: none; width:100%;'></td>\r\n";
        $content .= "</tr>\r\n";            
    }
}

$content .= "</table>\r\n"; 

这是我想做但不起作用的想法。

foreach($_POST as $key => $value) {

    $partqty = strpos($key, 'Part_Quantity');
    $partcond = strpos($key, 'Part_Condition');
    $partbrand = strpos($key, 'Part_Brand');
    $partmodel = strpos($key, 'Part_Model');
        if ($partqty === 0) {
        $content .= "<tr>\r\n";         
        $content .= "   <td style='width:15%;'><input value='" . $value . "' style='border: none; width:100%;'></td>\r\n";
        }
        if ($partqty === false) {
        $content .= "<tr>\r\n";         
        $content .= "   <td style='width:15%;'><input value='empty' style='border: none; width:100%;'></td>\r\n";
        }       
        if ($partcond === 0) {      
        $content .= "   <td style='width:65%;'><input value='" . $value . "' style='border: none; width:100%;'></td>\r\n";
        }
        if ($partcond === false) {      
        $content .= "   <td style='width:65%;'><input value='empty' style='border: none; width:100%;'></td>\r\n";
        }
        if ($partbrand === 0) {
        $content .= "   <td style='width:10%;'><input value='" . $value . "' style='border: none; width:100%;'></td>\r\n";
        }
        if ($partbrand === false) {
        $content .= "   <td style='width:10%;'><input value='empty' style='border: none; width:100%;'></td>\r\n";
        }
        if ($partmodel === 0) {
        $content .= "   <td style='width:10%;'><input value='" . $value . "' style='border: none; width:100%;'></td>\r\n";
        $content .= "</tr>\r\n";            
    }
        if ($partmodel === false) {
        $content .= "   <td style='width:10%;'><input value='empty' style='border: none; width:100%;'></td>\r\n";
        $content .= "</tr>\r\n";            
    }
}

Print_r示例:

Array (
  [Part_Quantity_1] => 1
  [Part_Condition_1] => New
  [Part_Brand_1] => brand1
  [Part_Model_1] => model1
  [Part_Quantity_2] => 2
  [Part_Condition_2] => New
  [Part_Brand_2] => 2
  [Part_Model_2] => 2
  [Estimated_Total] => 0
  [Estimated_Hours] => 0
  [Name] => JohnDoe
  [Email] => test@test.com
  [Email_Verify] => test@test.com
  [Confirmation] => I agree
  [NameID] => form2-name
  [EmailID] => form2-email
  [HoursID] => form2-hours
  [AjaxCall] => form2_ajax
)

2 个答案:

答案 0 :(得分:0)

试试这个。

// set dummy $_POST
$_POST = [
    'Part_Quantity_1' => '1',
    'Part_Condition_1' => 'New',
    'Part_Brand_1' => 'brand1',
    'Part_Model_1' => 'model1',
    'Part_Quantity_2' => '2',
    'Part_Condition_2' => 'New',
    'Part_Brand_2' => '2',
    'Part_Model_2' => '2',
    'Part_Quantity_3' => '3',
    'Estimated_Total' => '0',
    'Estimated_Hours' => '0',
    'Name' => 'JohnDoe',
    'Email' => 'test@test.com',
    'Email_Verify' => 'test@test.com',
    'Confirmation' => 'I agree',
    'NameID' => 'form2-name',
    'EmailID' => 'form2-email',
    'HoursID' => 'form2-hours',
    'AjaxCall' => 'form2_ajax'
];

$parts = [];
foreach ($_POST as $key => $value) {
    if (preg_match('#^Part_(?<type>Quantity|Condition|Brand|Model)_(?<counter>\d+)$#', $key, $matches) === 1) {
        if (!isset($parts[$matches['counter']])) {
            $parts[$matches['counter']] = array_fill_keys(['Quantity', 'Condition', 'Brand','Model'], 'empty');
        }
        $parts[$matches['counter']][$matches['type']] = $value;
    }
}

$rows = array_map(function($part) {
    $cells = [];
    foreach ($part as $key => $value) {
        $value = htmlspecialchars($value);
        $width = null;

        switch($key) {
        case 'Quantity':
            $width = "15%";
            break;
        case 'Condition':
            $width = "60%";
            break;
        case 'Brand':
        case 'Model':
            $width = "10%";
            break;
        }

        $cells[] = "    <td style=\"border: none; width: {$width};\"><input value=\"{$value}\"></td>";
    }

    return implode("\n", $cells);
}, $parts);

$table = "<tr>\n" . implode("\n<tr>\n", $rows) . "\n</tr>\n";

echo $table;

答案 1 :(得分:0)

不确定预期的输出或帖子数据是什么,但这应该有用,但它未经测试。我还注意到你没有输出行的标题

foreach($_POST as $key => $value) {
    switch($key) {
        case 'Part_Quantity':
            $width = "15%";
        break;
        case 'Part_Condition':
            $width = "60%";
        break;
        default:
            $width = "10%";
        break;
    }
    $content = "<tr>";
    $content .= "<td style='width:".$width.";'><input value='" . (is_int($value) ? $value : 'empty' . "' style='border: none; width:100%;'></td>";
    $content .= "</tr>";
}