Programme: Table HTML press



  • I have a table of states of storage for like 50 states. How to print the names of the US 50 states in a table of six columns.

    The challenge here is how to paint a table with horizontal columns with a fixed number of cells in the row. For now, I've only come up with the title of the function and what it looks like.

    echo grid_horizontal(array("A","B","C","D","E","F","A","B","C","D","E","F","AA"),6);
    

    echo grid_horizontal(array("A","B","C","D","E","F","A","B","C","D","E","F","AA"),3);

    I'm doing this for myself, and I've been advised to put my hand on these tasks, and how to start now.

    Don't judge your friends, I'll be glad to have any help.



  • I'll use the data you provided.

    The first fills many lines of the table, the second is in the next line and so on. Finally we reach the final line, which may need to be filled with empty tables. The grid_horizontal function, as shown in the example, allows Specify the mass and number of columns. It implies the width of the table, equal to 100 per cent, but it can be altered with a variable $table_width.

    function grid_horizontal($array, $size) {
        $table_width = 100;
        $width = intval($table_width / $size);
        $tr = '<tr align="center">';
        $td = "<td width=\"$width%%\">%s</td>";
        $grid = "<table width=\"$table_width%\">$tr";
        $i = 0;
        foreach ($array as $e) {
            $grid .= sprintf($td, $e);
            $i++;
            if (!($i % $size)) {
            $grid .= "</tr>$tr";
            }
        }
        while ($i % $size) {
            $grid .= sprintf($td, '&nbsp;');
            $i++;
        }
        $end_tr_len = strlen($tr) * -1;
        if (substr($grid, $end_tr_len) != $tr) {
         $grid .= '</tr>';
        } else {
            $grid = substr($grid, 0, $end_tr_len);
        }
        $grid .= '</table>';
        return $grid;
    }
    echo grid_horizontal(array("A","B","C","D","E","F","A","B","C","D","E","F","AA"),6);
    

    Use the base. http://php.net/manual/ru/class.pdo.php

    try{
        $db = new PDO("mysql:host=yourhost;dbname=yourdbname;charset=utf8","username","password");
        $query=$db->prepare("Select state from states");
        $query->excute();
        $states = [];
        while($row=$query->fetch(PDO::FETCH_OBJ)) {
            $states[] =  $row->state;
        }
    }catch(PDOException  $e ){
        echo "Error: ".$e->getMessage();
    }
    // генерируем HTML  таблицу
    $grid = grid_horizontal($states, 6);
    // и печатаем ее
    print $grid;   
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2