<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
  <title>Manage How-To Database</title>
  <LINK REL="stylesheet" HREF="/PHP/style.css">
  <META NAME="author"      CONTENT="Denice Deatrich">
  <META NAME="description" CONTENT="PHP web-based MySQL DB Management">
  <META NAME="keywords"    CONTENT="PHP web MySQL database">
</head>

<body>
  <h1 align="center">Manage How-To Database
  </h1>
<?php
  define
("STEP_FACTOR", 10 );
  global
$PHP_SELF;

  if( !
file_exists("db_connect.php") ) {
    echo
"<em><font color=\"red\">
     Error: Database connect parameters not available. </font></em><br><br>"
;
     exit;
  }
  require(
"db_connect.php" );

  
/** initialize these vars: **/
  
$sortkey = "";
  
$searchkey = "";
  
$nextrow = 0;

  
checkDBStatus( );
  if(
is_array($HTTP_POST_VARS) ) {
    
//  then SELF called with sort, next or search argument(s).
    
$i = 0;
    while( list(
$key, $val) = each($HTTP_POST_VARS) ) {
      
// process each POST variable argument
      
if( $key == "sort" ) {
        
$sortkey = $val;
        
//break;
      
}
      if(
$val == "next" ) {
        
$nextrow = $key;
      }
      if(
$key == "search" && strlen($val) > 0 ) {
        
$searchkey = $val;
      }
      
$i++;
      
//echo "DBG: key:val is " . $key.":".$val . "<BR>";
    
}
  }
  if( (
$link=mysql_pconnect($DB_HOST, $DB_USER, $DB_PW)) ) {
    
$q = "select * from how_to"; // start to build query string
    
if( strlen($searchkey) > 0 ) {
      
// then add a keyword search clause
      
$q = $q . " where keywords like \"%" . $searchkey ."%\"";
    }
    if(
strlen($sortkey) > 0 ) {
      
// then add the required sort specification
      
$q = $q . " order by " . $sortkey;
    }
    
// now build the HTML forms
    
echo "<table width=590 border=0 cols=3>
      <!-- First form allows a global view of all the data           -->
      <form target=\"_blank\" action=\"db_all.php\" method=\"post\">
      <tr>
      <td width=80><font size=-1>
      <input type=\"submit\" name=\"all\" value=\"Show All Data...\"></font>
      </td>
      <INPUT TYPE=\"hidden\" name=\"sort\" value=\"$sortkey\">
      <INPUT TYPE=\"hidden\" name=\"search\" value=\"$searchkey\">
      </form>"
;
      echo
"
      <!-- Second form contains the search field                     -->
      <form action=\"$PHP_SELF\" method=\"post\">
      <td nowrap align=\"center\"><font size=-1>Search: <font size=-1>
      <input type=\"input\" size=30 name=\"search\">
      </font> (search in keywords)</font></td>
      <td width=50><A TARGET=\"help\" HREF=\"help_db.html\">
      <img src=\"/question.gif\" align=\"right\" alt=\"(Help)\"></a></td>
      </form>
    </table>"
;
    echo
"
    <!-- Third form is the header row with embedded sort buttons     -->
    <form action=\"$PHP_SELF\" method=\"post\">
      <table border=1 cols=6>
      <tr bgcolor=\"#e1ffd8\">
      <th width=150>Action</th>
      <th width=180><input type=\"submit\" name=\"sort\" value=\"topic\"></th>
      <th width=50><input type=\"submit\" name=\"sort\" value=\"os\"></th>
      <th width=40><input type=\"submit\" name=\"sort\" value=\"ver\"></th>
      <th width=50><input type=\"submit\" name=\"sort\" value=\"arch\"></th>
      <th width=80>Keywords</th>
      </tr>
      </table>
    </form>"
;

    
/** okay, do the query. **/
    
if( $res=mysql_db_query($DB_NAME, $q, $link) ) {
      
$nrows = mysql_num_rows($res);
      
$currow = 0;
      
$n = 0;
      
$arr[0] = "";
      if(
$nextrow > 0 ) {
        
// then the user got here with the 'next' button
        
$currow = $nextrow;
        
$nextrow = $nextrow + STEP_FACTOR;
        
// flip thru rows til we get to relevant rows
        
while( ($n < $currow) && ($row=mysql_fetch_array($res, MYSQL_NUM)) ) {
          
$n++;
        }
        
$n = 0;
      }
      else {
        
$nextrow = STEP_FACTOR;
      }
      
/** fetch rows into array; we will display them in the 2nd form **/
      
while( ($n< STEP_FACTOR) && ($row=mysql_fetch_array($res, MYSQL_NUM)) ) {
        
$arr[$n] = $row;
        
$n++;
      }
      
// for($i = 0; $i < $n; $i++) {echo $i. ": " .$arr[$i][0]. "\n<BR>";}
      
mysql_free_result( $res );
    }
    else {
      
printError( "QUERY", $link );
      exit;
    }
  }
  else {
    echo
mysql_errno().": ".mysql_error()."<BR>";
    exit;
  }
  echo
