GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Scruff the Cat       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


CRUD-functions.php

    
<?php

/*

@package CRUD
  @version 1.0

Plugin URL: http://davin.50webs.com/research/2014/aoawf.html
  Description: Miscellaneous functions for the CRUD wordpress plugin.
  Author: Davin Pearson
  Author URL: <http://davin.50webs.com>

LIMITATION OF WARRANTY:

This file 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, or (at your option)
  any later version.

This file 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 for more details.

You should have received a copy of the GNU General Public License
 along with this software, see the file COPYING.  If not, see:

<http://www.gnu.org/licenses/gpl-3.0.txt>.

*/

function array_length($array)
{
  if (!is_array($array))
  {
    if (is_string($array))
    {
      echo h1("Arg to array_length is a string=$array");
      return 0;
      //assert(false);
    }
    else
    {
      echo h1("arg to array_length not an array");
      assert(false);
    }
    exit();
  }
  $len = 0;
  if (!empty($array))
  {
    foreach ($array as $a)
    {
      $len++;
    }
  }
  return $len;
}

function array_print($array)
{
  echo "BEG printing array<br>\n";
  $i = 0;
  foreach ($array as $a)
  {
    echo "line=$i: $a . <br>";
    $i++;
  }
  echo "END printing array<br>\n";
}

/**
* My version of the built-in substr function
*/
function mysubstr($string,$from,$to)
{
  $result = "";
  for ($i = $from; $i < $to; $i++)
  {
    $result .= $string[$i];
  }
  return $result;
}

/**
* My version of the built-in strpos function
*/
function mystrpos($string,$contains)
{
  for ($i = 0; $i < strlen($string); $i++)
  {
    if ($string[$i] == $contains)
    {
      //echo h1("Found");
      //xit();
      return $i;
    }
  }
  //exit();
  //echo h1("Not found");
  return -1;
}

function pre($arg)
{
  return "<pre>$arg</pre>\n";
}

function h1($title)
{
  return "<h1><span class=bold>$title</span></h1>\n";
}

function h1error($title)
{
  return "<span class=\"error\">$title</span><br><br>\n";
}

function h1ok($title)
{
  return "<span class=\"ok\">$title</span><br><br>\n";
}

function h1debug($title)
{
  return "<span class=\"debug\">$title</span><br><br>\n";
}

function readCSV($csvFile)
{
  //echo "<h1>BEG Foo</h1>\n";
  $file_handle = null;
  if (file_exists($csvFile))
  {
    $file_handle = fopen($csvFile, 'r');
  }
  if ($file_handle == null)
  {
    echo "<h1>File not found: $csvFile</h1>\n";
    exit();
  }
  //echo "<h1>file_handle=" . $file_handle . "</h1>\n";
  while (!feof($file_handle))
  {
    $line_of_text[] = fgetcsv($file_handle, 1024);
  }
  fclose($file_handle);
  //echo "<h1>END Foo</h1>";
  return $line_of_text;
}

function DD_MM_YYYY_to_YYYY_MM_DD($arg)
{
   assert(is_string($arg));
   if (substr($arg,2,1) != "/")
   {
      echo h1("substr($arg,2,1)=" . substr($arg,2,1));
      //assert false;
   }
   if (substr($arg,5,1) != "/")
   {
      echo h1("substr($arg,5,1)=" . substr($arg,5,1));
      //assert false;
   }
   $yyyy = substr($arg,6,4);
   $mm   = substr($arg,3,2);
   $dd   = substr($arg,0,2);
   //echo h1("yyyy=$yyyy");
   //echo h1("mm=$mm");
   //echo h1("dd=$dd");
   return $yyyy . "-" . $mm . "-" . $dd;
}

function slashes_to_minus_date($arg)
{
   //echo h1("BEGIN Arg to slashes_to_minus_date = $arg");
   if ($arg != "")
   {
      assert(my_is_string($arg));
      if (substr($arg,4,1) != "/")
      {
         echo h1("substr($arg,2,1)=" . substr($arg,4,1));
         assert(false);
      }
      if (substr($arg,7,1) != "/")
      {
         echo h1("substr($arg,5,1)=" . substr($arg,7,1));
         assert(false);
      }
      /*
2014/06/17
      */
      $yyyy = substr($arg,6,4);
      $mm   = substr($arg,3,2);
      $dd   = substr($arg,0,2);
      //echo h1("END Arg to slashes_to_minus_date = $arg");
      return $yyyy . "-" . $mm . "-" . $dd;
   }
   //echo h1("END Arg to slashes_to_minus_date = $arg");
   return "";
}

