php - Set of Excises arrays, vectors and more array Pt 1

881

I started studying recently and my friend passed me these exercises, but it seems that it is still too early for me I'm having difficulty building tables with arrays and vectors I even tried to try to do it on my own, but the video lessons did not show much related to the exercises that my friend passed to me

<!--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>

Well, i tried my best to do it, but i don't know and didn't get it all over, can you help me?

<!-- 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>";

779

Answer

Solution:

Welcome!

You are actually doing pretty great!

Our first exercise would likely look like something similar to:

<?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;

This is one way to do that, which we would be starting with a$html variable, and then we would step by step add to that, and finally we will be printing it.


Sum

<?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

enter image description here

The bug in the code was the wordarray before[. Your code would look like:

$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>";

People are also looking for solutions to the problem: Convert openssl command to php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.