Saturday, 12 December 2015

Searching and Sorting of The Data Using MySQL and PHP

Searching Techniques and Sorting Techniques Using MySQL

Prepared By Hitesh Vataliya

Searching is the process of selecting one record or more records from a table or combination of tables and showing as the result into PHP form using MySQL Query.  


To perform the query, three items must be identified:
1. Which fields will be used to identify the records required?
2. Which tables are required to produce the data required?
3. What are the criteria for identifying the records required?

For Example : 
Member is tablename.
Membernumber, forename,surname,birthdate,gender,address,pincode,telephonenumber are fields of table member.

Question 1: 
Identify the names and telephone numbers of club members with Member Number over 1000

Answer: 
Forename, surname and telephone number are selected from table member

SELECT FORENAME, SURNAME, `TELEPHONE NUMBER` FROM MEMBER WHERE `MEMBER NUMBER` > 1000

Question 2:
Select All the fields or columns of the table member.
Answer: 
The asterisk * symbol can be used in an expression string to extract all fields from a table of the database. 

SELECT * FROM MEMBER WHERE `MEMBER NUMBER` > 1000

Question 3: 
Identify the names and telephone numbers of club members with Surnames beginning with M and Member Number over 1000 
Answer : 
SELECT FORENAME, SURNAME, `TELEPHONE NUMBER` FROM MEMBER WHERE SURNAME LIKE “M*” AND `MEMBER NUMBER` > 1000

Question 4: 
Identity the names and telephone numbers of the club members with name begin with N and 3rd character is R and member number>1000.
Answer : 
SELECT FORENAME, SURNAME, `TELEPHONE NUMBER` FROM MEMBER WHERE FORENAME LIKE “N_R*” AND `MEMBER NUMBER` > 1000

Sorting
Sorting in MySQL performed by keyword order by and then asc or desc according to the requirement we can select either asc for ascending order and desc for descending order sorting of the data.

To perform a sort, two items must be identified:
1. Which field (or fields) will be used to decide the order of records?
2. For each field selected, will the order of sorting be ascending or descending?

To produce a list of people with the tallest first, the records would be sorted in descending order of height.  
To produce a list of people with youngest first, the records would be sorted in ascending order of age.
A very common way of ordering records relating to people is in alphabetical order.  To achieve alphabetical ordering requires the records to be sorted in ascending order of surname. 
A complex sort involves more than one sort condition involving two or more fields.
To achieve “telephone book order”, the name is sorted in ascending order of surname, then ascending order of forename.  In this case, the Surname field is the primary sort key, and the Forename field is the secondary sort key.  

For Example :
Question 1: 
Sort Selected Data in Ascending order. 
Answer :
SELECT FORENAME, SURNAME FROM MEMBER 
WHERE `MEMBER NUMBER` > 1000 ORDER BY SURNAME, FORENAME

Question 2: 
Sort Selected Data in Descending order. 
Answer :
SELECT FORENAME, SURNAME FROM MEMBER WHERE `MEMBER NUMBER` > 1000 ORDER BY `MEMBER NUMBER` DESC


For More Information : Website

Thursday, 10 December 2015

XML file access with PHP

What is XML? How to Access the Content Written in XML with PHP Language Code?

Prepared By Hitesh Vataliya


The Full form of XML is Extensible Markup Language
The XML is a widely accepted standard for data description and exchange.
It allows content authors to “mark up” their data with customized machine-readable tags. 
These tags don’t come from a predefined list; instead, create their own tags and structure, suited to their own particular requirements, as a way to increase flexibility and usability.
Every XML document must begin with a declaration that states the version of XML being used.
The document prolog is followed by a nested series of elements.
Elements are the basic units of XML;
They typically consist of a pair of opening and closing tags that enclose some textual content.
The textual data enclosed within elements is known, in XML parlance, as character data. This character data can consist of strings, numbers, and special characters.
Some Rules angle brackets and ampersands
 > Symbol  > is used
 <  Symbol  < is used
 &  Symbol  & is used


XML is the latest standard language used to store and transfer the database among websites or we can say it is a standard language used in webservices.

