Version: 0.11

Type: Full Script

Category: Databases

License: GNU General Public License

Description: A set of PHP3-scripts to adminstrate MySQL databases over the web. Idea: Encode GUI specs in MySQL tables. Features: Generates GUI to MySQL databases automatically. Uses MySQL and CGI realms for user authentication. Search databases by up to two fields. Sort by any field. You need to activate track_vars in your PHP.INI file. This snippet contains serveral ascii files concatenated one after another and separated by 'BEGIN' and 'END' tags that are not part of the PHP code.



******** BEGIN Generic/README
PHPMyGeneric - README
----------------

A set of PHP3-scripts to adminstrate MySQL databases over the WWW.

Version 0.1 - 20/07/2000

Copyright (C) 2000 Marko R. Riedel <mriedel@neuearbeit.de>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License <http://www.opensource.org/gpl-license.html>
for more details.

Requirements: 
-------------

  PHP3
  MySQL (tested with 3.21.x, 3.22.x and 3.23.x, works also in 3.20.x, 
        but in this case the "SHOW" commands should be commented out in 
        index.php3)
  a web-browser (doh!)

Idea
----

Encode GUI specs in MySQL tables.

Features
--------

Generates GUI to MySQL databases automatically. Uses MySQL and CGI
realms for user authentication. Search databases by up to two
fields. Sort by any field.

Example
-------

Suppose you have a database YellowPages that contains three tables:
books, people, and projects.

books contains the following fields:

+-------------------+
| field             |
+-------------------+
| id                |
| title             |
| publisher         |
| author            |
| erscheinungsdatum |
| projekt           |
| contactfname      |
| contactlname      |
| notes             |
+-------------------+

people contains the following fields:



+-------------------+
| field             |
+-------------------+
| id                |
| vorname           |
| name              |
| projekt           |
| telefon           |
| email             |
| responsibilities  |
| subfname          |
| sublname          |
| arbeitszeiten     |
| skills            |
| projekterfahrung  |
| expert            |
| hauspostanschrift |
| str               |
| Ort               |
| plz               |
| cellular          |
| fax               |
+-------------------+

projects contains the following fields:

+-------------------+
| field             |
+-------------------+
| id                |
| abteilung         |
| dienststelle      |
| name              |
| headfname         |
| headlname         |
| hauspostanschrift |
| phone             |
| fax               |
| email             |
| vicefname         |
| vicelname         |
| street            |
| zip               |
| town              |
| notes             |
+-------------------+

Note that the field 'id' is required an contains a unique identifier
of each record.

In order to build a web interface to your database, you create a
database called YellowPagesGUI (you form the name by appending the
string "GUI" to the name of your original database), containing the
tables books, projects, people, defaults and lookups.

The tables books, projects, and people of the YellowPagesGUI database
encode the GUI to the corresponding tables in the YellowPages
database. Each contains the following columns:

* field: the name of the field, e.g.
mysql> select field from books;
+-------------------+
| field             |
+-------------------+
| id                |
| title             |
| publisher         |
| author            |
| erscheinungsdatum |
| projekt           |
| contactfname      |
| contactlname      |
| notes             |
+-------------------+

* action: either empty, 'view', 'delete' or 'mail': what happens when
  you click on the field, e.g.
mysql> select action from books;
+--------+
| action |
+--------+
|        |
| view   |
| edit   |
|        |
|        |
|        |
|        |
|        |
|        |
+--------+

* rank: defines the order in which fields are displayed, e.g.
mysql> select rank from books;
+------+
| rank |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
|    7 |
|    8 |
|    9 |
+------+

* menu: either empty or 'tablename:fieldname' if a menu containing the
  values of the column fieldname from the table tablename should be
  displayed when entering or editing records, e.g.
mysql> select menu from books;
+---------------+
| menu          |
+---------------+
| NULL          |
| NULL          |
| NULL          |
| NULL          |
| NULL          |
| projects:name |
| NULL          |
| NULL          |
| NULL          |
+---------------+

