Archive for Web Development

Create Form in Joomla! Using perForms

Create new input form

  1. Go to Components > perForms
  2. Click the New button
  3. Type in Title, Intro, and Thanks text

Add forms input fields

  1. Click on Edit Items on the Input Forms we had just created
  2. Click New Item
  3. Fill it in

Email the forms

  1. Component > perForms
  2. Click on the form name
  3. Go to E-mails tab

Have forms go into a database

  1. Component > perForms
  2. Tick the checkbox and click on Data

Comments (3)

Using Database Query (DBQ) in Joomla!

How To Install DBQ Component in Joomla!

Download DBQ

  1. Download Database Query for Joomla! 1.0 here.

Install DBQ

  1. Login into the administration area and go to Installers > Component from the menu.
  2. Use the “Choose…” button under Upload Package File to select the file you just had downloaded. Then click on Upload File & Install.
  3. If installation is successful, a “Upload component – success” page will appear.

Connect DBQ to your database

  1. In the administration area, go to Component > DBQ Manager > Databases.
  2. Click New to specify new database connection.
  3. Select the correct Driver and Type according to your database. Enter “localhost” as the Hostname if you are using a local web server. Schemaname is the name of your database in the web server. Fill in the right Username and Password. Click Save when you are done.
    database connection for DBQ

Create Category

  1. In the administration area, go to Component > DBQ Manager > Categories.
  2. Click New to create a new category and fill in the Category Name, Category Title, and Description. Click Save when you are done.

Basic Search Query

Step One: Create the Query.

  1. In the administration area, go to Component > DBQ Manager > Queries.
  2. Click New to create a new query.
  3. Type in your SQL statement. For this exercise, I use the following SQL:

    SELECT
    pel_name    AS $<Pelajar_Name>,
    pel_no_matriks  AS $<Pelajar_No_Matriks>,
    pel_sesi    AS $<Pelajar_Sesi>,
    pel_id      AS $<Pelajar_View_Link>
    FROM tbl_pelajar
    WHERE pel_id != 0

  4. Fill in all the other fields in the Configuration tab as illustrated below:
    create query with DBQ
  5. On the Attributes tab, set Published to “Yes”.
  6. On the Parameters tab, set Order Variables As Found In Query to “Yes”.
  7. Click Save. The query will be listed in DBQ Manager > Queries. You can also view the query on the front end at http://localhost/joomla/index.php?option=com_dbquery

Step Two: Parse the Query.

  1. Tick the checkbox next to the query name, and click the Parse button.
  2. Check that the type of each variable is set to “Field Variable”

Step Three: Configure the Variables.

  1. Set all variables to active

Display Your Query

  1. Now that your query is ready, you can assign it to a menu in Joomla!. In the administration area, go to Menu > mainmenu.
  2. Click New. Select Components.
  3. Specify the name of your menu. Select DBQ Manager from Component and click Apply.
  4. Under parameters, enter the DBQ Query ID for your created query (you can check it on DBQ Manager > Queries) and click Save.
  5. You will see your newly created menu on your Joomla! home page.

Advanced Search Query

  1. In the administration area, go to Component > DBQ Manager > Queries.
  2. Tick our previous query and click the Copy button.
  3. Click on the query name to modify it using the following SQL:

  4. SELECT
    pel_name    AS $<Pelajar_Name>,
    pel_no_matriks  AS $<Pelajar_No_Matriks>,
    pel_sesi    AS $<Pelajar_Sesi>,
    pel_id      AS $<Pelajar_View_Link>
    FROM tbl_pelajar
    WHERE pel_id != 0
    $[PEL_SEARCH_NAME:  AND pel_name LIKE '%${pel_search_name}%'  ]
    $[PEL_SEARCH_NO_MATRIKS:  AND pel_no_matriks = '{pel_search_no_matriks}'  ]
    $[PEL_SEARCH_SESI:  AND pel_sesi = '${pel_search_sesi}'  ]

  5. Parse the query. Set :
    • PEL_SEARCH_NAME,  PEL_SEARCH_SESI and PEL_SEARCH_NO_MATRIKS to type “Statement Variable”.
    • pel_search_name to type “Keyword Variable”
    • pel_no_matriks and pel_search_sesi to type “Query Result Variable”
  6. Configure the variables:
    • Edit each statement variable to “Not Required”
    • For “Keyword Variable”, use “Text Box”
    • For “Query Result Variable”, use the “Drop Down List” input
    • Set SQL query for pel_no_matriks in the Parameters tab: SELECT pel_no_matriks AS value, pel_no_matriks AS label FROM tbl_pelajar ORDER BY pel_no_matriks
    • Set SQL query for pel_sesi in the Parameters tab: SELECT pel_sesi AS value, pel_sesi AS label FROM tbl_pelajar ORDER BY pel_sesi
    • Set the Data Source to “SQL Query”
  7. Test the query.

Basic View Query