function slashes_to_minus_datetime($arg)
{
   //echo h1("BEGIN Arg to slashes_to_minus_datetime=$arg");
   //echo "len(arg)=" . strlen($arg) . "<br>\n";
   if ($arg != "")
   {
      assert(my_is_string($arg));
      if ((substr($arg,4,1) != "/") and (substr($arg,4,1) != "-"))
      {
         echo h1("substr($arg,4,1)=" . substr($arg,4,1));
         assert(false);
      }
      if ((substr($arg,7,1) != "/") and (substr($arg,7,1) != "-"))
      {
         echo h1("substr($arg,7,1)=" . substr($arg,7,1));
         assert(false);
      }
      if (substr($arg,10,1) != " ")
      {
         echo h1("substr($arg,10,1)=" . substr($arg,10,1));
         assert(false);
      }
      if (substr($arg,13,1) != ":")
      {
         echo h1("substr($arg,13,1)=" . substr($arg,13,1));
         assert(false);
      }
      if (substr($arg,16,1) != ":")
      {
         echo h1("substr($arg,16,1)=" . substr($arg,16,1));
         assert(false);
      }
      /*
2014/06/17 20:30:00
      */
      $yyyy = substr($arg, 0,4);
      $mm   = substr($arg, 5,2);
      $dd   = substr($arg, 8,2);
      $hh   = substr($arg,11,2);
      $MM   = substr($arg,14,2);
      $ss   = substr($arg,17,2);
      //echo h1("END Arg to slashes_to_minus_datetime=$arg");
      return $yyyy . "-" . $mm . "-" . $dd . " " . $hh . ":" . $MM . ":" . $ss;
   }
   //echo h1("END Arg to slashes_to_minus_datetime=$arg");
   return "";
}

function my_is_string($candidate)
{
   return $candidate == "" or is_string($candidate);
}

//echo pre(htmlspecialchars($hidden_search_fields, ENT_QUOTES, 'UTF-8'));
//echo pre(htmlspecialchars($href_search_fields, ENT_QUOTES, 'UTF-8'));

function calculate_hidden_fields()
{
   global $db_fields;
   $result = "";
   for ($i = 0; $i < array_length($db_fields); $i++)
   {
      if ($db_fields[$i][TYPE] == "date")
      {
         $result .= "  <input type=\"hidden\" name=\"search_" . $db_fields[$i][PHP_NAME] . "_min\" value=\"" . $db_fields[$i][SEARCH_FIELDS][0] . "\">\n";
         $result .= "  <input type=\"hidden\" name=\"search_" . $db_fields[$i][PHP_NAME] . "_max\" value=\"" . $db_fields[$i][SEARCH_FIELDS][1] . "\">\n";
      }
      elseif ($db_fields[$i][TYPE] == "time")
      {
         $result .= "  <input type=\"hidden\" name=\"search_" . $db_fields[$i][PHP_NAME] . "_min\" value=\"" . $db_fields[$i][SEARCH_FIELDS][0] . "\">\n";
         $result .= "  <input type=\"hidden\" name=\"search_" . $db_fields[$i][PHP_NAME] . "_max\" value=\"" . $db_fields[$i][SEARCH_FIELDS][1] . "\">\n";
      }
      elseif ($db_fields[$i][TYPE] == "datetime")
      {
         $result .= "  <input type=\"hidden\" name=\"search_" . $db_fields[$i][PHP_NAME] . "_min\" value=\"" . $db_fields[$i][SEARCH_FIELDS][0] . "\">\n";
         $result .= "  <input type=\"hidden\" name=\"search_" . $db_fields[$i][PHP_NAME] . "_max\" value=\"" . $db_fields[$i][SEARCH_FIELDS][1] . "\">\n";
      }
      else
      {
         $result .= "  <input type=\"hidden\" name=\"search_" . $db_fields[$i][PHP_NAME] . "\" value=\"" . $db_fields[$i][SEARCH_FIELDS] . "\">\n";
      }
   }
   return $result;
}

function calculate_click_here_to_continue()
{
   global $db_fields,$page_id,$offset,$limit,$order_by,$asc,$like,$username,$password;
   $c = "<a href=\"?page_id=$page_id&action=browser&offset=$offset&limit=$limit&order-by=$order_by&asc=$asc&like=$like&username=$username&encoded=1&password=$password";
   for ($i = 0; $i < array_length($db_fields); $i++)
   {
      $s = $db_fields[$i][SEARCH_FIELDS];
      if (my_is_string($s))
      {
         $c .= "&search_" . $db_fields[$i][PHP_NAME] . "=" . $s;
         //"search_id=$search_id&search_name=$search_name&search_url-yellow=$search_url_yellow&search_phone=$search_phone&search_url-address=$search_url_address&search_email=$search_email&search_address=$search_address&search_categories=$search_categories"
      }
      else
      {
         $c .= "&search_" . $db_fields[$i][PHP_NAME] . "_min=" . $db_fields[$i][SEARCH_FIELDS][0];
         $c .= "&search_" . $db_fields[$i][PHP_NAME] . "_max=" . $db_fields[$i][SEARCH_FIELDS][1];
      }
   }
   // rand() function ensures the page is not cached
   $c .= "&r=" . rand() . "#start-here\">Click here to just return to the client browser</a>";
   $c = h1($c);
   return $c;
}