* browse: either 'Y' or 'N', indicates whether the field should be
  displayed in the table that is used to browse the database, e.g.
mysql> select browse from books;
+--------+
| browse |
+--------+
| N      |
| Y      |
| Y      |
| Y      |
| Y      |
| Y      |
| Y      |
| Y      |
| Y      |
+--------+

* title: the title of the field in your language, e.g.
mysql> select title from books;
+--------------------------------+
| title                          |
+--------------------------------+
| Id                             |
| Titel                          |
| Verlag                         |
| Autor                          |
| Erscheinungsdatum (JJJJ-MM-TT) |
| Projekt                        |
| Ansprechpartner (Vorname)      |
| Ansprechpartner (Nachname)     |
| Notizen                        |
+--------------------------------+

* textarea: either 'Y' or 'N', indicates whether a textarea should be
  used to enter and display the field.

When you have coded the user interface to your database (i.e. the
tables books, people and projects in the YellowPages database in our
example), you still have to fill in the 'defaults' and 'lookups'
tables.

The 'defaults' table for the YellowPagesGUI is shown below.

mysql> select * from defaults;
+----------+-------------+------+--------+
| name     | title       | id   | search |
+----------+-------------+------+--------+
| books    | B&uuml;cher | id   | title  |
| people   | Personal    | id   | name   |
| projects | Projekte    | id   | name   |
+----------+-------------+------+--------+

It contains the title of each table in your language, what field
should be used as a unique identifier, and what by what field the
table should by default be sorted.

The 'lookups' table for the YellowPagesGUI is shown below.

mysql> select * from lookups;
+----------+----------+---------------------------+--------------+
| source   | dest     | sourcefields              | destfields   |
+----------+----------+---------------------------+--------------+
| books    | projects | projekt                   | name         |
| books    | people   | contactfname:contactlname | vorname:name |
| people   | projects | projekt                   | name         |
| people   | people   | subfname:sublname         | vorname:name |
| projects | people   | headfname:headlname       | vorname:name |
| projects | people   | vicefname:vicelname       | vorname:name |
+----------+----------+---------------------------+--------------+

This table specifies links between the three tables that make up the
YellowPages database and must be present, although it may be left
empty. The first line says that by clicking on the 'projekt' field in
the books browse view, the user will be transferred to the record in
the projects table whose 'name' entry matches the 'projekt' field in
the books table. The second line requires two matches instead of one;
two is the maximum.

You would have to enter the five tables described above if you had the
YellowPages database on your machine. Assuming you had the
YellowPagesGUI database filled in correctly and the PHPMyGeneric
scripts were placed in the document root of your webserver, you could
then point your browser to

http://localhost/PHPMyGeneric/list.php3?dbases=YellowPages&table=books

and start working with your database.


You need to activate track_vars in your PHP.INI file.


******** END   Generic/README
******** BEGIN Generic/common.php3
<?
function getDefaults()
{
  global $conn, $dbaseGUI;
  global $tbTitles, $tbId, $tbSearch;

  $query  = "SELECT * FROM defaults";
  $result = mysql_db_query($dbaseGUI, $query, $conn) or
            reAuthenticate
              ("Datenbanklesezugriff fehlgeschlagen (get defaults).");
  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $tbTitles[$row["name"]] = $row["title"];
    $tbId[$row["name"]] = $row["id"];
    $tbSearch[$row["name"]] = $row["search"];
  }
}

function getGUI()
{
  global $conn, $dbaseGUI, $table, $tbId;

  global $flds, $hashToIndex, $fieldCount, $fields;
  global $fldAction, $fldMenu, $fldTitle, $fldBrowse, $fldTextarea;
  global $idFld;

  $query  = "SELECT * FROM $table ORDER BY rank ASC";
  $result = mysql_db_query($dbaseGUI, $query, $conn) or
            reAuthenticate
              ("Datenbanklesezugriff fehlgeschlagen (get GUI).");
  $i=0; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $flds[$i]=$row["field"];
  
    $hashToIndex[$row["field"]]=$i;
  
    $fldAction[$i] = $row["action"];
    $fldMenu[$i]   = $row["menu"];
    $fldTitle[$i]  = $row["title"];
  
    $fldBrowse[$i]   = !strcmp($row["browse"],   'Y') ? 1 : 0;
    $fldTextarea[$i] = !strcmp($row["textarea"], 'Y') ? 1 : 0;
  
    if(!strcmp($row["field"], $tbId[$table]))
      $idFld=$i;
  
    $i++;
  }

  $fields = implode($flds, ", ");
  $fieldCount=$i;
}