This query will let the user view a specifc record in the table.  Our third query resembles the other two queries except that it displays additional felds and accepts only a single input, the record ID.

  1. Create the query:

  2. SELECT
    p.pel_no_matriks  AS $<Pelajar_No_Matriks>,
    p.pel_name    AS $<Pelajar_Name>,
    j.jab_name    AS $<Jabatan_Name>,
    p.pel_sesi    AS $<Pelajar_Sesi>,
    p.pel_alamat  AS $<Pelajar_Alamat>,
    p.pel_jantina AS $<Pelajar_Jantina>,
    p.pel_ic      AS $<Pelajar_Ic>,
    p.pel_no_tel  AS $<Pelajar_No_Tel>,
    p.pel_id      AS $<Pelajar_View_Link>FROM tbl_pelajar p, tbl_jabatan jWHERE p.pel_id = ${pelajar_id} AND p.jab_id = j.jab_id

  3. In Attributes tab, set Rotate Results to “Yes”
  4. Parse the query:
    • pel_id as “Keyword Variable”
    • others as “Field Variables”
  5. Configure variables: pel_id to “Required”
  6. Test query.

Linking Queries Together

Earlier, we created the Pelajar_View_Link field variable which presented the record id for the selected record.  We will now modify this variable to create a link to the View query

  1. Configure the variable, Pelajar_View_Link:
    • In Parameters tab, set Enable Next Query to “Yes”
    • Set Next Query ID, enter the query ID
    • Set Next Query Task to “Execute Query”
    • Set Target Variable to pelajar_id
    • In Special tab, Enter: $replacement = ‘view’;
  2. Hide the view query using the query parameter “Hide On Select Screen”
  3. Test the query.

Comments (2)

How To Install Joomla! In Local Web Server

  1. Download Joomla! from www.joomla.org
  2. Extract the zip file onto the www folder of your web server. Start the server.
  3. Assuming that your Joomla! directory is www\Joomla, enter http://localhost/joomla/index.php in your web browser to start Joomla! installation.
  4. Pre-installation check. Click Next to continue.
  5. License. Click Next to continue.
  6. Step 1. Enter “localhost” as the Host Name and “root” as the MySQL user name. By default, there is no password for your MySQL server, so leave MySQL Password blank. Enter “Joomla” as the MySQL Database Name. Click Next to continue.
    Joomla! installation
  7. Step 2. Type in the name of your web site. Click Next to continue.
  8. Step 3. Enter your e-mail address and leave all other settings to default. Remember the password given. Click Next to Continue.
  9. Step 4. The installation is completed. You can click on “View Site” to take a look at your Joomla! site or click on “Administration” to enter the administration area. Make sure you deleted the Joomla\installation folder in your server for security reasons.

Leave a Comment

$_SESSION and $_SERVER['REQUEST_URI'];

$_SESSION['login_return_url'] = $_SERVER['REQUEST_URI'];

session_register — Register one or more global variables with the current session.

<?php
// Use of session_register() is deprecated
$barney = "A big purple dinosaur.";
session_register("barney");

// Use of $_SESSION is preferred, as of PHP 4.1.0
$_SESSION["zim"] = "An invader from another planet.";

// The old way was to use $HTTP_SESSION_VARS
$HTTP_SESSION_VARS["spongebob"] = "He's got square pants.";
?>

Source: PHP.net

$_SERVER is one example of a predefined variable.

$HTTP_SERVER_VARS [deprecated] — Server and execution environment information

‘REQUEST_URI’ – The URI which was given in order to access this page; for instance, ‘/index.html’.

Source: PHP.net

Leave a Comment

How To Create Component In Joomla!

For example, you want to create a new component called kecik. You have to create two folders:

  • Create com_kecik in joomla/administrator/components for the back end
  • Create com_kecik in joomla/components for the front end

When the component is loaded from the front end, Joomla! will look for kecik.php, while at the back end, it will look for admin.kecik.php. If you want to turn an existing web site into a Joomla! component, the index.php will become kecik.php

More on registering the component in Joomla.. aku malas nak taip ah, here.

Source: PACKT Publishing, Ken Del Castilo

Related: How to Convert an existing Web site to a Joomla! Web site

Leave a Comment

mysql_fetch_assoc

mysql_fetch_assoc — Fetch a result row as an associative array

 

Copied from PHP.net, for personal use

Leave a Comment

isset

isset — Determine whether a variable is set

If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte (“”) is not equivalent to the PHP NULL constant.

If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.

Examples

<?php

$var = ”;

// This will evaluate to TRUE so the text will be printed.
if (isset($var)) {
    echo “This var is set so I will print.”;
}

Copied from PHP.net, for my own use.

Leave a Comment

PHP $_GET

The $_GET variable is used to collect values from a form with method=”get”.

The $_GET Variable

The $_GET variable is an array of variable names and values sent by the HTTP GET method.

