You are not logged in.

  • Login

1

Friday, October 29th 2010, 2:44pm

Sortieren einer Tabelle per JS mit Daten aus MYSQL Datenbank

Ich möchte in meinem Formular die Tabelle per klick auf die Spaltenköpfe (buttons) sortieren. Meine Idee war es das Wort "id" aus dieser Zeile durch die jeweiligen Namen der angeklickten Spaltenköpfe zu ersetzen:

PHP Quellcode

1
$abfrage = "SELECT * FROM bestellung ORDER BY  id DESC";

Klickt man also auf den Spaltenkopf "Name" ändert JS die Zeile in folgendes um:

PHP Quellcode

1
$abfrage = "SELECT * FROM bestellung ORDER BY  name DESC";


Ich weiß aber nicht wie ich die PHP Variable per JS ändern kann. Oder ob es nicht sogar einen besseren Weg gibt.

Ich habe mein Script mal angehängt.

PHP Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta Name="keywords" content="">
<title>Kundenbestellung</title>
<link rel="stylesheet" type="text/css" href="jp.css">
</head>
<body>
<!-- ***********************************************  Formular  *********************************************** -->
<form method="post" action="Jerry.html" Name="kundenbestellung" onreset="return ResetCheck()">
<div><img src="jp.gif" style="width: 100px; position: absolute; left: 600px;"></div>
<div style="width: 520px;" class="right" >
 <select Name="Formular" style="width: 110px;" onchange="setValue(this)">
  <option value="bestellung" <?php if ($_POST['Formular'] == "bestellung") {echo "selected";} ?>>Bestellung</option>
  <option value="reklamation" <?php if ($_POST['Formular'] == "reklmamation") {echo "selected";} ?>>Reklamation</option>
 </select>
</div>
<div style="font-size: 18px;">K U N D E N B E S T E L L U N G</div><br>
<div style="font-size: 18px;">Bestellung verwalten:</div><br>
<div style="font-size: 14px;">Momentane Sortierung:</div>
<br>
<table style="text-align: center;">
<tr>
<td class="bold"><input type="button" value=" Anrede " style="width: 100%; background-color: #bbb;"></td>
<td class="bold"><input type="button" value=" Name " style="width: 100%; background-color: #bbb;"></td>
<td class="bold"><input type="button" value=" zuschicken? " style="width: 100%; background-color: #bbb;"></td>
<td class="bold"><input type="button" value=" Marke " style="width: 100%; background-color: #bbb;"></td>
<td class="bold"><input type="button" value=" Id-Nummer " style="width: 100%; background-color: #bbb;"></td>
<td class="bold"><input type="button" value=" Artikel " style="width: 100%; background-color: #bbb;"></td>
<td class="bold"><input type="button" value=" Weite " style="width: 100%; background-color: #bbb;"></td>
<td class="bold"><input type="button" value=" L&auml;nge " style="width: 100%; background-color: #bbb;"></td>
<td class="bold"><input type="button" value=" Menge " style="width: 100%; background-color: #bbb;"></td>
<td class="bold"><input type="button" value=" Bestellt am " style="width: 100%; background-color: #bbb;"></td>
</tr>
<tr>
<td>
<?
// VERBINDUNG*****************************************************************************************************************
$verbindung = mysql_connect ("localhost", "d00f19fd","jpkunde") or die ("keine Verbindung möglich. Benutzername oder Passwort sind falsch");
mysql_select_db("d00f19fd") or die ("Die Datenbank existiert nicht.");
 
$abfrage = "SELECT * FROM bestellung ORDER BY id DESC";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis))
  {
  echo "
    <tr style=\"background-color: #FFF;\">
      <td class=\"b1 bold pad\">".$row->anrede."</td>
      <td class=\"b1 bold pad\">".$row->name."</td>
      <td class=\"b1 bold pad\">".$row->zuschicken."</td>
      <td class=\"b1 bold pad\">".$row->marke."</td>
      <td class=\"b1 bold pad\">".$row->idnr."</td>
      <td class=\"b1 bold pad\">".$row->artikel."</td>
      <td class=\"b1 bold pad\">".$row->weite."</td>
      <td class=\"b1 bold pad\">".$row->laenge."</td>
      <td class=\"b1 bold pad\">".$row->menge."</td>
      <td class=\"b1 bold pad\">".$row->bestelltam."</td>
    </tr>
  ";
   }
?>
</table>
</body>
</html>

2

Sunday, October 31st 2010, 11:47am

Wo bleibt das Problem? ;)
Du kannst die Daten entweder per POST übergeben, oder aber via GET Parameter als Link. Dann kommst du auch nicht in das Problem, dass Benennung und Spaltennamen der selbe sein muss.
Übrigens solltest du den Spaltennamen validieren. Zum Beispiel nur kleinbuchstaben von a-z erlauben

PHP Quellcode

1
preg_match('/^[a-z]+$/', $col)

3

Sunday, November 7th 2010, 12:16pm

Oder probiere doch mal das hier:
http://tablesorter.com/docs/

Social bookmarks