"
  <!-- This form contains the rows of data, each with a 'go' button -->
  <!-- The 'go' button is used to work on a single individual row   -->
  <form target=\"_blank\"
   action=\"/PHP/db/db_process.php\" method=\"post\">
  <table border=1 cols=6>"
;
  
// data rows follow
  
if( $n <= 0 ) {
    echo
"<em><font color=\"red\">No more rows or no qualifying rows
    </font></em><br><br>"
;
  }
  else {
    for(
$i = 0; $i < $n; $i++ ) {
      echo
"<tr>
      <td width=150 nowrap bgcolor=\"OldLace\">
       <font size=-1><select name=\"option-$i\">
      <option value=\"none\">-
      <option value=\"notes\">Notes
      <option value=\"modify\">Modify
      <option value=\"delete\">Delete
      </select>"
;
      
printf( "%3s", $arr[$i][0] );
      
$rowid = $arr[$i][0];
      echo
"<input type=\"submit\" name=\"$rowid\" value=\"Go\">
      </font></td>"
;
      echo
"<td width=180 nowrap bgcolor=\"AliceBlue\"><font size=-1>"
       
. $arr[$i][1] . "</font></td>\n";
      echo
"<td width=50 bgcolor=\"lavender\"><font size=-1>" . $arr[$i][2]
       .
"</font></td>\n";
      echo
"<td width=40 bgcolor=\"MintCream\"><font size=-1>" . $arr[$i][3]
       .
"</font></td>\n";
      echo
"<td width=50 bgcolor=\"MistyRose\"><font size=-1>" . $arr[$i][4]
       .
"</font></td>\n";
      echo
"<td width=80 bgcolor=\"silver\"><font size=-1>" . $arr[$i][5]
       .
"</font></td>\n";
//         echo "<td>" . substr($arr[$i][6], 0, 10) . "</td></tr>\n";
    
}
  }
  echo
"</table>
  <!-- Finish the form with 2 more buttons: 'reset' and 'insert' -->
  <INPUT TYPE=\"reset\" VALUE=\"Reset Form\">
  <INPUT TYPE=\"submit\" name=\"insert\" VALUE=\"Insert New Data...\">
  </form>"
;
  if( (
$nextrow ) < $nrows ) {
    echo
"
    <!-- Last form! next button for browsing STEP_FACTOR rows at a time -->
    <!-- and we need to remember sort & search keys for subsequent pages -->
    <form action=\"$PHP_SELF\" method=\"post\">
    <INPUT TYPE=\"submit\" name=\"$nextrow\" value=\"next\"> "
;
    echo
"&gt;&gt; " . STEP_FACTOR .
     
" rows <INPUT TYPE=\"hidden\" name=\"sort\" value=\"$sortkey\">
    <INPUT TYPE=\"hidden\" name=\"search\" value=\"$searchkey\">
    </form>"
;
  }
?>
  <hr>
  <div class="footer">
  <address>
    <a href="mailto&#058;Denice&#046;Deatrich&#064;epfl.ch">Denice Deatrich</a>
  </address>
<!-- Created: Sun Jan 16 17:22:34 MET 2000 -->
<!-- hhmts start -->
Last modified: Tue Jun  6 23:48:30 MEST 2000
<!-- hhmts end -->
  </div>
</body>
</html>