Archive for the ‘PHP’ Category

Recommended CMS for newbies

Published: Aug 19, 2009
Tags: , , , , ,

1.) CCCA by IDX Web Designs

cccaContent management system based on PHP and MySQL. Powerful yet simple CMS for all. Comprehensive functionality. Pricing seems to be reasonable for the small company. Easy to install and user friendly.  Available only for All IDX Web Designs Clients. Contact IDX Web Designs and Request a quote

2.) Joomla by Wilco Jansen

joomla-s-webtreatsetc-128An award-winning content management system (CMS), which enables you to build Web sites and powerful online applications. Many aspects, including its ease-of-use and extensibility, have made Joomla the most popular Web site software available. Best of all, Joomla is an open source solution that is freely available to everyone.

3.) WordPress by Ryan Boren

wordpress-logoState-of-the-art publishing platform with a focus on aesthetics, web standards, and usability. WordPress is both free and priceless at the same time.
More simply, WordPress is what you use when you want to work with your blogging software, not fight it.

4.) Soholaunch

soholaunchAn easy-to-use website creation tool to help you build, maintain, and manage your personal or business website. It runs right from your website, making it easy to take shopping cart orders online, create forms, and edit site pages from any computer in the world!

5.) Drupal

drupal-s-webtreatsetc-128A free software package that allows an individual or a community of users to easily publish, manage and organize a wide variety of content on a website. Tens of thousands of people and organizations are using Drupal to power scores of different web sites

Rocky Rasonable

A filipino Senior PHP Programmer, Web Developer and Webmaster based in Davao, Philippines. Expert in Joomla, WordPress, Soholaunch, Oscommerce, Drupal, Social Media Sites, and etc.

More Posts - Website

 

Archive for the ‘PHP’ Category

Recommended CMS for newbies

Published: Aug 19, 2009
Tags: , , , , ,

search-engine-optimization-blogThis is the first in what I hope will be a long list of tutorials that I like to call Practical Programming in PHP. Lets get down to business, today I’m going to show you how to create a simple search engine that you can use for your very own site or some other project that you may be working on. For this tutorial I will be using a MySQL database. The bulk of the code should be compatible with PHP 3 and 4.

Background:

Before we begin, I’ll show you the table I’ll be using in the examples:

First_Name Middle_Name Last_Name
Dana Johnson Smith
Jill Angel Petersburg
Jack Coner Mitchel

If you feel confused about any of the functions in the tutorial, have a look at my previous tutorial that deals with common database functions.

Down To Work:

Before we actually make the search engine, we need to create a basic webpage that will have an input field where the user can enter his or her search query. I’m going to keep mine simple; feel free to make an elaborate one with lots of bells and whistles. The code for my page is below:

<html>

<head>

  <title>Simple Search Engine version 1.0</title>

</head>

<body>

  <center>

    Enter the first, last, or middle name of the person you are looking for:

    <form action="search.php" method="post">

      <input type="text" name="search_query" maxlength="25" size="15"><br>

      <input type="reset" name="reset" value="Reset"> 

      <input type="text" name="submit" value="Submit">

    </form>

  </center> 

</body>

</html>

It’s a pretty basic page so I’m not going to explain alot of it. Basically, the user will enter the first, middle, or last name of the person they are looking for and hit enter. The contents of the input field will be passed to a php script named “search.php” which will handle the rest.

Now that the page is out of the way, let’s create the actual script. First, we need to connect to the database using mysql_pconnect() and select the table using mysql_select_db(). Next, we want to parse the value passed to the script to see if it contains any invalid input, such as numbers and funky characters like #&*^. You should always validate input, don’t rely on things like JavaScript to do it for you, because once the user disables JavaScript all that fancy validation goes down the toilet. To check the input we are going to use a regular expression, they are a bit confusing and will be explained in a later tutorial. For now, all you need to know is that it will check to see if value passed is a string of characters. All right, enough chatter, here is the first part of the script:

<?php 

  mysql_pconnect("host", "username", "password") or die("Can't connect!");

  mysql_select_db("Names") or die("Can't select database!"); 

  if (!eregi("[[:alpha:]]", $search_query))

  {

    echo "Error: you have entered an invalid query, you can only use characters!<br>";

    exit;

  }

Now that we’ve done that, we will form the search query.

