Home

stringsum.php (2.31kB)


<?php

if (!$_REQUEST['a']) {

     require($_SERVER['DOCUMENT_ROOT']."/ui/engine.php");

     $GLOBALS['pgtitle'] = "String Sum";

     $GLOBALS['subtitle'] = "<a href=\"viewsrc.php?filename=stringsum.php\">View Source</a>";

     $GLOBALS['bodyarg'] = " onload=\"document.getElementById('thestring').focus()\"";

     $GLOBALS['js'] = "

           function createRequestObject() {  //Shamelessly copied and modified from http://mikeoncode.blogspot.com/2006/02/ajax-project-to-get-you-going.html

                 var ro;

                 var browser = navigator.appName;

                 if(browser == \"Microsoft Internet Explorer\"){

                       ro = new ActiveXObject(\"Microsoft.XMLHTTP\");

                 }

                 else{

                       ro = new XMLHttpRequest();

                 }

                 return ro;

           }

                 

           var http = createRequestObject();

                 

           function sndReq() {

                 document.getElementById('output').innerHTML = '<img src=\"working.gif\" alt=\"Working, please wait...\" />';

                 http.open('GET', 'stringsum.php?a=strsubmit&thestring='+document.getElementById('thestring').value);

                 http.onreadystatechange = handleResponse;

                 http.send(null);

           }

                 

           function handleResponse() {

                 if(http.readyState == 4){

                       var response = http.responseText;

                       document.getElementById('output').innerHTML = response;

                 }

           }";

     printheader();

     ds("Introduction", "Please see my <a href=\"http://blog.jwcxz.com/?p=48\">blog post</a> on checksumming as a method of memorization.");

     ds("Configuration", "</p>

           <form action=\"javascript:sndReq()\" name=\"strsum\">

                 <p>String: <input type=\"text\" name=\"thestring\" id=\"thestring\" value=\"\" style=\"width: 95%\" /></p>

                 <p><input type=\"submit\" name=\"submit\" value=\"Find Sum!\" /></p>

           </form><p>");

     

     ds("Output", "<div style=\"width: 80%; background-color: #CCFFCC; text-align: center; padding: 30px; margin: 0px auto 0px\" id=\"output\">&nbsp;</div>");

     printfooter();

}

elseif($_REQUEST['a']=="strsubmit") {

     $letters = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10, 'k' => 11, 'l' => 12, 'm' => 13, 'n' => 14, 'o' => 15, 'p' => 16, 'q' => 17, 'r' => 18, 's' => 19, 't' => 20, 'u' => 21, 'v' => 22, 'w' => 23, 'x' => 24, 'y' => 25, 'z' => 26 );

     $str = $_REQUEST['thestring'];

     $stra = str_split($str);

     foreach ($stra as $k) {

           $sum += $letters[strtolower($k)];

     }

     echo "{$str}: {$sum}";

}