function getMenu($field, $menuTable, $menuField, $selected)
{
  global $conn, $dbase;

  $query  = "SELECT $menuField " .
              "FROM $menuTable ORDER BY $menuField ASC";
  $result = mysql_db_query($dbase, $query, $conn) or
            reAuthenticate
              ("Datenbanklesezugriff fehlgeschlagen (get menu).");;
?>
<SELECT NAME=<? echo $field?>>
<?
  if(!strcmp($selected, "") || 
      preg_match("/^\s+$/", $selected)){
      $row = mysql_fetch_row($result);
      echo "<OPTION SELECTED VALUE=\"$row[0]\"> $row[0]\n";

      while ($row = mysql_fetch_row($result))
        echo "<OPTION VALUE=\"$row[0]\"> $row[0]\n";
  }
  else{
    $present=0;
    while ($row = mysql_fetch_row($result)){
      echo "<OPTION ";
      if(!strcmp($row[0], $selected)){
         $present=1;
         echo "SELECTED";
      }
      echo " VALUE=\"$row[0]\"> $row[0]\n";
    }
    if(!$present)
      echo "<OPTION SELECTED VALUE=\"$selected\"> $selected\n";
  }
?>
</SELECT>
<?
}

function constructHiddenArgs()
{
  global $dbase, $table, $hiddenArgs;
  global 
    $operator, $search, $toSearch, $search1, $toSearch1, $sortBy;

  $hiddenArgs=
    "<INPUT type=hidden name=dbase value=$dbase>\n" .
    "<INPUT type=hidden name=table value=$table>\n";

  if($search!=""){
    $hiddenArgs .= "<INPUT type=hidden name=search " . 
                   " value=\"$search\">\n";
    $hiddenArgs .= "<INPUT type=hidden name=toSearch value=$toSearch>\n";
  }
  if($search1!=""){
    $hiddenArgs .= "<INPUT type=hidden name=operator value=$operator>\n";
    $hiddenArgs .= "<INPUT type=hidden name=search1" .
                   " value=\"$search1\">\n";
    $hiddenArgs .= "<INPUT type=hidden name=toSearch1 value=$toSearch1>\n";
  }
  if($sortBy!="")
    $hiddenArgs .= "<INPUT type=hidden name=sortBy value=$sortBy>\n";
}

function constructUrl()
{
  global $dbase, $table, $url;
  global 
   $operator, $search, $toSearch, $search1, $toSearch1, $sortBy;


  $url="list.php3?dbase=$dbase&table=$table&time=" . time();
  if($search!="")
    $url .= "&search=" . rawurlencode($search) . "&toSearch=$toSearch";
  if($search1!="")
    $url .= "&operator=$operator" . 
          "&search1=" . rawurlencode($search1) . "&toSearch1=$toSearch1";
  if($sortBy!="")
    $url .= "&sortBy=$sortBy";

}


function authenticate(){
  global $PHP_AUTH_USER, $PHP_AUTH_PW;
  global $badUser, $badPW;

  if(!isset($PHP_AUTH_USER) ||
      (isset($badUser) && !strcmp($badUser, $PHP_AUTH_USER) &&
       isset($badPW) && !strcmp($badPW, $PHP_AUTH_PW))){
    Header("WWW-Authenticate: Basic " . 
           "realm=\"YellowPages\"");
    Header("HTTP/1.0 401 Unauthorized");

    echo "<HR>\n<H1> Access denied. </H1>\n";

    $url = "list.php3?time=" . time();
    if(isset($badUser) && isset($badPW))
      $url .= "&badUser=$badUser&badPW=$badPW";

    echo "<H2><A HREF=\"$url\">Try again.</A></H2>\n";

    exit; 
  }
}