$query= mysql_query("SELECT * FROM some_table WHERE First_Name= '$search_query' 

OR Middle_Name= '$search_query' OR Last_Name= '$search_query' ORDER BY Last_Name");

Look confusing? I’ll explain, what’s happening is, we’re asking MySQL to search all the rows in First_Name, Middle_Name, and Last_Name for a match to the query entered by the user; then, take the results of that search, alphabetize the results by Last_Name.

The rest of the coding from now on is a breeze. We will get the results from the query using mysql_fetch_array( ), and check to see if there is a match using mysql_num_rows(). If there is a match, or matches, we will output it along with the number of matches found; if there isn’t, we’ll report to the user that we couldn’t find anything.

$result= mysql_num_rows($query);

  if ($result == 0)

  {

    echo "Sorry, I couldn't find any user that matches your query ($search_query)";

    exit; 

  }

  else if ($result == 1)

  {

    echo "I've found <b>1</b> match!<br>";

  }

  else {

    echo "I've found <b>$result</b> matches! <br>";

  }

  while ($row= mysql_fetch_array($query))

  {

    $first_name= $row["First_Name"];

    $middle_name = $row["Middle_Name"];

    $last_name = $row["Last_Name"];

    echo "The first name of the user is: $first_name.<br>";

    echo "The middle name of the user is: $middle_name.<br>";

    echo "The last name of the user is: $last_name.<br>";

  }

?>

I added that extra if statement so that when we report how many users we’ve found, its output will be in proper English. If I we don’t, the script will echo “I’ve found 1 matches” which obviously isn’t good grammar :P The rest of the script loops through the results and prints them to a webpage. That’s all, we’ve finished the script! The entire script is included below:

<html>

<head>

  <title>Simple Search Engine version 1.0 - Results </title>

</head>

<body> 

<?php 

  mysql_pconnect("host", "username", "password") or die("Can't connect!");

  mysql_select_db("Names") or die("Can't select database!"); 

  if (!eregi("[[:alpha:]]", $search_query))

  {

    echo "Error: you have entered an invalid query, you can only use characters!<br>";

    exit; //No need to execute the rest of the script.

  }

  $query= mysql_query("SELECT * FROM some_table WHERE First_Name='$search_query' 

  OR Middle_Name= '$search_query' OR Last_Name='$search_query' ORDER BY Last_Name");

  $result= mysql_numrows($query);

  if ($result == 0)

  {

    echo "Sorry, I couldn't find any user that matches your query ($search_query)";

    exit; //No results found, why bother executing the rest of the script?

  }

  else if ($result == 1)

  {

    echo "I've found <b>1</b> match!<br>";

  }

  else {

    echo "I've found <b>$result</b> matches!<br>";

  }

  while ($row= mysql_fetch_array($query))

  {

    $first_name= $row["First_Name"];

    $middle_name = $row["Middle_Name"];

    $last_name = $row["Last_Name"];

    echo "The first name of the user is: $first_name.<br>";

    echo "The middle name of the user is: $middle_name.<br>";

    echo "The last name of the user is: $last_name. <br>";

  }

?> 

</body>

</html>

Rocky Rasonable

A filipino Senior PHP Programmer, Web Developer and Webmaster based in Davao, Philippines. Expert in Joomla, WordPress, Soholaunch, Oscommerce, Drupal, Social Media Sites, and etc.

More Posts - Website

 

Archive for the ‘PHP’ Category

Recommended CMS for newbies

Published: Aug 19, 2009
Tags: , , , , ,

The first thing we need to do is connect to the database.

mysql_connect("somehost", "username", "password") or die ("Can't connect!");

This will try to connect to the database on somehost and login with “username” as the username and “password” as the password. If it can’t, it will output an error message saying that it can’t connect. For your own code be sure to change somehost to your host (most of the times it’s localhost, ask your admin), username to your username (duh), and password to your password. Another way to connect to a database is to open a persistent connection. To do this, use the mysql_pconnectfunction and pass it the same arguments as mysql_connect. Why open a persistent connection? When you call mysql_pconnect, instead of going out and opening a connection to the database, it sees if one is already open, if it is, the script will use it. Also, when the script has finished executing, the connection to the database will not automatically be closed like it is when using mysql_connect. This way the connection can be used later on. Using a persistent connection is a good idea if your scripts constantly need to connect to the database.

After we have opened a connection to the database, we then select a database.

mysql_select_db("database_name") or die("Can't select database!");

This will try to select the database named “database_name” (for your own code change it to the name of your database). If it can’t select the database, it will output and error. Once you’re actually connected to a database, you will want to query a table in the database to get whatever you want done. A query looks like this:

mysql_query("Some query");

Common queries are SELECTand INSERT For full documentation go to the mysql web site (http://www.mysql.com). Another common php function is mysql_num_rows; if it isn’t obvious this gets the number of rows from a query. Here is an example of how it can be used with mysql_query:

<?php

  $result= mysql_query("SELECT * FROM some_table");

  $number_of_rows= @mysql_num_rows($result);

  if ($number_of_rows == 0)

  {

    echo "Sorry there are no rows";

  }

  else {

    echo "Yes! we found some rows!";

  }

?>

Now you may be wondering why I put the @ sign before mysql_num_rows. In php, the @ sign suppress errors; I put it in front of mysql_num_rows so that if there are no rows, MySQL will not output a bunch of errors. So when would mysql_num_rows be useful? Well, you could use it for an authentication script which searchs the database for a username and password and if it doesn’t find any (i.e. if no rows are returned), it tells the user that the username, or password, are not correct.

Another really useful function is mysql_fetch_array, because it gets the rows and puts them in an array that contains the name of the rows. That way instead of having to access each row by number you can do it by name! For example, let’s say that our database looked like this:

User Password
John afasdfadsfdsf
Billy tla;jrjealjwqsldajf
Mitch pqrtupipripewir

We would use the following code to get the users’ names and output them:

<?php

  echo "The users in this database are: <br>";

  $result= mysql_query("SELECT * FROM some_table");

  while ($row= mysql_fetch_array($result))

  {

    $username= $row["User"];

    echo "$username<br>";

  }

?>

This will output all the usernames in a database; you can add error checking if you like. The while statement is read “while there are rows that satisfy the query, put the contents of the row from the column ‘User’ into the variable ‘username,’ and print the usernames (each on a new line) to an HTML page.”

Now let’s cover a couple of functions that actually work with the database. The first is mysql_create_db, don’t you just love how the functions are named you can figure out what they do just by looking at the function name, this one obviously creates a database. Here’s how to use it:

<?php

  echo "I am going to try to create a database...<br>";

  if (mysql_create_db("test_database"))

  {

    echo "Hooray, I've created the database!<br>";

  }

  else {

    echo "Darn couldn't create the database! because: ";

    echo "mysql_error() <br>";

  }

?>

You can see I used a new function, mysql_error, you don’t really need to know too much about it, all it does is return the error string sent by MySQL. Now since we learned how to create a database, how’s about we learn to delete one. To do that use the mysql_drop_db, here is how to use it:

<?php

  echo "I am going to try to delete a database...<br>";

  $result= mysql_drop_db("test_database");

  if (!$result)

  {

    echo "Darn couldn't I couldn't delete the database!<br>";

  }

  else {

    echo "Hooray, I've deleted the database<br>";

  }

?>

You can see that the syntax is very similar to that of mysql_create_db, just pop the name of the database you want to delete into the function.

The next two items aren’t functions, rather they are queries that you can use to manage an existing table. The following query will insert data into a database:

<?php

  echo "I am going to try to insert data into a table...<br>";

  $result= mysql_query("INSERT INTO test_database (username, password) VALUES

	               (Rahim, adfjaldadfsdaf)");

  if (!$result)

  {

    echo "Darn couldn't I couldn't delete the database!<br>";

  }

  else {

    echo "Hooray, I've deleted the database<br>";

  }

?>

This query should be pretty obvious, it inserts the data defined in between the parentheses into the rows. Just a little note to remember, the order in which you write out the column names is the order your data will be entered (i.e. a row with the contents Rahim will be entered under username, not password since we wrote username then pasword, if it was reveresd Rahim would be put under password).

The next query we’ve already gone over, I’m just going to add to it; after I’m done you should be able to use it to help create a simple search engine (upcomming tutorial)! For the sake of brevity I’ll remove all the extra php stuff and just show you the “meat” of the code.

$result= mysql_query("SELECT name FROM some_table WHERE name=Joe AND

         lastname=Sixpack OR lastname=Becker ORDER BY lastname LIMIT 20");

Now I know that looks like a long query, but it’s not really all that bad. What it’s pretty much saying is: “Get me the name from some_table where the name is Joe and the lastname is Sixpack or Becker, oh and by the way while your at it, put it in alphabetical order by the lastname; oh and one last thing, just get the first 20 results please.” MySQL has lots of other filters that you can add on to the SELECT statement, I highly suggest you download the MySQL documentation and give it a perusing.

Rocky Rasonable

A filipino Senior PHP Programmer, Web Developer and Webmaster based in Davao, Philippines. Expert in Joomla, WordPress, Soholaunch, Oscommerce, Drupal, Social Media Sites, and etc.

More Posts - Website

 

Translator

English flagItalian flagKorean flagChinese (Simplified) flagChinese (Traditional) flagPortuguese flagGerman flagFrench flagSpanish flagJapanese flag
Arabic flagGreek flagDutch flagBulgarian flagCzech flagCroatian flagDanish flagFinnish flagHindi flagPolish flag
Romanian flagSwedish flagNorwegian flagCatalan flagFilipino flagHebrew flagIndonesian flagLatvian flagLithuanian flagSerbian flag
Slovak flagSlovenian flagUkrainian flagVietnamese flagAlbanian flagEstonian flagGalician flagMaltese flagThai flagTurkish flag
Hungarian flag         

My Partners

Review rockyrasonable.com on alexa.com

hostgator
Hostgator templateplazzaelegantthemesrocketthemeTopPhilippineWebsites.com Programming Blogroll Center

Tags

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

Powered by WP Robot