Posts

Easy database querying with dOOdads (C#, .NET)

Been some time since I posted about MyGeneration and dOOdads. Still using it regularly, even though it is a few years old now. A few sites I do still use .NET 1.1, so it is still in use. A brief (re)introduction - MyGeneration is a tool that can generate code for you, through the use of templates (you can create your own, and there is a template library ). It comes bundled with the dOOdads data abstraction library, which allows you to update and query your database without knowing the intricacies of interacting with it (i.e. .NET classes to use, SQL to write etc). Both MyGeneration and dOOdads support multiple databases (SQL Server, MySQL, Oracle etc) and regeneration of code, changing the connection string is often all that is needed to move to another system. It was freeware initially, but is now hosted on SourceForge and no longer actively maintained by the main developers. A basic example of a query would be: Employees e = new Employees(); e.Where.EmployeeID.Value = 1; if(e.Q...

jQuery UI draggables - helper CSS class

While working with jQuery UI draggables , I have noticed that there does not seem to be a class associated with the draggable helper, and so it is styled in the same way as the draggable you selected. As a result you have to resort to the start function option to add the class dynamically: $(".dragme").draggable({helper: "clone", start: function(e, ui) { $(ui.helper).addClass("ui-draggable-helper"); } }); Then just create a style for ui-draggable-helper , e.g. .ui-draggable-helper { border: 1px dotted #000; padding: 6px; background: #fff; font-size: 1.2em; }

Place JavaScript code at bottom of page

Recently I've been placing scripts at the bottom of the page, instead if the top. However, I don't put all of them there - the key ones required by other scripts (e.g. libraries like jQuery) are still in the header. The pages can appear to load faster, and in some cases may save some (albeit not always that much) code, e.g. .... <script src="js/jquery-1.2.6.min.js" type="text/javascript"></script> <script type="text/javascript"> $( function() { $("a.foo").click(doSomething); }; function doSomething() { return confirm("About to visit " + this.href + ", continue?"); } </script> </head> <body> ... </body> </html> Can instead be: .... <script src="js/jquery-1.2.6.min.js" type="text/javascript"></script> </head> <body> ... <script type="text/javascript"> $("a.foo").click(doSomething); function doSomethi...

jQuery Quick Tip: In general $(this).attr("attribute") = this.attribute

jQuery is a very useful JavaScript library for manipulating you web pages. However, I have seen it used to get attribute values when it doesn't have to be, normally to get element attributes. For example, opening all links with a class external and giving them a title (tooltip). $("a.external").click( function() { window.open($(this).attr("href")); return false; } ).each( function() { $(this).attr("title", $(this).attr("title") + " External link: " + $(this).attr("href")); } ); Would be the same as $("a.external").click( function() { window.open(this.href); return false; } ).each( function() { this.title += " External link: " + this.href; } ); There are some attributes that in JavaScript aren't the same as they are in html. The ones you may have issues with are listed below. for (htmlFor) class (className) readonly (readOnly) maxlengt...

Querying a SQLite 3 database using PHP

Previously, I detailed a way of creating a database in A basic hit counter using PHP and SQLite 3 using PHP Data Objects and PHP 5. For most web sites, SQLite would be fine, but for very high volume (as in many hundreds of thousands of hits per day), there are better options - Appropriate Uses For SQLite (SQLite.org) has more details on when SQLite is a good option. Here is an example of how to query the data and display it on a page (to see which pages are popular for example). <? $dbfolder = $_SERVER["DOCUMENT_ROOT"]."/data/"; $dbname = $_SERVER["HTTP_HOST"]."_log.sq3"; $logdb = new PDO("sqlite:".$dbfolder.$dbname); $starttable = "<table> <tr> <th>Page</th> <th>Counter</th> </tr>"; $endtable = "</table>"; $tablecontents = ""; foreach ($logdb->query("SELECT * FROM hits ORDER BY counter DESC") as $row) { $tablecontents .= " <t...

A basic hit counter using PHP and SQLite 3

SQLite is a lightweight database engine that is bundled with PHP 5. Using PHP Data Objects (PDO) you can create and edit SQLite databases and tables. A SQLite databases is just a single file that is stored in a folder that has modify rights applied to it (otherwise you can't change it). This sample shows how to create a database, add a table to it and insert and update a record in it. By using PDO, you can easily change it to use different database engines (like MySQL, IBM DB2 etc [others, like MSSQL and Oracle are in a experimental stage though, check the PDO site (linked above) for more information]). You can copy this code into a separate file and include it within pages that you want a counter on &lt? include("counter.php"); ?> . <? // logging page hits $dbfolder = $_SERVER["DOCUMENT_ROOT"]."/data/"; $dbname = $_SERVER["HTTP_HOST"]."_log.sq3"; // check if database file exists first if(!file_exists($dbfolder.$dbna...

JavaScript dates and timezone offsets

Working with dates can be problematic when handling dates returned by a remote server. They were returned in GMT format, but when displayed on a page, the time was either ahead or behind (depending on which timezone you are in). For example, if you have the date returned via JSON: var dates = {"StartDate": new Date(1216944000000),"EndDate": new Date(1217030399000)} So the date now has the timezone offset added: dates.StartDate = Fri Jul 25 2008 01:00:00 GMT+0100 (GMT Standard Time) dates.EndDate = Sat Jul 26 2008 00:59:59 GMT+0100 (GMT Standard Time) So now, to take into account the timezone offset, getTimezoneOffset() can be used, along with setMinutes() and getMinutes() dates.StartDate.setMinutes(dates.StartDate.getTimezoneOffset() + dates.StartDate.getMinutes()); dates.EndDate.setMinutes(dates.EndDate.getTimezoneOffset() + dates.EndDate.getMinutes()); For a useful JavaScript library for working with dates (it extends the native Date object), check out Dat...