function reAuthenticate($msg){
  global $PHP_AUTH_USER, $PHP_AUTH_PW;

  echo "<HR>\n<H1> $msg </H1>\n";

  $url = "list.php3?time=" . time() .
        "&badUser=$PHP_AUTH_USER&badPW=$PHP_AUTH_PW";

  echo "<H2><A HREF=\"$url\">Try again.</A></H2>\n";

  exit;
}

?>
******** END   Generic/common.php3
******** BEGIN Generic/delete.php3
<?
require("common.php3");

$host  = "localhost";

if(!isset($dbase) || !isset($table)){
  $dbase = "YellowPages";
  $table = "books";
}

authenticate();
$user  = $PHP_AUTH_USER;
$pass  = $PHP_AUTH_PW;


$conn  = mysql_connect($host, $user, $pass) or
         reAuthenticate("Database logon incomplete. (Delete.)");

$query  = "DELETE FROM $table WHERE id=$id";
mysql_db_query($dbase, $query, $conn);

constructUrl();
header("Location: $url");
?>
******** END   Generic/delete.php3
******** BEGIN Generic/doEdit.php3
<?
require("common.php3");

$host  = "localhost";

if(!isset($dbase) || !isset($table)){
  $dbase = "YellowPages";
  $table = "books";
}

authenticate();
$user  = $PHP_AUTH_USER;
$pass  = $PHP_AUTH_PW;


$conn  = mysql_connect($host, $user, $pass) or
         reAuthenticate("Database logon incomplete. (Do edit.)");

$dbaseGUI   = $dbase . "GUI";

getDefaults();

$query  = "SELECT field FROM $table ORDER BY rank ASC";
$result = mysql_db_query($dbaseGUI, $query, $conn) or
          reAuthenticate("Cannot read from database. (Edit.)");

$i=0; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  if(!strcmp($row["field"], $tbId[$table]))
    continue;

  $flds[$i]=$row["field"];
  $vals[$i]=addSlashes($HTTP_POST_VARS[$flds[$i]]); 

  $i++;
}
$updateCount=$i;

$query  = "UPDATE $table SET ";
for($i=0; $i<$updateCount-1; $i++)
  $query .= $flds[$i] . "='" . $vals[$i] . "', ";
$query .= $flds[$i] . "='" . $vals[$i] . "' ";
$query .= "WHERE " . $tbId[$table] ."='$id'";


$result = mysql_db_query($dbase, $query, $conn) or
  reAuthenticate("Cannot write to database. (Edit.)");



constructUrl();
header("Location: $url");
?>
******** END   Generic/doEdit.php3
******** BEGIN Generic/doInsert.php3
<?
require("common.php3");

$host  = "localhost";

if(!isset($dbase) || !isset($table)){
  $dbase = "YellowPages";
  $table = "books";
}

authenticate();
$user  = $PHP_AUTH_USER;
$pass  = $PHP_AUTH_PW;

$conn  = mysql_connect($host, $user, $pass) or
         reAuthenticate("Database logon incomplete. (Do insert.)");

$dbaseGUI   = $dbase . "GUI";

getDefaults();

$query  = "SELECT field FROM $table ORDER BY rank ASC";
$result = mysql_db_query($dbaseGUI, $query, $conn) or
          reAuthenticate("Cannot read from database. (Insert.)");

$i=0; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  if(!strcmp($row["field"], $tbId[$table]))
    continue;

  $flds[$i]=$row["field"];
  $vals[$i]=addSlashes($HTTP_POST_VARS[$flds[$i]]); 

  $i++;
}

$flds[$i]=$tbId[$table];
$vals[$i]=time() . getmypid();

$fields = implode($flds, ", ");
$values = "'" . implode("', '", $vals) . "'";

$query = "INSERT INTO $table ($fields) VALUES ($values)";
$result = mysql_db_query($dbase, $query, $conn) or
  reAuthenticate("Cannot write to database. (Insert.)");

