Showing posts with label Php. Show all posts
Showing posts with label Php. Show all posts

Install Apache 2 In Linux


Apache is the web server piece of our puzzle. From within your terminal window issue the command:

sudo apt-get install apache2

If, by chance, you are using a distribution that does not use Sudo, you will need su to the root user and issue the above command without the sudo command.
Depending on your OS installation, the above command might need to pick up some dependencies. If so, okay those dependencies. At the end of the installation, Apache should automatically start. If it doesn't, issue the following command:

sudo /etc/init.d/apache2 start

You can now open up a browser and point it to the IP address (or domain) of the server to get the famous "It works!" page. You are ready to move on to PHP.

Php include and require

we are using include and require for add or call other php classes and function in to another file .Something like extending.Therefore we can reuse the previous codes to our new file or function.
Say in java we are using import to get a jar file for extensions similarly we are using include in php.

Include –> This will produce only warnings if we have an error and script will continue (E_COMPILE_ERROR)

<html>
<body>
<?php include 'Needed.php'; ?>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
</body>
</html>

 

Require –> This will produce fatal error then it will stop the script (E_WARNING)

<html>
<body>
<?php require 'Needed.php'; ?>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
</body>
</html>

 



If you want the execution to go on and show users the output, even if the include file is missing, use include. Otherwise, in case of Framework, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

Use of HTML ALT tag in Php

Mostly in web development we don’t need the alt text option for images.Because it can be simply neglect to use by Programmers.But incase if we have an image loading error for a specific server then server has to response the error with specific Id or name to client or a tester.
Simply I can give an example: Say If the user enabled the text-only option on his/her browser because of the slow internet .Then the image can’t be shown there therefore we are using ALT as a best Practice.
we can use alt in many ways as mentioned bellow,


Decorative image or meaning less:If we use the image as bullet,symbols and spacer then we can leave the space as it is
Here we can the leave the ALT text as it is .Simply non-text Because we don’t need to tag a name a for a symbol


Meaning full image:If our image contain any text then we should mention the alt attribute for that image because of the above reasons.Here we can give the ALT name as alt=”fronter-Learning” Therefore we can give some meaning full details to the user

Meaning full and non textual : Incase if It is a photograph of any other informational image Then we better have to give a brief description about that image .
Here we can give the ALT name as description alt=”Containing natural Image with trees” Therefore we can give some meaning full details to the user
Also it has some condition that text should be kept to under 50 characters and should avoid specialized symbols.

Import CSV File To Server And Upload Data As Stream In Mysql Using Php

 
To allow users to upload files from a form can be very useful.This script will allow you to upload files from your browser to your hosting, using PHP. The first thing we need to do is create an HTML form that allows people to choose the file they want to upload.This form sends data to the file "upload.php", which is what we will be creating next to actually upload the file.

  • The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded
 
  • The type="file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field

 

  • Here we are attaching CSV File which can contain data like bellow

Name

Description

Price($)

shipping($)

quantity

Pen Drive

Which made by …

25

1.23

25

paper

Which is made by…

2.5

0

2000

Similarly we can have many number of rowed data.Therefore we should create the Mysql Data base which should contain the table column names as Above.










Index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IndexFile</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" />
</form>
<form enctype="multipart/form-data" action="process.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Select File : <input name="uploadedfile" type="file" />
<input type="submit" value="Process Data" />
</form>
</body>
</html>


Upload.php


<?php
// Where the file is going to be placed 
$target_path = "C:/wamp/www/Release/uploads/";
echo $target_path . basename( $_FILES['uploadedfile']['name']); 
/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). 
" has been uploaded"; 
} else{
echo "There was an error uploading the file, please try again!";
}
header('Location: http://localhost/Release/');
?>


By using the global PHP $_FILES array you can upload files from a client computer to the remote server.


The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error".


 
process.php

