Server-Side

...now browsing by category

This is the section where all server-side posts reside. PHP, ASP.NET, C#, JAVA, PYTHON, SQL are examples of post topics.

 

Drupal 6 Social Networking

Saturday, April 18th, 2009
Drupal 6 Social Networking is one of the lastest publications addressing the use of the popular open source content management framework.

Drupal 6 Social Networking is one of the lastest publications addressing the use of the popular open source content management framework.

I’ve fought it for a long enough time, but I’m finally taming my dislike of Drupal.  I’ll admit - its mostly because I’m now using it for what it is - a framework and not as a content management system.  Now I seem to be growing as a Drupler.  Fast-foward to a few weeks back: I was browsing about Amazon and came across Drupal 6 Social Networking.  After reading the entire book in a few days, I felt it deserved a review.

Drupal 6 Social Networking is written to reach a wide audience - from programmers to content administrators, no matter the level of experience.  It is authored by Michael Peacock of Peacock-Carter, a web developer from Newcastle, UK.

The book is broken down into 10 chapters, ranging from an introduction to drupal and social networks, installing drupal, using the framework to create the functionality desired, and even into web site promotion.

The highlights of this book is that is an easy read and is written for those that are not exactly sure of what certain features relate to in the system.  For example, have you ever wondered what the full use of Taxonomy is in Drupal?  Peacock has a casual writing style that gets directly to the point, accompanying his writing with on-point visual examples to add visual detail to his writing.

If you are new to the Drupal framework, this book will guide you from installation to project setup.  If you are seasoned with the framework, but not exactly sure how to make the best of specific modules to get the performance / functionality you need, this book will enlighten you with which module to use and why to use it.

Overall, this is a worthy purchase that gets directly to what the title states - drupal 6 and social networking.  This mean, you guessed it, Michael directly focuses on how drupal can be used to build a social network and he doesn’t slide off topic in doing so.

For those interested:

5-for-1 Deal from SitePoint - profits going to victims of Australian bushfires

Thursday, February 12th, 2009


SitePoint Special Sale

Just thought I’d pass this awesome offer along to those that read the blog.

SitePoint is offering a deal to get 5 of their books in PDF format for only $29.99.

The beauty of it is that the proceeds are going to be donated to the victims of the Australian bushfires.

I’ve read a few of the books and some are hit or miss.  I definitely recommend:

Check it out, its for a good cause!

Quickie - Import Database to MYSQL via SSH/Linux

Friday, December 12th, 2008

Here is a quickie for importing an sql file to mysql on linux….

mysql -u -p dbname < sqlfile

Example

> mysql -u root -p wordpress_db < "/home/webster/backup.sql"
> Enter Password:

And there you have it…

MYSQL Backup Quickie

Saturday, November 22nd, 2008

Okay, due to timing I will be adding very quick tutorials/notes for myself.  Selfish I know, but it could prove to be of use to starters as well.

If you have your SSH handy + access to working with your database, run the following command to backup your database.

mysqldump -username -password databaseName > nameoffile.sql

So, for example - if you have a root account with the password Password and a database named MyDB and you wanted to call the backup MyDBBackup.sql and save the file into your /home/username directory.

mysqldump -root -p MyDB > /home/username/MyDBBackup.sql

Populating TextBox with Date via C#

Friday, August 22nd, 2008

I have found through my web application development experience that it can be quite the pain if someone messes up a date. This is a huge headache for reporting purposes.

Methods to get around this can be one of many things. Validation for example simply forces the user to put in a date before they can submit information. Who is to say they are putting in the right date anyway? The ASP.NET calendar control is nice, as it gives a calendar and allows the user to pick a date. One method that I have found to work quite nicely for data that is entered for a day is to have the date set automatically.

What would be a scenario for this? The ideal setting is to track a date when an action is committed. Examples include form submission (no-brainer), requests, updates, etc.

For example, let’s say we have the following form:

<form runat="server">
  <asp:Textbox ID="dateBox" runat="server"></asp:TextBox>
</form>

