What?
A note for myself on some code to convert a string of two names into a string made up of the first name and then using the initial of the second name.
copyraw
-- What I have
John Smith
Fred.Bloggs

-- What I want
John S.
Fred B.
  1.  -- What I have 
  2.  John Smith 
  3.  Fred.Bloggs 
  4.   
  5.  -- What I want 
  6.  John S. 
  7.  Fred B. 

How?
Category: Personal Home Page :: Article: 556

What?
A quick note on a htaccess rewrite rule I'm liking.

What does it do?
What I type:
copyraw
http://www.mywebsite.com/blog/videos.html
  1.  http://www.mywebsite.com/blog/videos.html 
Sends this to server:
copyraw
http://www.mywebsite.com/index.php?myFolder=blog&myFiles=videos
  1.  http://www.mywebsite.com/index.php?myFolder=blog&myFiles=videos 
How?
Category: Personal Home Page :: Article: 520

What?
For those of you who use Preg_Replace. Preg_replace is a function that uses regular expressions to search and replace a string.

Why?
Because my understanding with regular expressions is shady and varies from language to language, I've written this article as a quick reference point.

How?

What?
Just a quick note on how to format a given filesize and to reduce the display output to a small string, eg:
copyraw
196 bytes          : displays as => "196 bytes"
   12945 bytes        : displays as => "12 Kb"
   1478515 bytes      : displays as => "1 Mb"
   8798745455 bytes   : displays as => "8 Gb"
  1.  196 bytes          : displays as => "196 bytes" 
  2.     12945 bytes        : displays as => "12 Kb" 
  3.     1478515 bytes      : displays as => "1 Mb" 
  4.     8798745455 bytes   : displays as => "8 Gb" 

Category: Personal Home Page :: Article: 468

What?
This is intended for activity/session durations and although I usually get MySQL to do the date/time calculations, there are times when we have to do with PHP. This is the shortest way I know to properly convert seconds into total hours, minutes and seconds (taking into account regional settings and without using a date function).

How?
copyraw
$total_time =intval(intval($total_seconds)/ 3600).":";
$total_time.=str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":";
$total_time.=str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT);

// yields
// 82800  = 23:00:00
// 108000 = 30:00:00
  1.  $total_time =intval(intval($total_seconds)3600).":"
  2.  $total_time.=str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":"
  3.  $total_time.=str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT)
  4.   
  5.  // yields 
  6.  // 82800  = 23:00:00 
  7.  // 108000 = 30:00:00 
On one line:
copyraw
$total_time=intval(intval($total_seconds)/ 3600).":".str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":".str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT);
  1.  $total_time=intval(intval($total_seconds)3600).":".str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":".str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT)
Category: Personal Home Page :: Article: 413

So I know it's quite a long title but I'm not sure what I'll be searching next time. It has taken me a lot longer than I thought it would mostly out of frustration and the inability to know exactly what I'm looking for... always difficult. After a cup of tea the solution was glaringly obvious, just do a primary school mathematics table and it all makes sense (see my 10 mod table below).

This article is a quick note (so I never spend as long again) in PHP on how to determine when looping through a loop, which entry was first and which was last. This is incredibly useful for pagination.


Do you hate sites that do not have a search feature? I do. I think it defeats the purpose of cramming information endlessly in cyberspace.

So we want to give a search engine to our users. This sounds really simple, we could try:
copyraw
$search_term_esc = AddSlashes($search_term);
$sql = "SELECT * FROM Content WHERE content_body LIKE '%$search_term_esc%'";
  1.  $search_term_esc = AddSlashes($search_term)
  2.  $sql = "SELECT * FROM Content WHERE content_body LIKE '%$search_term_esc%'"
Great! Few problems though, multiple terms are not supported; quotation marks and apostrophes may be an issue;
Category: Personal Home Page :: Article: 328

