首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Excise数组、向量和更多数组的集合Pt 1

Excise数组、向量和更多数组的集合Pt 1
EN

Stack Overflow用户
提问于 2019-06-04 10:19:01
回答 1查看 68关注 0票数 0

我最近开始学习,我的朋友传给我这些练习,但似乎对我来说还为时过早我无法使用数组和向量构建表格我甚至尝试自己尝试,但视频课程没有显示太多与我朋友传给我的练习相关的内容

代码语言:javascript
复制
<!--Include 5 values in a vector, and then display all values in an HTML table using for or foreach-->
<?php
 $name = array ("$name1", "$name2", "$name3", $name4 $name5);

/*Here I do not know how to add values to the names in the array using post
Much less For and Foreach
 */

 ?>
<html>
<head>
<title>1</title>
<meta charset="UTF-8">
</head>
<body>
<table method="post">
  <tr>
    <th>Names</th>
    <th></th>
  </tr>
  <tr>
    <td>1</td>
    <td></td>
  </tr>
  <tr>
    <td>2</td>
    <td></td>
  </tr>
  <tr>
    <td>3</td>
    <td></td>
  </tr>
  <tr>
    <td>4</td>
    <td></td>
  </tr>
  <tr>
    <td>5</td>
    <td></td>
  </tr>
</table>
</body>
</html>

嗯,我尽了最大的努力,但我不知道,也没有把它做完,你能帮我吗?

代码语言:javascript
复制
<!-- Read 4 numbers via POST, place them in a vector and show them on screen -->
<?php


$html = '<html>
<head>
<title>3</title>
<meta charset="UTF-8">
</head>
<body>';

$value1 = 1;
$value2 = 2;
$value3 = 3;
$value4 = 4;

$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
$value3 = $_POST['value3'];
$value4 = $_POST['value4'];



/*Here i have a doubt, i dont know how to show te values in a div */
echo "<div>1º value:  </div>\n";
echo "<div>2º value:  </div>\n";
echo "<div>3º value:  </div>\n";
echo "<div>4º value:  </div>\n";


$finalvalue = array [['value1', 'value2'],['value3', 'value4']];

echo $finalvalue;

$html .= "</body>
</html>";
EN

回答 1

Stack Overflow用户

发布于 2019-06-04 12:49:03

欢迎!

你真的做得很好!

我们的第一个练习可能类似于:

代码语言:javascript
复制
<?php

// This would the start of our HTML
$html = '<html>
<head>
<title>1</title>
<meta charset="UTF-8">
</head>
<body>';

$names = array("name1", "name2", "name3", "name4", "name5");

// This would the start of our Table
$html .= '<table>';
foreach ($names as $key => $value) {
    $html .= '<tr>';
    $html .= '<th>' . $key . '</th>';
    $html .= '<th>' . $value . '</th>';
    $html .= '</tr>';
}

$html .= '</table>';

// This would the end of our HTML
$html .= "</body>
</html>";

// Here, we will be printing it.
echo $html;

这是一种方法,我们从一个$html变量开始,然后一步一步地添加,最后打印出来。

总和

代码语言:javascript
复制
<?php

$html = '<html>
<head>
<title>1</title>
<meta charset="UTF-8">
</head>
<body>';

$big_numbers_array = [[10, 11, 12], [13, 14, 15]];

// Here we have the sum each row and sum of both rows

echo "<div>Sum of row one is: " . array_sum($big_numbers_array[0]) . "</div>\n";
echo "<div>Sum of row two is: " . array_sum($big_numbers_array[1]) . "</div>\n";

echo "<div>Sum of all rows is: " . (array_sum($big_numbers_array[0]) + array_sum($big_numbers_array[1])) . "</div>\n";


// Here we would be having a table with cumulative sum: 

$html = '<table>';
$html .= '<tr>';
$html .= "<td>Number</td>";

foreach ($big_numbers_array as $big_key => $small_numbers_array) {
    foreach ($small_numbers_array as $small_key => $number) {
        $html .= "<tr>";
        $html .= "<th>" . $big_key . ": " . $small_key . "</th>";
        $sum += $number;
        $html .= "<th>" . $sum . "</th>";
        $html .= "</tr>";
    }
}

$html .= '</table>';

$html .= "</body>
</html>";

echo $html;

错误

代码中的bug是[之前的单词array。您的代码将如下所示:

代码语言:javascript
复制
$html = '<html>
<head>
<title>3</title>
<meta charset="UTF-8">
</head>
<body>';

$value1 = 1;
$value2 = 2;
$value3 = 3;
$value4 = 4;

$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
$value3 = $_POST['value3'];
$value4 = $_POST['value4'];

/*Here i have a doubt, i dont know how to show te values in a div */
echo "<div>1º value: </div>\n";
echo "<div>2º value: </div>\n";
echo "<div>3º value: </div>\n";
echo "<div>4º value: </div>\n";

$finalvalue = [['value1', 'value2'], ['value3', 'value4']];

$html .= "</body>
</html>";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56436570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档