Category: Programming


Php function to load table into a hash

February 24th, 2021 — 10:51am

Here’s a function I use to load mysql data into a hash for quick lookups in my code:

function select_sql_hash ($link,$sql,&$all_hash,$key) {
  $r = mysqli_query($link,$sql);
  if (!$r) {
   $message  = 'Invalid query: ' . mysqli_error() . "\n";
   $message .= 'Whole query: ' . $sql;
   print "$message\n";
        exit;
  }
  while ($results = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        $all_hash[$results[$key]] = $results;
  }
  mysqli_free_result($r);
}
This can be called something like this:
$topics = array();
select_sql_hash($db,"select topicid,name,created_date from topic",$topics,"topicid");
The results is a hashed array where $topic[5][‘name’] is the “name” field for that topic, and $topic[5][‘created_date’] is the date created, etc. This is handy if the code is doing some mapping to print out topic names and you’d like to avoid joining the tables. Or it may save additional queries to the database when displaying data.

Comments Off on Php function to load table into a hash | php

Simply Weather Progressive Web App

April 18th, 2017 — 3:51pm

I put together a relatively simple “Progressive Web App” called SimplyWeather.  It uses a weather feed to create a simple forecast for your current location.  A Progressive Web App is supposed to act like a natively installed app, but written using web technologies.  If you load the above link on your phone, it should ask you to install to your home screen, at which point you can run it from there (it’ll look like a local app).

Overall, building a PWA is a bit of a PITA, with some very finicky and temperamental requirements.  I found the “service worker” and “manifest” particularly feisty.  Also, I’m not sure how to send updates consistently – the app seems to cache most everything, and not refresh styles or html even when you try to force that.

So, I think PWA’s still have a way to go, in terms of online documentation and examples.  Then it still remains to be seen if people prefer to just stay inside the app stores as they have been.

Comments Off on Simply Weather Progressive Web App | mobile, Programming

First reactions to Python

January 3rd, 2017 — 5:28pm

After hearing about the python programming language for years, but never using it, I finally hunkered down to do a little testing.  Python is billed as an object oriented language and better than Java (apparently because a “Hello World” program is shorter to write in python – seriously, google it).

I’ve used many languages over the years, but mostly stick with php, perl, and javascript.  So I assumed python would be similar in syntax, and not too hard to pick up.  I went through a few courses over at lynda.com, and was on my way.

 

Waaaay back in 1990, I took a course programming in COBOL.  I recall using graph paper to write programs, because the actual column / indentation of each line mattered to the execution of the code.  Boy, was that tedious!  I was so glad to be done with that language, knowing I’d NEVER have to worry about counting whitespace again.  Imagine my surprise when I found that that in python, rather than using pesky {} braces to clearly indicate the start / end of loops, conditionals, etc., you actually have to make sure each line is indented exactly right!  What?  Oh yes.  Soon you too can get this error, which I have to believe is unique to python:

IndentationError: expected an indented block

Talk about maddening…

Ok, so let’s move on.  My first effort was to build a small web page to pull in data from an open API on the web.  Here are the results:

http://robertswebforge.com/examples/python-fda/index.cgi

Pretty ugly, right?  Having come from using php, I got used to being able to embed html code easily inside a single script.  Surely python, the age of the web, could do something similar…but…ughh..I had to write the dreaded:

print “Content-Type: text/html;charset=utf-8”

which I thought I had abandoned when I stopped using Perl to build web applications.

Now, I realize that there are many modules, frameworks, and libraries that can be used to address some of these shortcomings, and I’m sure it’s a very powerful language, in the hands of an experienced python developer.  I will probably continue to experiment with it, but my initial reaction is…bleh.

 

 

Comments Off on First reactions to Python | Programming

Multi-threaded Perl

December 21st, 2016 — 12:56pm

I recently had a chance to work on some multi-threaded perl via forking and child processes.  The idea is to spawn multple processes to handle doing multiple tasks simultaneously.  This is useful in many cases.  In my particular application, I had a script that was checking a list of servers for connectivity (essentially a ‘ping’), and alarm if unreachable.

A few sites that were helpful to me:

perldoc.perl.org/functions/fork.html

www.perlmonks.org/index.pl?node_id=43497

The coding can look a little weird, but when you call “fork”, you end up with a complete duplicate of the process – code, variables, etc.  The new process (child) starts executing from the fork call, and usually you immediately test to see whether you are the parent or the child, and go from there.  The child should exit after doing it’s job.  The parent needs to check periodically (or wait) for its child processes to “die” (yes, that’s the terminology), or you will create zombie processes (yes, that’s what they are called) on the server – not a good idea.  For this, you’ll want to implement “waitpid()”, shown in the example site referenced above.  Once all the children are spawned, the parent process needs to loop, collecting the dead child processes to clean things up properly.

Forking is a useful technique to learn, and isn’t hard once you understand the concepts involved.

Comments Off on Multi-threaded Perl | Programming

Progressive Web Apps

November 29th, 2016 — 3:35pm

I recently spent some time researching “Progessive Web Apps”, or PWAs.  They are a relatively new way to make mobile apps which utilize the web browser, rather than requiring full installation via an app store.  Google seems to be driving this particulare movement.  Here are some links I used in my research:

Your First Progressive Web App – how to build a simple, functional app, with a demo included.

Debugging Service Workers – after you get into things, this link comes in handy

Web App Install Banners – this is how you get someone to install your “app” once it’s coded and ready.

Sampling of PWA tools

I’ll update this post as I find more resources, and publish a few PWA’s myself.  I think this is the future for many apps – easier to update and distribute and lower storage requirements on the device.

I was able to get a fairly simple web app set up here: Listomizer.  You can enter in a list of items, then have the app make a random choice, alphabetize them, or randomly sort them.  When you load this, your phone should ask you if you want to add a button to your home screen.  Then it looks like an app when you open it.

Comments Off on Progressive Web Apps | mobile, Programming

Back to top