За последние 24 часа нас посетили 36938 программистов и 7904 робота. Сейчас ищут 1754 программиста ...

Помогите с основами работы с массивами в PHP

Тема в разделе "PHP для новичков", создана пользователем misenor368, 31 дек 2024.

  1. misenor368

    misenor368 Новичок

    С нами с:
    16 дек 2024
    Сообщения:
    2
    Симпатии:
    0
    Hello!

    I am just starting to learn PHP and I am having trouble understanding how to work with arrays. Maybe one of you can help me figure it out.

    I have a task: I need to create an array that will contain a list of products (for example, the name and price of each product), and then display this list on the screen in the form of an understandable table. So far, I have written the following code:

    Код (Text):
    1. <?php
    2.  
    3. $products = array(
    4.     array("name" => "Product 1", "price" => 100),
    5.     array("name" => "Product 2", "price" => 200),
    6.     array("name" => "Product 3", "price" => 300)
    7. );
    8.  
    9. foreach ($products as $product) {
    10.     echo $product["name"] . " - " . $product["price"] . " rub.<br>";
    11. }
    12. ?>
    The code seems to work, but the output is a simple list. How can I make the information appear as an HTML table?

    I would be grateful for code examples or links to articles that explain such things in detail.

    Thank you!
     
  2. Drunkenmunky

    Drunkenmunky Старожил

    С нами с:
    12 авг 2020
    Сообщения:
    1.511
    Симпатии:
    284
    PHP:
    1. <?php
    2. $products = array(
    3.     array("name" => "Product 1", "price" => 100),
    4.     array("name" => "Product 2", "price" => 200),
    5.     array("name" => "Product 3", "price" => 300)
    6. );
    7.  
    8. echo '<table border=1 cellspacing=4 cellpadding=0>
    9.  <tr>
    10.    <th>name</th>
    11.    <th>price</th>
    12.  </tr>';
    13.  
    14. $template = "  <tr>\n    <td>%s</td>\n    <td>%d</td>\n  </tr>\n";
    15.  
    16. foreach ($products as $product)
    17. {
    18.     printf($template, htmlspecialchars($product["name"]), $product["price"]);
    19. }
    20.  
    21. echo '</table>';
    22. ?>
     
  3. Vladimir Kheifets

    Vladimir Kheifets Новичок

    С нами с:
    23 сен 2023
    Сообщения:
    485
    Симпатии:
    97
    Адрес:
    Бавария, Германия
    Hello!
    PHP:
    1. <html>
    2. <head>
    3. <style>
    4. table, td{border-collapse:collapse}
    5. td{padding: 3 5 3 5}
    6. </style>
    7. <head>
    8. <body>
    9.  
    10. <?php
    11.  
    12. class viewArray{
    13.  
    14.     private function tableRow($cols) {
    15.         $row = "";
    16.         foreach($cols as $col)
    17.             $row .= "<td>$col</td>";
    18.         return "<tr>$row</tr>";
    19.     }
    20.  
    21.     public function arrayToHtmlTable($arr){
    22.         $table = "<table border=1>";
    23.         $table .= $this->tableRow(array_keys($arr[0]));
    24.         foreach($arr as $row)
    25.             $table .= $this->tableRow($row);
    26.         $table .= "</table>";
    27.         return $table;
    28.     }
    29. }
    30.  
    31.  
    32.  
    33. $obj = new viewArray();
    34.  
    35. $products = array(
    36.     array("name" => "Product 1", "price" => 100),
    37.     array("name" => "Product 2", "price" => 200),
    38.     array("name" => "Product 3", "price" => 300)
    39. );
    40.  
    41. echo $obj -> arrayToHtmlTable($products);
    42.  
    43. $products = array(
    44.     array("name" => "Product 1", "price" => 100, "height" => 10, "length" => 10 ),
    45.     array("name" => "Product 2", "price" => 200, "height" => 20, "length" => 20),
    46.     array("name" => "Product 3", "price" => 300, "height" => 30, "length" => 30 )
    47. );
    48.  
    49. echo $obj -> arrayToHtmlTable($products);
    Good luck!
     
  4. mkramer

    mkramer Суперстар
    Команда форума Модератор

    С нами с:
    20 июн 2012
    Сообщения:
    8.497
    Симпатии:
    1.726
    А класс тут зачем? Чтоб було?