Now, using the code behind file, we can write a simple line of code to send today’s date straight to the dateBox ASP Textbox upon the loading of the form.

With an aspx file, in the code-behind we can add an attribute to the Page_Load method to autopopulate a date (or set any value - same premise). Below we will set it within an IF statement for when the page posts back:

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
{
  // This will happen upon a postback
  dateBox.Text =DateTime.Today.ToString("MM/dd/yyyy");
}

Or you can set the value in outside of the PostBack IF statement:

  dateBox.Text = DateTime.Today.ToString("MM/dd/yyyy");

As you can see, that simple line of code will send today’s date straight to the textbox without the user having to enter it. We have included it in the page load portion to see that it fires upon, as you guessed it, the loading of the page/form. To break it down further, we can add some pretty neat attributes to the code to produce other effects:

If we want to add tomorrow’s date, we can use .AddDays(+1)

  DateTime.Today.AddDays(+1).ToString("MM/dd/yyyy");

If we want to add yesterday, we can use .AddDays(-1)

  DateTime.Today.AddDays(-1).ToString("MM/dd/yyyy");

Want to populate a year? You can use the same premise. As Nico VanHaaster has pointed out, you can call the AddYears() method to combat leap year occurances:

  DateTime.Today.AddYears(-1).ToString("MM/dd/yyyy");

As one can see, it’s a very simple approach to solving a problem with user’s entering a date and becoming frustrated if they have to do this more than once (such as they enter this data after each task on a given day).

Happy Programming!

Populate DropDown List with MySQL and PHP

Friday, August 22nd, 2008

Drop-down lists have long been a form designer’s friend in terms of packing a lot of choices into a single area. The maintenance of these however can become tedius over time if you must continually update the list.

In the static Web 1.0 days, we would have coded each option in the list. Fortunately through the use of a server-side language and a database we can make this a lot more efficient.

In this example I will showcase how to do this in PHP. For those that use ASP.NET, I’m pretty sure you are accustomed to the drag-n-drop capabilities of Visual Studio, where you simply drag-n-drop and set your connection settings accordingly.

In PHP however, it is a little more work. In the following I will have assumed you already have a table named states created with 3 columns (I used state_id, state, state_abbr - *If you haven’t created the table, Kim Briggs has a wonderful sql example at http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-create-state-table.file .*

I will also assume that you have already connected to the database in your file.

We will begin first by creating a variable which will hold our SQL query. From there we will create a variable that will hold the result.

<?
$query="SELECT * FROM state";
$result = mysql_query ($query);

Then we shall begin the drop down list:

echo "<select name='state' value=''>State</option>";

And we will use a while statement to create an array to cycle through the results printing HTML options for each item. As you will see, we will be binding the columns to the values that we want to print on the page.

while($myfetch=mysql_fetch_array($result)){
echo "<option value=$myfetch[state_id]>$myfetch[state_abbr]</option>";
}

From there we will close the list and the PHP file.:

echo "</select>";
?>

And for the code in its complete form:

 <?
  //Create SQL Query
  $query="SELECT * FROM state";
  // Variable for results
  $result = mysql_query ($query);
  // Begin select tag
  echo "<select name=state value=''>State</option>";
  //Return each result
  while($myfetch=mysql_fetch_array($result))
  {
     echo "<option value=$myfetch[state_id]>$myfetch[state_abbr]</option>";
  }
  //End select
  echo "</select>";
?>

Easy enough huh? Do note that with the query, we can control how the results are printed (IE alphabetical, by id, etc.). This can be used for a variety of fields, from states to categories.

Dynamically Populating a DropDownList with C#

Wednesday, August 20th, 2008

Okay, out of fairness I thought I would post the same tutorial for my favorite dynamic duo - ASP.NET and C#. We will assume we are using the same database name or table name. We will work with a asp:DropDownList control such as:


<asp:DropDownList id="state" runat="server"></asp:DropDownList>

We will create a method called StateDropDown:

private void StateDropDown()
{
}

From there we will define our connection settings - typically what we have set in the web.config file.

SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["MySettings"].ConnectionString);