function get_create_table_query()
{
   global $db_fields, $db_database;
   //echo "<pre>\n";
   $query = "";

//$query .= "CREATE DATABASE $db_database;\n\n";
   $query .= "USE $db_database;\n\n";
   $query .= "DROP TABLE clients_table;\n\n";
   $query .= "CREATE TABLE clients_table (\n";
   for ($i = 0; $i<array_length($db_fields); $i++)
   {
      if ($i != 0)
      {
         $query .= ", ";
      }
      $query .= $db_fields[$i][PHP_NAME] . " " . $db_fields[$i][MYSQL_INIT] . "\n";
   }
   $query .= ") DEFAULT CHARACTER SET utf8;\n\n";
   return $query;
}

function get_insert_table_query()
{
   global $db_hostname, $db_username, $db_password, $db_database;
   global $db_fields;
   $link = mysqli_connect($db_hostname,$db_username,$db_password);
   if (!$link)
   {
      echo h1error('Unable to connect to the database server');
      exit();
   }

if (!mysqli_set_charset($link, 'utf8'))
   {
      echo h1error('Unable to set database connection encoding.');
      exit();
   }

$here_is_the_sql_query = "<span class=\"big-black\">Here is the SQL query:</span><br>\n";
   $query = "USE $db_database;\n\n";
   $result = mysqli_query($link,$query);
   if (!$result)
   {
      echo h1error("USE query failed");
      echo $here_is_the_sql_query;
      echo pre($query);
      echo calculate_click_here_to_continue();
      exit();
   }
   $query = "SELECT ";
   for ($i=0; $i<array_length($db_fields); $i++)
   {
      if ($i > 0)
      {
         $query .= ",";
      }
      $query .= $db_fields[$i][PHP_NAME];
   }
   $query .= " FROM clients_table;\n";
   //$query = "SELECT id,name,url_yellow,phone,url_address,email,address,categories,time_fixed FROM clients_table;\n";

   $result = mysqli_query($link,$query);
   if (!$result)
   {
      echo h1error("SELECT query failed:");
      echo $here_is_the_sql_query;
      echo pre($query);
      echo calculate_click_here_to_continue();
      exit();
   }
   //echo "\n";
   $query = "INSERT INTO clients_table (";
   for ($i = 0; $i<array_length($db_fields); $i++)
   {
      if ($i != 0)
      {
         $query .= ", ";
      }
      $query .= $db_fields[$i][PHP_NAME];
   }
   $query .= ") VALUES\n";
   //$query .= "(";
   $nrows  = 0;
   //echo h1("About to do x loop");
   while ($x = mysqli_fetch_array($result))
   {
      //echo h1("nrows=$nrows");
      if ($nrows != 0)
      {
         $query .= ",";
      }
      $query .= "(";
      for ($i = 0; $i<array_length($db_fields); $i++)
      {
         if ($i != 0)
         {
            $query .= ",";
         }
         $query .= "'" . escape_quotes($x[$i]) . "'";
      }
      $query .= ")\n";
      $nrows++;
   }
   if ($nrows == 0)
   {
      $query = "";
   }
   else
   {
      $query .= ";\n";
   }
   return $query;
}

function my_explode($str)
{
   $len = strlen($str);
   $newarray = array();
   $newstr = "";
   for ($i = 0; $i<$len; $i++)
   {
      $ch = $str[$i];
      if ($ch == '\'')
      {
         $newstr .= $ch;
         $i++;
         while (($ch = $str[$i]) != '\'')
         {
            if ($ch == '\\')
            {
               $i++;
               $ch = $str[$i];
               $newstr .= "\\" . $ch;
               $i++;
               $ch = $str[$ch];
            }
            else
            {
               $newstr .= $ch;
               $i++;
            }
         }
         assert($ch == '\'');
         $newstr .= $ch;
      }
      elseif ($ch == ';')
      {
         $newarray[] = $newstr;
         $newstr = "";
      }
      else
      {
         $newstr .= $ch;
      }
   }
   return $newarray;
}

function escape_quotes($str)
{
   assert(my_is_string($str));
   $len = strlen($str);
   for ($i=0; $i<$len; $i++)
   {
      $ch = $str[$i];
      if ($ch == '\'')
      {
         $result .= "\\'";
      }
      else
      {
         $result .= $ch;
      }
   }
   return $result;
}

?>
Back
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sun Sep 25 16:11:10 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to