Avatar

Datenbankabfrage für die Startseite (General)

by Auge ⌂, Friday, November 04, 2016, 12:00 (2731 days ago) @ hintersatz

Hallo

Bevor ich meinen Entwurf in das Wiki einfüge, möchte ich ihn hier vorstellen. Das soll auch als Test für die Verständlichkeit der Erklärung und des Codes dienen. ich bitte dich hiermit um eine Rückmeldung.

Los geht's:

<?php
 
// static database settings (copy'n'paste from config/db_settings.php)
$mlfdb['host'] = 'localhost';
$mlfdb['user'] = 'database_username';
$mlfdb['password'] = 'database_password_123';
$mlfdb['database'] = 'database_name';
// initialise the global variables
$mlfReturn = false;
$mlfOutput = '';
 
/**
 * reads the latest entries from the mlf2 database
 *
 * The number of entries is defined in the query (LIMIT x).
 *
 * @param array $db
 * @param bool $debug (optional)
 * @return bool false
 * @return array
 */
function readLatestForumEntries($db, $debug = false) {
 // initialise the variable to return the output
 $r = array();
 // connecting to the database
 $connid = @mysqli_connect($db['host'], $db['user'], $db['password'], $db['database']);
 // no connection (error)
 if (!$connid) return false;
 // define the query
 @mysqli_set_charset($connid, "utf8");
  $query = "SELECT
  ft.id,  -- the id of the entry itself
  ft.tid,  -- the id of the thread (for threaded view)
  DATE_FORMAT(ft.time + INTERVAL 1 HOUR, '%d.%m.%Y %H:%i:%s') AS posting_time,  -- date format must be adapted to the language settings
  if((ft.user_id > 0), ud.user_name, ft.name) AS name,  -- user name from the forum table or the users table (if available)
  ft.subject
 FROM mlf2_forum_table AS ft
  LEFT JOIN mlf2_userdata_table AS ud
   ON ud.user_id = ft.user_id
  -- accession 0: everybody, 1: only for registered users, 2: moderators and admins only
  -- leave out, if there are no categories in your forum
  WHERE ft.category IN(SELECT id FROM mlf2_category_table WHERE accession = 0)
  ORDER BY ft.time DESC  -- latest entries as first
  LIMIT 6";  -- limit the output to the latest 6 entries
 // debugging output
 if ($debug === true) {
  echo '<h3>the query string</h3>';
  echo '<pre>'. print_r($query, true) .'</pre>';
 }
 $result = mysqli_query($connid, $query);
 // debugging output
 if ($debug === true) {
  echo '<h3>the return value of <code>mysqli_query()</code></h3>';
  echo '<pre>'. print_r($result, true) .'</pre>';
 }
 if ($result === false) return false;
 while ($row = mysqli_fetch_assoc($result)) {
  $r[] = $row;
 }
 mysqli_free_result($result);
 mysqli_close($connid);
 return $r;
}
 
$mlfOutput  = '<section id="mlfLatestEntries">';
$mlfOutput .= '<h2>Latest entries in the forum</h2>';
$mlfReturn = readLatestForumEntries($mlfdb);
//$mlfReturn = readLatestForumEntries($mlfdb, true); // for debugging purposes
// if $mlfReturn is an array, build the list of the entries, otherwise display a "no entries" message
if (is_array($mlfReturn) === true) {
 $mlfOutput .= '<ul>';
 foreach ($mlfReturn as $row) {
  $mlfOutput .= '<li>';
  // URL has to be adapted to threaded-posting-view (nested listing) if needed
  $mlfOutput .= '<a href="http://example.com/path/to/forum/index.php?id='. htmlspecialchars($row['id']) .'">'. htmlspecialchars($row['subject']) .'</a> - '. htmlspecialchars($row['name']) .', '. htmlspecialchars($row['posting_time']);
  $mlfOutput .= '</li>';
 }
 $mlfOutput .= '</ul>';
} else {
 $mlfOutput .= '<p class="mlfErrorMessage">No entries found.</p>';
}
$mlfOutput .= '</section>';
echo $mlfOutput;
 
?>

Vorgehen:

1. Speichere den Code als PHP-Datei.
2. Passe die Tabellennamen und die Datenbankzugangsdaten an ($mlfdb).
3. Passe die Anzahl der zur Darstellung vorgesehenen Postings an (LIMIT x).
4. Passe den Pfad zum Forum bei der Zusammenstellung des Links an, wenn es ein Link werden soll. Ansonsten entferne den Link und die darin enthaltene URL (<a href="..."> und </a>). Die Felder ft.id und ft.tid können dann auch aus der Abfrage raus.
5. Ersetze den Vorgabetext der Überschrift mit einem in der gewünschten Sprache (vermutlich deutsch?).
6. Lade die angepasste Datei auf deinen Webspace hoch.
7. Binde das Skript mit include('pfad/zum/skript.php'); an der gewünschten Stelle in deine Startseite ein und rufe diese auf.

Es sollten nun die x letzten Beiträge angezeigt werden. Das Datumsformat entspricht dem in Deutschland üblichen Format (tt.mm.jjjj) mit führenden Nullen. Wie gesagt, achte auf die Verlinkung, die du wohl nicht haben willst (nicht öffentliches Forum).

Um die Gestaltung mit CSS kümmern wir uns hernach.

Tschö, Auge

--
Trenne niemals Müll, denn er hat nur eine Silbe!


Complete thread:

 RSS Feed of thread