Here in below Example, XML file given for accessing it's content (address.xml file) using php code :

address.xml


<?xml version='1.0'?>
<address>
<street>168, New Road</street>
<county>Baroda</county>
<city>
<name>Vadodara</name>
<zip>390021</zip>
</city>
<country>India</country>
</address>


PHP Code

<?php
// load XML file
$xml = simplexml_load_file('address.xml') or die ("Unable to load XML!");
// access XML data
echo "City: " . $xml->city->name . "\n";
echo "Postal code: " . $xml->city->zip . "\n";
?>


If you wants to learn more concepts of XML and PHP Visit : our website

 




Tuesday, 8 December 2015

Creating & Managing Notepad File Using PHP Code

File Handling in PHP

Using the Built in Functions like,
  1. fopen(),
  2. fclose(),
  3. feof(),
  4. realpath(),
  5. dirname(),
  6. basename(), etc.


fopen() : Open a file with PHP Language, fopen() function is required to create connection between our PHP Coding file and Other file like Notepad file or we can say .txt file.

It works same as other languages built-in functions works like C Programming Language or C++ Programming Language.

Syntax of Function fopen()

fopen(filename,mode)


For Example

$file1= fopen("Hitesh.txt", "w");

We can open a file in different modes as given below





Writing the Content on to the File.

If you have a version of PHP below version 5, then you can use the fwrite() function. But you first need to use fopen( ) to get a file handle.
In the next script, we'll try to write some text to a file. We'll use the "w" option, as this will create a file for us, if we don't have one with the filename chosen.
<?PHP
$file = fopen("Hitesh.txt", "w");
fwrite($file, "Hi, This is file Handling Program of PHP");
fclose($file);
print "file created and one line of text is written inside Hitesh.txt";
?>
In Latest Version of 5 of Above version of PHP, We can use file_put_contents() Function
It is used in the same way, but has an optional third parameter:
file_put_contents($file_handle, $file_contents, context);
The context option can be FILE_USE_INCLUDE_PATH, FILE_APPEND, LOCK_EX. 
So to append to the file, just do this:
file_put_contents($file_handle, $file_contents, FILE_APPEND);

  
Reading From the File

<?PHP
$file_contents = fopen( "Hitesh.txt", "r" );
print $file_contents;
fclose($file_contents);
?>
Here Print $file_content will print all the content written in the file.
Same way we can write or print the content of the file by using fgets() function which reads content line by line.

<?PHP
$file = fopen("Hitesh.txt", "r");
while (!feof($file)) {
$line_of_text = fgets($file);
print $line_of_text . "<BR>";
}
fclose($file);
?>

fclose() function is used to close connection between PHP code or Hitesh.txt File.
Fopen() function is used for creating connection between PHP coding and Hitesh.txt file, and if you have created connection then your responsibility to close it after reading or writing work is completed.




Some Useful Functions for Printing FileName, FilePath and Directory on which file is there.

Create a file with filename “Hitesh.php” and Store it on to the folder “PHP1”

Print the Path of a File By Using realpath() function
<?PHP
$filepath = realpath("Hitesh.php");
print "The path of the File Hitesh.php is: " . $filepath;
?>
If You wants to get the exact path of file, then, you can use realpath(). In between the round brackets of the function, type the name of the file which is created by you Here in above example Hitesh.php is the file name and $filepath variable prints the name of the file.

Print the Folder or Directory Name, but not the file name
<?PHP
$f = dirname("PHP/Hitesh.php");
print "The folder or directory name is: " . $f . "<BR>";
?>
You can get the names of the folders, you can use the dirname( ) function. This will strip off the name of the file and return the rest of the text between the round brackets of the function.

Print the Filename By Using basename() function
<?PHP
$v = basename("PHP/Hitesh.php");
print "The File Name is: " . $v . "<BR>";
?>
In above example the $v prints the file name “Hitesh.php”.


For learning PHP PROJECT TRAINING in  VADODARA, GUJARAT visit website : WWW
Vataliyatuitionclasses.com