The $_GET variable is used to collect values from a form with method=”get”. Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser’s address bar) and it has limits on the amount of information to send (max. 100 characters).

Example

<form action=”welcome.php” method=”get”>
Name: <input type=”text” name=”name” />
Age: <input type=”text” name=”age” />
<input type=”submit” />
</form>

When the user clicks the “Submit” button, the URL sent could look something like this:

http://www.w3schools.com/welcome.php?name=Peter&age=37

The “welcome.php” file can now use the $_GET variable to catch the form data (notice that the names of the form fields will automatically be the ID keys in the $_GET array):

Welcome <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

Note: When using the $_GET variable all variable names and values are displayed in the URL. So this method should not be used when sending passwords or other sensitive information! However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

 

Copied from w3schools, for my own use.

Leave a Comment

Check Empty Field In Forms

In registration forms, usually you would want to mark certain fields as ‘required’. Thus, users should not be able to continue until these fields is filled. Presented are two methods of checking empty fields in forms:

Using PHP:

<?php
session_start();
unset($_SESSION);
$req_fields = array("_name"=>"name", "timestamp"=>"your time", "field"=>"some field", "check"=>"checkbox");
$msg = "&nbsp;";

function check_empty_fields($method = "post") {
    global $req_fields, $msg;
    $use_method = ($method == "post") ? $_POST : $_GET;
    $errors = "";
    $count_empty = 0;
    foreach ($req_fields as $key => $val) {
        $field_name = (array_key_exists($key, $req_fields)) ?  $req_fields[$key] : $key;
        if (!array_key_exists($key, $use_method) || empty($use_method[$key])) {
            $_SESSION[$key] = true;
            $errors .= "|".$field_name;
            $count_empty++;
        } else {
            $_SESSION[$key] = false;
        }
    }
    if ($count_empty == 0) {
        return true;
    } else {
        $msg = "The following (required) fields are empty:";
        $msg_parts = explode("|", ltrim($errors, "|"));
        $num_parts = count($msg_parts);
        $msg .= "<br><b>";
        for ($i = 0; $i < $num_parts; $i++) {
            $msg .= $msg_parts[$i];
            if ($i <= $num_parts - 2) {
                $msg .= ($i == $num_parts - 2) ? " &amp; " : ", ";
            }
        }
        $msg .= "</b>\n";
        return false;
    }
}
if (isset($_POST['submit'])) check_empty_fields();
// and the form field like:
// echo <input type=\"text" name=\"_name\"";
// if (isset($_POST['_name'])) echo " value=\"".$_POST['_name']."\"";
// if (!$_SESSION['_name']) echo " class=\"invalid\">";
?>

Pros: Pretty. File can be separated.

Cons: Server-side. And I can’t seem to make it work, hahah.

Source: finalwebsites

Using Javascript:

<script language="JavaScript" type="text/javascript">

// Javascript validation functions
// http://www.designplace.org/

//function to check empty fields

function isEmpty(strfield1, strfield2, strfield3) {

//change "field1, field2 and field3" to your field names
strfield1 = document.forms[0].field1.value
strfield2 = document.forms[0].field2.value
strfield3 = document.forms[0].field3.value

  //name field
    if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ')
    {
    alert("\"Field 1\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }

  //url field
    if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ')
    {
    alert("\"Field 2\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }

  //title field
    if (strfield3 == "" || strfield3 == null || strfield3.charAt(0) == ' ')
    {
    alert("\"Field 3\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }
    return true;
}

//function to check valid email address
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.forms[0].email.value;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1)
   {
      alert('A valid e-mail address is required.\nPlease amend and retry');
      return false;
    }
    return true;
}

//function that performs all functions, defined in the onsubmit event handler

function check(form)){
if (isEmpty(form.field1)){
  if (isEmpty(form.field2)){
    if (isEmpty(form.field3)){
        if (isValidEmail(form.email)){
          return true;
        }
      }
  }
}
return false;
}

</script>

Pros: Client-side. More importantly, it works. 

Cons: Same long code on every page. And not as pretty T_T

Source: DesignPlace

Leave a Comment

Get Data From MySQL Database

The following code demonstrates getting data from MySQL database:

<?php
include 'config.php';
include 'opendb.php';

$query  = "SELECT NoMatriks, Nama, NamaBapa FROM student";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "No Matriks : {$row['NoMatriks']} <br>" .
         "Nama : {$row['Nama']} <br>" .
         "Nama Bapa : {$row['NamaBapa']} <br><br>";
}

include 'closedb.php';
?>

Source: PHP-MySQL-Tutorial

Method II:

$query="select * from pelajarjmti"; 
$rt=mysql_query($query);         
echo mysql_error();                   

while($nt=mysql_fetch_array($rt)){
echo "$nt[NoMatriks] $nt[NamaPelajar] $nt[NoIC] $nt[Bangsa]<br> ";    

Source: plus2net

Leave a Comment

Older Posts »