From there, much like the PHP example, we will create an SQL query that will scope the database table to populate the dropdown. We will assume i’m connecting to a database named info and a table named state.

string Sql = "SELECT * FROM info..state";

Now for the fun of C# - creating a dataset, filling the dataset, then applying that dataset to your asp:dropdown control.

SqlDataAdapter sqlAdapter = new SqlDataAdapter(Sql, Con);
DataSet ds = new DataSet();
sqlDA.Fill(ds);
state.DataSource = ds.Tables[0];
state.DataTextField = "state_abbr";
state.DataValueField = "state_id";
state.DataBind();
}

And now for the unmolested code:

private void StateDropDown()
{
//Define your connection
SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["MySettings"].ConnectionString);
//Create your SQL query/string
string Sql = "SELECT * FROM info..state";
SqlDataAdapter sqlAdapter = new SqlDataAdapter(Sql, Con);
DataSet ds = new DataSet();
sqlDA.Fill(ds);
//Define which control will accept this dataset
state.DataSource = ds.Tables[0];
state.DataTextField = "state_abbr";
state.DataValueField = "state_id";
//Bind the dataset to the control
state.DataBind();
}

Test Database Connectivity in PHP with PDO

Tuesday, August 19th, 2008

Testing connectivity to a database can be achieved a few different ways in PHP. Today we’ll take a quick look at a feature called PHP Database Objects (PDO). PDO is defined by php.net as:

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.

Now, on to how this bad boy works.

As with anything, we must start by assigning various variables. Similar to other methods of connecting, we will assign variables for the mysql connection host, database, username, and password.

// Assign variables for connecting to MYSQL Host
$dsn = 'mysql:host=localhost;dbname=mydatabase;';
$user = 'user';
$password = 'password';

In other ways, we could break it down into more variables:

$db_host = 'localhost';
$db_name = 'mydatabase';
$user = 'user';
$password = 'password';
$dsn = 'mysql:host=$db_host;dbname=$db_name;';

From there we will move on to a try catch statement using the PDO class to create a<strong> new PDO()</strong>. We will use our trust try/catch statement to test the connection and report any errors that may occur.

// Test the connection, alert if fails - very similar to C# in a sense
try
{
$sqlconn = new PDO($dsn, $user, $password);
//echo 'success';<img class="btnimage" onclick="formattext('text', 'outdent', '')" src="http://www.bobbyhash.com/wysiwyg/kevinrte/images/outdent.png" alt="" width="25" height="24" />
}

We will use $e as the exception / what our event will argue.

catch (PDOException $e)
{
echo 'Connection failed: ' .$e->getMessage();
}

As you can see it is a simple process. Set the variables, then utilize a try/catch statement to make the PDO request / handle the error.

And now for the unmolested code: Enjoy!

// Assign variables for connecting to MYSQL Host
$dsn = 'mysql:host = localhost;dbname=litebox;';
$user = 'user';
$password = '';

// Test the connection, alert if fails - very similar to C# or
//any other try/catch in a programming language in a sense
try
{
$sqlconn = new PDO($dsn, $user, $password);
//echo 'success';
}
catch (PDOException $e)
{
echo 'Connection failed: ' .$e->getMessage();
}

The Move To Wordpress

Monday, August 18th, 2008

Wordpress move is going fantastic!

What? My Program Works?

Monday, May 21st, 2007

I gotta say, the ability to understand programming pays off. After getting fed up with the time I was wasting logging into Ta-Da List I decided to write my own listing program.

I like to look at it as a list management system.

I really didn’t need to be super organized, such as organizing by day, week, etc. I created the list just how I would create my own sticky notes - random, jot some info down with a title and post it. The program works just the same - I have an idea, I click my bookmark, the web application comes up and I’m ready to go. Add a title, a little descriptive information and wah-la! It’s posted. I can even edit the list if I screw something up and I can delete it once the task/list/whatever is completed.

I’m still thinking about releasing it once I complete the interface to meet my tastes. Will it be open source? Most likely!

PHP and MySQL are a great marriage. What are some of the applications some of you guys are building to meet your needs?