header("Location: list.php3?dbase=$dbase&table=$table&time=" . time());
?>
******** END   Generic/doInsert.php3
******** BEGIN Generic/edit.php3
<?
require("common.php3");

$host  = "localhost";

if(!isset($dbase) || !isset($table)){
  $dbase = "YellowPages";
  $table = "books";
}

authenticate();
$user  = $PHP_AUTH_USER;
$pass  = $PHP_AUTH_PW;


$conn  = mysql_connect($host, $user, $pass) or
         reAuthenticate("Database logon incomplete. (Edit.)");

$dbaseGUI   = $dbase . "GUI";

getDefaults();
?>
<HTML>
<HEAD>
<TITLE>Neuer Eintrag (<? echo $tbTitles[$table]?>).</TITLE>
</HEAD>
<BODY bgColor=white>

<FORM action="doEdit.php3" method="post">
<INPUT type=hidden name=id value=<? echo $id    ?>>

<?
constructHiddenArgs();
echo $hiddenArgs;
?>

<TABLE bgColor=#faebd7 border=0 cellPadding=1 cellSpacing=2 width=100%
borderColor=lightseagreen>

<?
getGUI();

$query     = "SELECT $fields FROM $table WHERE id=$id";
$result    = mysql_db_query($dbase, $query, $conn) or
  reAuthenticate("Cannot read from database. (Edit.)");
$row       = mysql_fetch_row($result);

for($i=0; $i<$fieldCount; $i++){
  if($i==$idFld)
    continue;

  $value=stripslashes($row[$i]);
?>
<TR>
<TD VALIGN=TOP WIDTH=20% bgColor="#ffe4b5">
<? echo $fldTitle[$i] ?>
</TD>
<TD bgColor="#ffe4b5">
<?
  if(preg_match("/([a-zA-Z]+):([a-zA-Z]+)$/", 
     $fldMenu[$i], $parts))
    getMenu($flds[$i], $parts[1], $parts[2], $value);
  else if($fldTextarea[$i]){
?>
<TEXTAREA rows=5 cols=80 name=<? echo $flds[$i] ?>>
<? echo $value; ?>
</TEXTAREA>
<?
  }
  else{
?>
<INPUT type  =text size=80 
       name  =<? echo $flds[$i] ?>
       value =<? echo "\"$value\""  ?>>
<?
  }
?>
</TD>
</TR>
<?
}
?>

</TABLE>

<P>
<INPUT id=submit1 name=submit1 type=submit 
       value="Get Search Results">
</P>

</FORM>

<HR>

<FORM action="delete.php3" method="post">
<INPUT type=hidden name=id value=<? echo $id ?>>

<INPUT id=submit1 name=submit1 type=submit 
       value="Logon">

<? echo $hiddenArgs; ?>

</FORM>

</BODY>
</HTML>
******** END   Generic/edit.php3
******** BEGIN Generic/insert.php3
<?
require("common.php3");

$host  = "localhost";

if(!isset($dbase) || !isset($table)){
  $dbase = "YellowPages";
  $table = "books";
}

authenticate();
$user  = $PHP_AUTH_USER;
$pass  = $PHP_AUTH_PW;


$conn  = mysql_connect($host, $user, $pass)  or
         reAuthenticate("Database logon incomplete. (Insert.)");

$dbaseGUI   = $dbase . "GUI";

getDefaults();
?>
<HTML>
<HEAD>
<TITLE>Neuer Eintrag (<? echo $tbTitles[$table]?>).</TITLE>
</HEAD>
<BODY bgColor=white>

<FORM action="doInsert.php3" method="post">
<INPUT type=hidden name=dbase value=<? echo $dbase ?>>
<INPUT type=hidden name=table value=<? echo $table ?>>


<TABLE bgColor=#faebd7 border=0 cellPadding=1 cellSpacing=2 width=100%
borderColor=lightseagreen>

<?
getGUI();

