How to get the full URL of page using PHP

Here is the block of code which will give the full url of the page:


$page_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Advertisements

Using more than one array in loop in PHP

Usually we use foreach loop when there is a need to use only one array. But what if there are more than one array…

Foreach loop works only with a single array. To step through multiple arrays it’s better to use the each() function in a while loop. Have a look at it..


while(($code = each($codes)) && ($name = each($names))) {
echo '<option value="' . $code['value'] . '">' . $name['value'] . '</option>';
}

each() returns information about the current key and value of the array and increments the internal pointer by one, or returns false if it has reached the end of the array. This code would not be dependent upon the two arrays having identical keys or having the same sort of elements. The loop terminates when one of the two arrays is finished.

Difference between == and === operator in PHP

When you want to compare values in PHP then you can use either == or === operator. But these both operator are not same. It’s good to know the difference between them.

These both operators fall under the category of Comparison Operators. The first operator ‘==’ is known as Equal Operator in PHP and checks for the equal values and the second operator ‘===’ is known as Identical Operator and checks for equal values and it also checks that if both the values are of same variable type or not.

How to remove index.php from url in CodeIgniter

Yes, you can remove the index.php from your url in your CodeIgniter web application.

You just need to add a .htaccess file in your web application folder.

Create a file and give it name .htaccess and save it in the root folder, and then copy the below block of code and paste it in the .htaccess file.


<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?/$1 [L]

</IfModule>

One more change you have to do in your config.php file.

Go to :

application/config/config.php

and remove index.php from $config[‘index_page’]

All Done. Enjoy!

How to know CodeIgniter Version

To know the current version of CodeIgniter you are using, you just need to echo a constant.


<?php echo CI_VERSION; ?>

Another way to know the version of CodeIgniter is to check it manually in the file where the constant CI_VERSION is declared.

The constant CI_VERSION is declared in :

system/core/CodeIgniter.php

You can check it there as well if you don’t prefer or like the first option.

 

Using SQL IN Operator with PHP Array

Using SQL IN operator with PHP array

Suppose we have a array named ‘id_array’ which contains id’s. Now, in order to get all the records from our MySQL table using this array of id, we can use the below given code :


SELECT * FROM TABLE WHERE id IN (".implode(',',$id_array).")

Backup MySQL Database Using PHP

Here is the full script to create a backup of your MySQL database using PHP :

backup_tables('localhost','username','password','database');

/* backup the db OR just a table */

function backup_tables($host,$user,$pass,$name,$tables = '*'){

$link = mysql_connect($host,$user,$pass);

mysql_select_db($name,$link);

//get all of the tables

if($tables == '*') {

$tables = array();

$result = mysql_query('SHOW TABLES');

while($row = mysql_fetch_row($result)) {

$tables[] = $row[0];

}

}

else {

$tables = is_array($tables) ? $tables : explode(',',$tables);

}

//cycle through

foreach($tables as $table) {

$result = mysql_query('SELECT * FROM '.$table);

$num_fields = mysql_num_fields($result);

$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));

$return.= "\n\n".$row2[1].";\n\n";

for ($i = 0; $i < $num_fields; $i++) {

while($row = mysql_fetch_row($result)) {

$return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j < $num_fields; $j++) {

$row[$j] = addslashes($row[$j]);

$row[$j] = ereg_replace("\n","\\n",$row[$j]);

if (isset($row[$j])) {

$return.= '"'.$row[$j].'"' ;

}

else {

$return.= '""';

}

if ($j < ($num_fields-1)) {

$return.= ',';

}

}

$return.= ");\n";

}

}

$return.="\n\n\n";

}

//save file

$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

fwrite($handle,$return);

fclose($handle);

}

How to upload multiple files in CodeIgniter

View : (Save it with name upload.php in your views folder)


<?php echo form_open_multipart('upload/do_upload'); ?>

<?php echo $this->session->userdata('msg');

$this->session->set_userdata('msg', ''); ?>

<input type="file" name="file[]" id="file[]" required multiple="multiple">

<button type="submit" class="btn-input" name="submit" id="submit">Submit</button>

<?php echo form_close(); ?>

Controller : (Save it with name Upload.php in your controller folder)


public function do_upload() {

$filesCount = count($_FILES['file']['name']);

$uploadPath = 'uploads/';   /* uploads folder must be located in your root folder (along with application folder) */

if(!is_dir($uploadPath)) {

mkdir($uploadPath,0755,TRUE);

}

for($i = 0; $i < $filesCount; $i++){

$_FILES['userFile']['name'] = $_FILES['file']['name'][$i];

$_FILES['userFile']['type'] = $_FILES['file']['type'][$i];

$_FILES['userFile']['tmp_name'] = $_FILES['file']['tmp_name'][$i];

$_FILES['userFile']['error'] = $_FILES['file']['error'][$i];

$_FILES['userFile']['size'] = $_FILES['file']['size'][$i];

$config['upload_path'] = $uploadPath;

$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp|doc|pdf|docx|rtf';

$this->load->library('upload', $config);

$this->upload->initialize($config);

if($this->upload->do_upload('userFile')){

$fileData = $this->upload->data();

$uploadData[$i]['file_name'] = $fileData['file_name'];

$uploadData[$i]['created'] = date("Y-m-d H:i:s");

$uploadData[$i]['modified'] = date("Y-m-d H:i:s");

}

}

if(!empty($uploadData)){

$this->session->set_userdata('msg', 'Files Uploaded Successfully');

$this->session->set_userdata('status', 'success');

$this->load->view('upload', $uploadData);

}

else {

$this->session->set_userdata('msg', $this->upload->display_errors());

$this->session->set_userdata('status', 'fail');

$this->load->view('upload');

}

 

How to send email with an attachment in PHP using PHPMailer

Firstly, download the PHPMailer library from the given link :

https://github.com/PHPMailer/PHPMailer

Now extract the zip folder and rename it to ‘PHPMailer’ and place it in your root folder where all the php files are located.

Then after put this code :


require_once('PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = ''; /* put the email from which the email is to be sent */
$mail->FromName = ''; /* put the name of a person who is sending email */
$mail->Subject = ''; /* give a subject of email if you want */
$message_body = "Hello, sending mail using PHPMailer";
$mail->Body = $MESSAGE_BODY;
$mail->AddAddress( '' ); /* put the email of the recipient */

$mail->AddAttachment('PATHTOFILE'); /* put the path to the file */

if ($mail->Send())
{
echo "Mail Sent";
}
else
{
echo "Could not send mail";
}