<?php
 $retval = "<option selected='selected' value=''>Tutorials ...</option>";
 /*** mysql hostname ***/
 $hostname = 'localhost'; 

 /*** mysql username ***/
 $username = 'username';

 /*** mysql ***/
 $password = 'password';

 $dbname = 'wordpress_database_name';
 if (isset($_GET['dbname'])) $dbname = $_GET['dbname'];
 $tname = 'posts_table_name';

 $link = mysql_connect($hostname, $username, $password);
 if (!$link) {
    die('Could not connect: ' . mysql_error());
 }
 mysql_select_db($dbname);
 // <option id='MyNew-20140327' onclick='windowopen("http://www.rjmprogramming.com.au/wordpress/?p=6976","_blank"); ' value='http://www.rjmprogramming.com.au/wordpress/?p=6976'>Linux Monitor Processes Primer Tutorial (20140327)</option>
 $res = mysql_query("SELECT  ID, post_date, post_title, guid, DATE_FORMAT(post_date,'%Y%m%d') As pdate FROM " . $tname . " WHERE post_status='publish' ORDER BY post_date DESC");
 if ($res == 0) {
  echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
 } else if (mysql_num_rows($res) == 0) {
  echo("<b>Query executed successfully</b>");
 } else {
  $j = 1;
  while (($r_array = mysql_fetch_row($res))) {
    $retval .= "\n<option id='MyNew-" . $r_array[4] . "' onclick='windowopen(\"" . $r_array[3] . "\",\"_blank\"); ' value='" . $r_array[3] . "'>" . $r_array[2] . " (" . $r_array[4] . ")</option>";
    if (($j % 19) == 0) {
      $retval .= "\n<option value=''>Tutorials ...</option>";
    }
    $j++;
  }
 }
 mysql_close($link);
 $newhtml = $retval;
 if (file_exists(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html")) {
   if (!file_exists(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html_original")) {
     copy(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html",str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html_original"); 
   }
   $whtml = str_replace("<select", "~",  str_replace("</select", "~", file_get_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html")));
   $earr = explode("~", $whtml);
   if (count($earr) == 5) {
    $selarr = explode(">", $earr[3]);
    $newhtml = $earr[0] . "<select" . $earr[1] . "</select" . $earr[2] . "<select" . $selarr[0] . ">" . $retval . "</select" . $earr[4];
    file_put_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html", $newhtml);
    // chown(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html", "normaluser"); // may need this
     if (file_exists(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html")) {
   if (!file_exists(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html_original")) {
     copy(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html",str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html_original"); 
   }
   $whtml = str_replace("<select", "~",  str_replace("</select", "~", file_get_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html")));
   $earr = explode("~", $whtml);
   if (count($earr) == 5) {
    $selarr = explode(">", $earr[3]);
    $newhtml = $earr[0] . "<select" . $earr[1] . "</select" . $earr[2] . "<select" . $selarr[0] . ">" . $retval . "</select" . $earr[4];
    file_put_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html", $newhtml);
    // chown(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html", "normaluser");  // may need this
   } else {
    $newhtml = $retval;
   }
   } 
   } else {
    $newhtml = $retval;
   }
 } 
 file_put_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/tutorial_options.html", $newhtml);
 echo $retval;
?>