for($i=0; $i<$fieldCount; $i++){
  if($i==$idFld)
    continue;

?>
<TR>
<TD VALIGN=TOP WIDTH=20% bgColor="#ffe4b5">
<? echo $fldTitle[$i] ?>
</TD>
<TD bgColor="#ffe4b5">
<?
  if(preg_match("/([a-zA-Z]+):([a-zA-Z]+)$/", 
     $fldMenu[$i], $parts))
    getMenu($flds[$i], $parts[1], $parts[2], "");
  else if($fldTextarea[$i]){
?>
<TEXTAREA rows=5 cols=80 name=<? echo $flds[$i] ?>>
</TEXTAREA>
<?
  }
  else{
?>
<INPUT type=text size=80 name=<? echo $flds[$i] ?>>
<?
  }
?>
</TD>
</TR>
<?
}
?>

</TABLE>

<P>
<INPUT id=submit1 name=submit1 type=submit 
       value="Get Search Results">
</P>

</FORM>

</BODY>
</HTML>
******** END   Generic/insert.php3
******** BEGIN Generic/list.php3
<?
require("common.php3");

$host  = "localhost";

if(!isset($dbase) || !isset($table)){
  $dbase = "YellowPages";
  $table = "books";
}

authenticate();
$user  = $PHP_AUTH_USER;
$pass  = $PHP_AUTH_PW;

$conn  = mysql_connect($host, $user, $pass) or
         reAuthenticate("Database logon incomplete.");

$result = mysql_listtables($dbase) or
          reAuthenticate
            ("Database logon incomplete. (List tables)."); 
$i = 0; 
while ($i < mysql_num_rows($result)) { 
  $tbNames[$i] = mysql_tablename($result, $i); 
  $i++; 
}
$tableCount = $i;

$dbaseGUI   = $dbase . "GUI";

getDefaults();
?>
<HTML>
<HEAD>
<TITLE>
Network Address Database: 
<? echo $tbTitles[$table] ?>
</TITLE>
</HEAD>
<BODY bgColor=white>

<TABLE WIDTH=100%>
<TR>
<?
for($i=0; $i<$tableCount; $i++){
  if(!strcmp($table, $tbNames[$i]))
    echo "<TD bgColor=#30FFFF><H1>\n";
  else
    echo "<TD><H1>\n";

  echo "<A HREF=list.php3?dbase=$dbase&table=$tbNames[$i]>\n";
  echo $tbTitles[$tbNames[$i]];
  echo "</A></H1></TD>\n";
}
?>
</TR>
</TABLE>
<?
getGUI();

if(!isset($sortBy))
  $sortByLocal  = "$flds[$idFld] DESC";
else
  $sortByLocal  = "$sortBy ASC";

if(!isset($search)){
  $where="";
  $searchArgs="";
}
else{
  $where="WHERE $toSearch LIKE '$search%'";
  $searchArgs="&toSearch=" . $toSearch .
              "&search="   . rawurlencode($search);
}

if(isset($search1)){
  if(!isset($search)){
    $where  = "WHERE $toSearch1 LIKE '$search1%'";
    $searchArgs="&toSearch1=" . $toSearch1 .
                "&search1="   . rawurlencode($search1);
  }
  else{
    if(!isset($operator)){
      $operator='or';
    }
    $searchArgs .= "&operator=$operator";
    $where .= " $operator $toSearch1 LIKE '$search1%'";
    $searchArgs.="&toSearch1=" . $toSearch1 .
                 "&search1="   . rawurlencode($search1);
  }
}
if(isset($sortBy))
  $searchArgs .= "&sortBy=$sortBy";


for($ind=0; $ind<26; $ind++){
  echo "<A HREF=list.php3?time=" . time() .
       "&dbase=$dbase&table=$table" .
       "&sortBy=$tbSearch[$table]"  .
       "&search="                     
       . chr(Ord("A")+$ind) . 
       "&toSearch=$tbSearch[$table]>" 
       . chr(Ord("A")+$ind) . "</A>\n";
}

?>