This is a note to myself but also to anyone out there who's spent as long as I did looking for a solution to this. Maybe it's just me but this is the scenario:
  1. Joomla works fine with international characters
  2. A Module Extension I wrote for Joomla! displayed funny characters.
  3. The Joomla! articles were displaying the correct characters for that language set.
  4. I needed to make my extension read data from a MySQL database and display the caracters as intended with UTF8.

I tried enough extensions and forum solutions, and although these changes would have an effect on the module (such as take away accents and convert to ASCII), they weren't what we were looking for.

The quick solution was to make the script run a MySQL command at the start:
copyraw
SET NAMES 'utf8'
  1.  SET NAMES 'utf8' 
Now I need to run this command from within a Joomla! extension using the mysql Joomla! classes, here's how I've used in this script pulling IDs and titles from the a sample table:
Category: Personal Home Page :: Article: 303

Quick tip or note to self: How to get yesterday's date irrespective of date format or daylight savings time.
copyraw
$today=date("Y-m-d");
$yesterday = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") - 1, date("Y")));
  1.  $today=date("Y-m-d")
  2.  $yesterday = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") - 1, date("Y")))
Category: Personal Home Page :: Article: 299

I've decided to put something in here as it took me an age to find out how I could do it.

This is when using a MySQL query within a PHP script.  The process is used often to do a statistics table or top ten chart of your data (eg. movies, music, etc).

My aim is to do the following:

  1. retrieve data from a table,
  2. count the number of times each data exists,
  3. sort it in reverse order so that the most frequent is at the top of the list
  4. print out each row with the number of times that particular data appeared in a row

My old method was to:


 I'm beginning a list as I've just spent an age trying to get PHP output to create a text file.  Then my client showed me how she then opens the text file in Excel, so I said we could get the script to do that instead.

PHP to TXT

copyraw
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: attachment; filename=foo.txt');
echo 'contents of file';
  1.  header('Content-Type: text/plain; charset=utf-8')
  2.  header('Content-Disposition: attachment; filename=foo.txt')
  3.  echo 'contents of file'

 

PHP to XLS

copyraw
$export_file = "my_name.xls"; 
ob_end_clean(); 
ini_set('zlib.output_compression','Off'); 
header('Pragma: public'); 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT'); 
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header ("Pragma: no-cache"); 
header("Expires: 0"); 
header('Content-Transfer-Encoding: none'); 
header('Content-Type: application/vnd.ms-excel;'); 
header("Content-type: application/x-msexcel");  
header('Content-Disposition: attachment; filename="'.basename($export_file).'"');
  1.  $export_file = "my_name.xls"; 
  2.  ob_end_clean()
  3.  ini_set('zlib.output_compression','Off')
  4.  header('Pragma: public')
  5.  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT")
  6.  header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT')
  7.  header('Cache-Control: no-store, no-cache, must-revalidate')
  8.  header('Cache-Control: pre-check=0, post-check=0, max-age=0')
  9.  header ("Pragma: no-cache")
  10.  header("Expires: 0")
  11.  header('Content-Transfer-Encoding: none')
  12.  header('Content-Type: application/vnd.ms-excel;')
  13.  header("Content-type: application/x-msexcel")
  14.  header('Content-Disposition: attachment; filename="'.basename($export_file).'"')
Category: Personal Home Page :: Article: 240

The Issue

If you've ever made PHP scripts to process data within a LAMP environment (Linux, Apache, MySQL, PHP) then this happens a lot.  In the following example, our HTML form will allow the user to specify a date (so excludes hours, minutes and seconds).  For demonstration purposes, I'm going to be using the European date format so DD/MM/YYYY.

The Solution

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

RSS Feed

Related Articles

Joes Revolver Map

Joes Word Cloud

table   first   page   function   note   form   error   code   license   list   system   create   deluge   where   client   display   report   time   order   would   need   name   field   joomla   creator   script   google   date   parameter   website   file   uploaded   following   value   version   windows   server   added   work   files   database   used   mysql   case   zoho   user   source   data   using   find   JoelLipman.Com

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.