[PHP-NOTES] note 130205 added to language.variables

[email protected] ("Anonymous")
Newsgroups php.notes
Message-ID <[email protected]>
<?php
session_start();

// Datenbankverbindung etablieren
$servername = "mysql";
$username = "root";
$password = "htlkrems";
try {
  // Instanz des PDO Objektes erzeugen
  $conn = new PDO("mysql:host=$servername;dbname=classicmodels", $username, $password);
  // PDO Error Mode auf Exception setzen
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  //echo "Connected successfully"; // Ausgabe wenn DB-Verbindung ok
} catch (PDOException $e) {
  // Ausgabe der Fehlermeldung falls DB-Verbindung fehlgeschlagen
  echo "Connection failed: " . $e->getMessage();
}

if (isset($_GET['del'])) {

  if (isset($_GET['del']) && is_array($_GET['del'])) {

    $deleteIds = $_GET['del'];

    foreach ($deleteIds as $ele) {

      $conn->query("SET FOREIGN_KEY_CHECKS = 0");
      $conn->query("DELETE FROM offices WHERE officeCode LIKE '$ele'");
    }
  }
}

if (!isset($_SESSION['search'])) {
  $_SESSION['search'] = "";
}

if (isset($_GET["search"])) {
  $_SESSION['search'] = $_GET["search"];
}

if (!isset($_SESSION['sort'])) {
  $_SESSION['sort'] = "officeCode";
}

if (isset($_GET['sort'])) {
  $_SESSION['sort'] = $_GET['sort'];
}

$search = $_SESSION['search'];
if (isset($_GET['lett'])) {
  $lookfor = $_GET['lett'];
  $search = '';
  $_SESSION['search'] = '';
} else {
  $lookfor = $search;
}
$sort = $_SESSION['sort'];

$stmt = $conn->query("SELECT * FROM offices WHERE city LIKE '$lookfor%' ORDER BY $sort");   // SQL Query ausführen
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);      // alle Daten mit fetchAll fetchen -> Ergebnis ist ein 2-dimensionales Array

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Products</title>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
  <div class="container">
    <!-- STEP 1: Ausgabe des Array -->
    <?php
    // echo "<pre>"; print_r($result); echo "</pre>"; 
    ?>

    <!-- STEP 2: Ausgabe in einer HTML-Tabelle -->
    <h1>Products</h1>
    <table>
      <thead>
        <tr>
          <?php
          for ($i = 0; $i < 26; $i++) {
            echo "<th><a href='?lett=" . chr(65 + $i) . "'>" . chr(65 + $i) . "</a></th>";
          }
          ?>
        </tr>
      </thead>
    </table>
    <form action="offices.php" method="GET">
      <?php
      echo "<input type='text' name='search' value='" . $_SESSION['search'] . "'>";
      ?>
      <input type="submit">
    </form>
    <form action="offices.php" method="GET">
      <table class="table">
        <thead>
          <tr>
            <th></th>
            <?php
            echo "<th><a href='?sort=officeCode'>Office Code</a></th>";
            echo "<th><a href='?sort=city'>City</a></th>";
            echo "<th><a href='?sort=phone'>Phone</a></th>";
            echo "<th><a href='?sort=addressLine1'>Address 1</a></th>";
            echo "<th><a href='?sort=addressLine2'>Address 2</a></th>";
            echo "<th><a href='?sort=state'>State</a></th>";
            echo "<th><a href='?sort=country'>Country</a></th>";
            echo "<th><a href='?sort=postalCode'>Postal Code</a></th>";
            echo "<th><a href='?sort=territory'>Territory</a></th>";
            ?>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <?php
          foreach ($result as $row) {
            // Iteration über alle Datensätze -> Ergebnis ist ein 1-dimensionales assoziatives Array
            // print_r($result); // Ausgabe des Arrays zu Testzwecken
            echo "<tr>";
            // Werte in einer Table-row ausgeben
            echo "<td><input type='checkbox' name='del[]' value='" . $row['officeCode'] . "'></td>";
            echo "<td>" . $row['officeCode'] . "</td>";
            echo "<td>" . $row['city'] . "</td>";
            echo "<td>" . $row['phone'] . "</td>";
            echo "<td>" . $row['addressLine1'] . "</td>";
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 193.170.70.27)
----
Manual Page -- https://php.net/manual/en/language.variables.php
Edit        -- https://main.php.net/note/edit/130205
Del: integrated  -- https://main.php.net/note/delete/130205/integrated
Del: useless     -- https://main.php.net/note/delete/130205/useless
Del: bad code    -- https://main.php.net/note/delete/130205/bad+code
Del: spam        -- https://main.php.net/note/delete/130205/spam
Del: non-english -- https://main.php.net/note/delete/130205/non-english
Del: in docs     -- https://main.php.net/note/delete/130205/in+docs
Del: other reasons-- https://main.php.net/note/delete/130205
Reject      -- https://main.php.net/note/reject/130205
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.