&nbsp;&nbsp;&nbsp;
<? echo "<A HREF=list.php3?time=" . time() .
                          "&dbase=$dbase&table=$table>" ?>
Alle</A>
<P>

<FORM action="insert.php3" method="post">
<INPUT type=hidden name=dbase value=<? echo $dbase ?>>
<INPUT type=hidden name=table value=<? echo $table ?>>

<INPUT id=submit1 name=submit1 type=submit 
       value="New Entry">
</FORM>

<HR>

<FORM action="list.php3" method="post">

<INPUT type=hidden name=dbase value=<? echo $dbase ?>>
<INPUT type=hidden name=table value=<? echo $table ?>>

<INPUT type=hidden name=time value=<? time() ?>>
<INPUT type=hidden name=sortBy 
       value=<? if(!isset($sortBy)) echo $tbSearch[$table]; 
                else echo $sortBy;?>>

<INPUT name=search value=<? echo "\"$search\""; ?>>

<SELECT NAME=toSearch>
<?
for($ind=0, $echoed=0; $ind<$fieldCount; $ind++){
  if($ind==$idFld) continue;

  $echoed++;
  echo "<OPTION ";
  if(($toSearch=="" && $echoed==1) || 
    !strcmp($toSearch, $flds[$ind])){
    echo "SELECTED";
  }
  echo " VALUE=$flds[$ind]>$fldTitle[$ind]\n";
}
?>
</SELECT>
<BR>
<?
if($operator=="" || !strcmp($operator, "and"))
  $opind=0;
else
  $opind=1;

$opsel=array("SELECTED", "");
?>
<SELECT NAME=operator>
<OPTION <? echo $opsel[$opind]; ?> VALUE="and"> Und
<OPTION <? echo $opsel[1-$opind]; ?> VALUE="or"> Oder
</SELECT>

<BR>

<INPUT name=search1 value=<? echo "\"$search1\""; ?>>

<SELECT NAME=toSearch1>
<?
for($ind=0, $echoed=0; $ind<$fieldCount; $ind++){
  if($ind==$idFld) continue;

  $echoed++;
  echo "<OPTION ";
  if(($toSearch1=="" && $echoed==2) || 
    !strcmp($toSearch1, $flds[$ind])){
    echo "SELECTED";
  }
  echo " VALUE=$flds[$ind]>$fldTitle[$ind]\n";
}
?>
</SELECT>

<BR>

<INPUT id=submit1 name=submit1 type=submit 
       value="Searches">
&nbsp;&nbsp;&nbsp; 
</FORM>

<?
$query  = "SELECT "
         ."source, dest, sourcefields, destfields "
         ."FROM lookups";
$result = mysql_db_query($dbaseGUI, $query, $conn);
$i=0; while ($row = mysql_fetch_row($result)){
  if(!strcmp($row[0], $table)){
    if(preg_match("/^([a-zA-Z]+)$/", $row[2], $parts) ||
       preg_match("/:([a-zA-Z]+)$/", $row[2], $parts)){
      $ind=$hashToIndex[$parts[1]];

      $fldLookup[$ind]=1;
      $fldLookupDest[$ind]=$row[1];
      $fldLookupSourceFields[$ind]=$row[2];
      $fldLookupDestFields[$ind]=$row[3];
    }
  }

  $i++;
}
?>
<TABLE WIDTH=100% BGCOLOR=#faebd7 BORDER=1 CELLSPACING=2>
<?
echo "<TR>\n";
for($i=0; $i<$fieldCount; $i++){
  if($fldBrowse[$i])
    echo "<TD><A HREF=list.php3?"
                    ."dbase=$dbase&table=$table&"
                    ."sortBy=$flds[$i]\n"
            ."</A>$fldTitle[$i]</TD>\n";
}
echo "</TR>\n";


