ProcessWire - Datenbank manuell abfragen: Unterschied zwischen den Versionen
Aus Wikizone
Steff (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „ $result = $this->db->query("SELECT id, name, title, url FROM yourtablename WHERE id=$id");“) |
Steff (Diskussion | Beiträge) |
||
| Zeile 1: | Zeile 1: | ||
| + | https://processwire.com/talk/topic/1684-reading-and-displaying-data-from-a-custom-table/ | ||
| + | http://processwire.com/api/include/ (Bootstraping -> import data to recide in PW | ||
| + | == Snippets == | ||
| + | === Datenbankabfrage aus PW === | ||
$result = $this->db->query("SELECT id, name, title, url FROM yourtablename WHERE id=$id"); | $result = $this->db->query("SELECT id, name, title, url FROM yourtablename WHERE id=$id"); | ||
| + | |||
| + | === Externe Daten als Seite (mit URL) ausgeben === | ||
| + | But if that data needs to stay external, then Sinnut's solution is a good way to go. You would use the DB's primary key (or some other unique column) to serve as the urlSegment that loads the page. You'd setup one page/template to handle all that data, and it would find it like this: | ||
| + | <syntaxhighlight lang="php"> | ||
| + | $key = (int) $input->urlSegment1; | ||
| + | if(!$key) throw new Wire404Exception(); | ||
| + | |||
| + | $result = $yourDB->query("SELECT make, model, year FROM your_table WHERE id=$key"); | ||
| + | if(!$item->num_rows) throw new Wire404Exception(); | ||
| + | |||
| + | list($make, $model, $year) = $result->fetch_row(); | ||
| + | |||
| + | echo "<ul>"; | ||
| + | echo "<li>Make: $make</li>"; | ||
| + | echo "<li>Model: $model</li>"; | ||
| + | echo "<li>Year: $year</li>"; | ||
| + | echo "</ul>"; | ||
| + | </syntaxhighlight> | ||
Version vom 17. Oktober 2018, 15:56 Uhr
https://processwire.com/talk/topic/1684-reading-and-displaying-data-from-a-custom-table/ http://processwire.com/api/include/ (Bootstraping -> import data to recide in PW
Snippets
Datenbankabfrage aus PW
$result = $this->db->query("SELECT id, name, title, url FROM yourtablename WHERE id=$id");
Externe Daten als Seite (mit URL) ausgeben
But if that data needs to stay external, then Sinnut's solution is a good way to go. You would use the DB's primary key (or some other unique column) to serve as the urlSegment that loads the page. You'd setup one page/template to handle all that data, and it would find it like this:
$key = (int) $input->urlSegment1;
if(!$key) throw new Wire404Exception();
$result = $yourDB->query("SELECT make, model, year FROM your_table WHERE id=$key");
if(!$item->num_rows) throw new Wire404Exception();
list($make, $model, $year) = $result->fetch_row();
echo "<ul>";
echo "<li>Make: $make</li>";
echo "<li>Model: $model</li>";
echo "<li>Year: $year</li>";
echo "</ul>";