<?php
//connect to localhost
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//connect to Database
mysql_select_db("cart", $con);
//File path to CSV
$filePath = "C:/wamp/www/Release/uploads/";
$filePath = $filePath . basename( $_FILES['uploadedfile']['name']); 
echo $filePath;
//read the CSV file to Stream
$row = 1;
if (($handle = fopen("$filePath", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    $row++;
    $name=$data[0];
    $des=$data[1];
    $pri=$data[2];
    $ship=$data[3];
    $quan=$data[4];
//input the red data from CSV in to Database
mysql_query("INSERT INTO products (name,description,price,shipping,quantity)
VALUES ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')");
} 
fclose($handle);
}
//To redirect to index page
header('Location: http://localhost/Release/');
mysql_close($con);
?>



Have Fun With PHP «

Php Tactics

$GLOBALS

$GLOBALS[ index ] is use to store all global variable in an array.it’s use to access the variable out of the specific function.

simply it’s working like java Static variable.Therefore if we want to use  a variable inside of he function we have to use $GLOBALS

CASE I:
<?php
$a = 5;
$b = 10;
function myTest()
{
global $a, $b;
$b = $a + $b;
}
myTest();
echo $b;
?> 
CASE II:
<?php
$a = 5;
$b = 10;
function myTest()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
myTest();
echo $b;
?> 



STATIC
Also we can use static keyword to make the same feature as GLOBALS
static $variable 





 CONCATENATION

usually we are using “ + “ to combine two words. but in php we are using some “ . ” Operator to combine two word.

<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?> 



ARRAYS

Arrays can be define  many ways in php.

CASE i:
$cars=array("Saab","Volvo","BMW","Toyota");
CASE ii:
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
?> 




ASSOCIATIVE ARRAYS

we can pass some stored data with this array set.for example if anyone want to pass the users Id and address to database they can use associative Arrays.

CASE i:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
CASE ii:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
?> 



FOREACH LOOP

According to the array list count we can make a easy for loop with few line of code.Therefore the loop will count the number of index in Array.

<?php
$x=array("one","two","three");
foreach ($x as $value)
  {
  echo $value . "<br />";
  }
?>

It will print the all value that array contain.



GET AND POST METHOD

This get and post method is just a pipe to transfer data to next page or to Database.There is no restriction that get method should use for getting data and post method should for posting data .This get and post depend on what we are defining in HTML forms.  and name of the input.

<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> 
  
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old! 
here if we use form method as post then we have  to call $_POST in php.Just we are changing the name of the Pipe. 
Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.
$_REQUEST can be use for any of the methods get of post . 

Php Repository and Code Editor

Smart Git

We are using smart Git repository based application to Store and review code Based on Version Control.Also it use to switch between multiple repositories .This is the best practice for team based projects

Here we have many function like

->Commit
-> Stage
->Unstage
->Sync
->Pull
->Push

Smart Git – Pull

it’s use to get the files from server to your local machine.If you want to commit

Smart Git – Commit Button (“Commit is only save the changes on your local machine”)

if you want to fix a bug then you have to commit a file through smart Git.When you click on commit then it will ask what that you want to commit description.Also Before performing a commit, you often want to review your or others  changes in order to catch any errors.

Smart Git offers several ways of doing this:

Review mode – Screen switcher  :

  • This mode allow you to review the code changes in each file one by one .
  • compare window used to compare current and past code changes based on commitments.
  • Index editor is basically a combination of two difference.A difference between repository and the Git index 
  • Also if we find any error on our last commit then we can recommit from our last index to restore.
  • ADD is use to do further changes by selecting commit .

Smart Git – Push

After finishing the Changes if you want to upload the files to server you can access that through Push button.

Smart Git – Log

Inspecting the History :

Clicking by the Log button on the main dock we can view the history of our or others  past and present changes on specific file .

Zend Development Environment

This is a Php Editor use to Debug Php code and compare files through version Control.Which has debug,code review option like visual studio.

Conclusion :

Smart Git and Zend both application are integrated with each other therefore we can do version control through smart and editing through Zend.