$query  = "SELECT $fields FROM $table $where ORDER BY $sortByLocal";
$result = mysql_db_query($dbase, $query, $conn);
while ($escapedRow = mysql_fetch_row($result)){
  for($i=0; $i<$fieldCount; $i++)
    $row[$i]=stripslashes($escapedRow[$i]);

  echo "<TR>\n";

  for($i=0; $i<$fieldCount; $i++)
    if($fldBrowse[$i]){
      if(!strcmp($row[$i], "") || 
          preg_match("/^\s+$/", $row[$i]))
    	echo "<TD> &nbsp; </TD>\n";
      else if(!strcmp($fldAction[$i], 'view') ||
              !strcmp($fldAction[$i], 'edit'))
        echo "<TD><A HREF=$fldAction[$i].php3?"
                        ."dbase=$dbase&table=$table&"
                        ."id=$row[$idFld]$searchArgs>\n"
                        ."$row[$i]</A></TD>\n";
      else if(!strcmp($fldAction[$i], 'mail'))
        echo "<TD><A HREF=mailto:$row[$i]>"
                        ."$row[$i]</A></TD>\n";
      else if($fldLookup[$i]){
        echo "<TD><A HREF=list.php3?dbase=$dbase"
                                 ."&table=$fldLookupDest[$i]";
	if(preg_match("/^([a-zA-Z]+):([a-zA-Z]+)$/", 
                      $fldLookupSourceFields[$i], $sourceParts) &&
           preg_match("/^([a-zA-Z]+):([a-zA-Z]+)$/", 
                      $fldLookupDestFields[$i], $destParts))
          echo "&toSearch=$destParts[1]"
              ."&search=" . 
                rawurlencode($row[$hashToIndex[$sourceParts[1]]])
              ."&operator=and"
              ."&toSearch1=$destParts[2]"
              ."&search1=" . 
                rawurlencode($row[$hashToIndex[$sourceParts[2]]]);
	else
          echo "&toSearch=$fldLookupDestFields[$i]"
              ."&search=" . 
                rawurlencode
                  ($row[$hashToIndex[$fldLookupSourceFields[$i]]]);

        echo ">$row[$i]</A></TD>\n";
      }
      else
    	echo "<TD> $row[$i] </TD>\n";
    }

  echo "</TR>\n";
}

mysql_close($conn);
?>
</TABLE>
</BODY>
</HTML>
******** END   Generic/list.php3
******** BEGIN Generic/view.php3
<?
require("common.php3");

$host  = "localhost";

if(!isset($dbase) || !isset($table)){
  $dbase = "YellowPages";
  $table = "books";
}

authenticate();
$user  = $PHP_AUTH_USER;
$pass  = $PHP_AUTH_PW;

$conn  = mysql_connect($host, $user, $pass) or
         reAuthenticate("Database logon incomplete. (View).");

$dbaseGUI   = $dbase . "GUI";

getDefaults();
?>
<HTML>
<HEAD>
<TITLE> Eintrag anzeigen (<? echo $tbTitles[$table]?>).</TITLE>
</HEAD>
<BODY bgColor=white>

<TABLE bgColor=#faebd7 
       border=0 cellPadding=1 cellSpacing=2 width=100%
       borderColor=lightseagreen>

<?
getGUI();

$query  = "SELECT $fields FROM $table WHERE " . $tbId[$table] . "=$id";
$result = mysql_db_query($dbase, $query, $conn) or
          reAuthenticate("Cannot read from database. (View).");
$escapedRow = mysql_fetch_row($result);
for($i=0; $i<$fieldCount; $i++){
  if($i==$idFld) continue;

  $value=stripslashes($escapedRow[$i]);
  if(!strcmp($value, "") || preg_match("/^\s+$/", $value))
    $value = "&nbsp; ";
?>
<TR>
<TD VALIGN=TOP WIDTH=20% bgColor="#ffe4b5">
<? echo $fldTitle[$i] ?>
</TD>
<TD bgColor="#ffe4b5">
<? echo $value ?>
</TD>
</TR>
<?
}
?>

</TABLE>

<P>
<FORM action="list.php3" method="post">

<?
constructHiddenArgs();
echo $hiddenArgs;
?>

<INPUT id=submit1 name=submit1 type=submit value="Continue">
</FORM>
</P>

</BODY>
</HTML>
******** END   Generic/view.php3