Friday, April 28, 2017
Wednesday, March 8, 2017
GROUP_CONCAT MAX LENGHT
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD','root');
define('DB_DATABASE', 'test');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$access ='PROGRAMMER';
$result = mysqli_query($db,"SET SESSION group_concat_max_len = 100000000") or die(mysqli_error($db));
$sql ="SELECT group_concat(concat('\'',REPLACE(code,'-',''),'\'')) code
FROM (select code from expense_acct
) mydata";
$result = mysqli_query($db,$sql) or die(mysqli_error($db));
$row = mysqli_fetch_array($result);
echo $row['code'];
?>
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD','root');
define('DB_DATABASE', 'test');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$access ='PROGRAMMER';
$result = mysqli_query($db,"SET SESSION group_concat_max_len = 100000000") or die(mysqli_error($db));
$sql ="SELECT group_concat(concat('\'',REPLACE(code,'-',''),'\'')) code
FROM (select code from expense_acct
) mydata";
$result = mysqli_query($db,$sql) or die(mysqli_error($db));
$row = mysqli_fetch_array($result);
echo $row['code'];
?>
Monday, March 6, 2017
Remove all script elements using JS
var r = document.getElementsByTagName('script');
for (var i = (r.length-1); i >= 0; i--) {
if(r[i].getAttribute('id') != 'a'){
r[i].parentNode.removeChild(r[i]);
}
}
for (var i = (r.length-1); i >= 0; i--) {
if(r[i].getAttribute('id') != 'a'){
r[i].parentNode.removeChild(r[i]);
}
}
Wednesday, February 22, 2017
SHOW HIDE DIV
<div id='content'>Hello World</div>
<input type='button' id='hideshow' value='hide/show'>
jQuery:
jQuery(document).ready(function(){
jQuery('#hideshow').live('click', function(event) {
jQuery('#content').toggle('show');
});
});
For versions of jQuery 1.7 and newer use
jQuery(document).ready(function(){
jQuery('#hideshow').on('click', function(event) {
jQuery('#content').toggle('show');
});
});
Thursday, February 16, 2017
Get Time Ago in PHP
//timeAgo Function
public function timeAgo($time_ago){
$time_ago = strtotime($time_ago);
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
return "just now";
}
//Minutes
else if($minutes <=60){
if($minutes==1){
return "one minute ago";
}
else{
return "$minutes minutes ago";
}
}
//Hours
else if($hours <=24){
if($hours==1){
return "an hour ago";
}else{
return "$hours hrs ago";
}
}
//Days
else if($days <= 7){
if($days==1){
return "yesterday";
}else{
return "$days days ago";
}
}
//Weeks
else if($weeks <= 4.3){
if($weeks==1){
return "a week ago";
}else{
return "$weeks weeks ago";
}
}
//Months
else if($months <=12){
if($months==1){
return "a month ago";
}else{
return "$months months ago";
}
}
//Years
else{
if($years==1){
return "one year ago";
}else{
return "$years years ago";
}
}
}
Wednesday, February 15, 2017
Ignore case insensitive in php
<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>
Thursday, February 9, 2017
Prevent session from expiring in ajax
PHP: index.php
<?phpsession_start();
$_SESSION['username'] = "Raymond";
$_SESSION['status'] = "Active";
?>
<html>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<body>
<form method="post">
<?php echo $_SESSION['status']; ?>
<input type="submit" value="submit"/>
</form>
</body>
</html>
<script>
//REFRESH SESSION
setInterval(function()
{
$.ajax({
url:'refresh_session.php',
success:function(response){
alert(response);
}
});
}, 6000);//time in milliseconds
</script>
PHP: refresh_session.php
<?php
session_start();
// store session data
if (isset($_SESSION['username'])) $_SESSION['username'] = $_SESSION['username'];
if (isset($_SESSION['status'])) $_SESSION['status'] = 'inactive';
echo $_SESSION['status'];
?>
Thursday, February 2, 2017
Extract table from html to excel
<?php
session_start();
ob_start();
if(isset($_POST['EXTRACT'])){
$file = 'outpputfile.xls';
}
?>
<html>
<body>
<form action="" method="post">
<!-- some body here... -->
<!--------------------------------------------------------------------------------------- -->
<table width="700" border="1" cellpadding="0">
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>4</td>
<td>4</td>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>5</td>
<td>5</td>
<td>5</td>
<td>5</td>
</tr>
</table>
<!--------------------------------------------------------------------------------------- -->
<?php
if(isset($_POST['EXTRACT'])){
$content = ob_get_contents();
ob_end_clean();
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); header("Content-type: application/vnd.ms-excel;charset:UTF-8");
header('Content-length: '.strlen($content));
header('Content-disposition: attachment; filename='.basename($file));
echo $content;
exit;
}
?>
<input type="submit" name="EXTRACT" value="EXTRACT TO EXCEL" class="buttons"></td>
<!-- end of some body here... -->
</form>
</body>
</html>
session_start();
ob_start();
if(isset($_POST['EXTRACT'])){
$file = 'outpputfile.xls';
}
?>
<html>
<body>
<form action="" method="post">
<!-- some body here... -->
<!--------------------------------------------------------------------------------------- -->
<table width="700" border="1" cellpadding="0">
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>4</td>
<td>4</td>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>5</td>
<td>5</td>
<td>5</td>
<td>5</td>
</tr>
</table>
<!--------------------------------------------------------------------------------------- -->
<?php
if(isset($_POST['EXTRACT'])){
$content = ob_get_contents();
ob_end_clean();
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); header("Content-type: application/vnd.ms-excel;charset:UTF-8");
header('Content-length: '.strlen($content));
header('Content-disposition: attachment; filename='.basename($file));
echo $content;
exit;
}
?>
<input type="submit" name="EXTRACT" value="EXTRACT TO EXCEL" class="buttons"></td>
<!-- end of some body here... -->
</form>
</body>
</html>
Date - Date in Javascript
HTML
<form method="post" name="fillup">
<input type="text" placeholder="yyyy/mm/dd" name="filing_deadline" onchange="getStatus()">
<input type="text" placeholder="yyyy/mm/dd" name="actual_date_of_filling" onchange="getStatus()" >
<input type="text" readonly name="filing_status" onchange="getStatus()">
</form>
JAVASCRIPT
<script type="text/javascript" >
function getStatus() {
var actual = new Date(document.fillup.actual_date_of_filling.value);
var deadline = new Date(document.fillup.filing_deadline.value);
var timeDiff = deadline.getTime() - actual.getTime();
var DaysDiff = timeDiff / (1000 * 3600 * 24);
var stat="";
if(DaysDiff==0){
stat="Within the deadline";
}else if(DaysDiff>0){
if(DaysDiff>1) stat=DaysDiff+" Days Advance";
else stat=DaysDiff+" Day Advance";
}else if(DaysDiff<0){
if(Math.abs(DaysDiff)>1) stat=Math.abs(DaysDiff)+" Days Late";
else stat=Math.abs(DaysDiff)+" Day Late";
}else{
stat="";
}
document.fillup.filing_status.value=stat;
}
<script>
Monday, January 30, 2017
TIME AGO in PHP
<?php
//DATE FORMAT
//echo date("F d Y H:i:s");
// TESTING
function test($ts){
echo time_passed($ts) . '<br />';
}
test(time());
test(strtotime("January 30 2017 07:41:08"));
test(time()-(60*17));
test(time()-((60*60*2) + (60*55)));
test(time()-(60*60*3+60));
test(strtotime('-1 day'));
test(strtotime('-13 days'));
test(strtotime('January 30 2017 07:31:08'));
test(strtotime('July 23 2016'));
test(strtotime('November 18 1999'));
test(strtotime('March 1 1937'));
//TESTING- end
//FUNCTION
function time_passed($timestamp){
//type cast, current time, difference in timestamps
$timestamp = (int) $timestamp;
$current_time = time();
$diff = $current_time - $timestamp;
//intervals in seconds
$intervals = array (
'year' => 31556926, 'month' => 2629744, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'minute'=> 60
);
//now we just find the difference
if ($diff == 0)
{
return 'just now';
}
if ($diff < 60)
{
return $diff == 1 ? $diff . ' second ago' : $diff . ' seconds ago';
}
if ($diff >= 60 && $diff < $intervals['hour'])
{
$diff = floor($diff/$intervals['minute']);
return $diff == 1 ? $diff . ' minute ago' : $diff . ' minutes ago';
}
if ($diff >= $intervals['hour'] && $diff < $intervals['day'])
{
$diff = floor($diff/$intervals['hour']);
return $diff == 1 ? $diff . ' hour ago' : $diff . ' hours ago';
}
if ($diff >= $intervals['day'] && $diff < $intervals['week'])
{
$diff = floor($diff/$intervals['day']);
return $diff == 1 ? $diff . ' day ago' : $diff . ' days ago';
}
if ($diff >= $intervals['week'] && $diff < $intervals['month'])
{
$diff = floor($diff/$intervals['week']);
return $diff == 1 ? $diff . ' week ago' : $diff . ' weeks ago';
}
if ($diff >= $intervals['month'] && $diff < $intervals['year'])
{
$diff = floor($diff/$intervals['month']);
return $diff == 1 ? $diff . ' month ago' : $diff . ' months ago';
}
if ($diff >= $intervals['year'])
{
$diff = floor($diff/$intervals['year']);
return $diff == 1 ? $diff . ' year ago' : $diff . ' years ago';
}
}
?>
//DATE FORMAT
//echo date("F d Y H:i:s");
// TESTING
function test($ts){
echo time_passed($ts) . '<br />';
}
test(time());
test(strtotime("January 30 2017 07:41:08"));
test(time()-(60*17));
test(time()-((60*60*2) + (60*55)));
test(time()-(60*60*3+60));
test(strtotime('-1 day'));
test(strtotime('-13 days'));
test(strtotime('January 30 2017 07:31:08'));
test(strtotime('July 23 2016'));
test(strtotime('November 18 1999'));
test(strtotime('March 1 1937'));
//TESTING- end
//FUNCTION
function time_passed($timestamp){
//type cast, current time, difference in timestamps
$timestamp = (int) $timestamp;
$current_time = time();
$diff = $current_time - $timestamp;
//intervals in seconds
$intervals = array (
'year' => 31556926, 'month' => 2629744, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'minute'=> 60
);
//now we just find the difference
if ($diff == 0)
{
return 'just now';
}
if ($diff < 60)
{
return $diff == 1 ? $diff . ' second ago' : $diff . ' seconds ago';
}
if ($diff >= 60 && $diff < $intervals['hour'])
{
$diff = floor($diff/$intervals['minute']);
return $diff == 1 ? $diff . ' minute ago' : $diff . ' minutes ago';
}
if ($diff >= $intervals['hour'] && $diff < $intervals['day'])
{
$diff = floor($diff/$intervals['hour']);
return $diff == 1 ? $diff . ' hour ago' : $diff . ' hours ago';
}
if ($diff >= $intervals['day'] && $diff < $intervals['week'])
{
$diff = floor($diff/$intervals['day']);
return $diff == 1 ? $diff . ' day ago' : $diff . ' days ago';
}
if ($diff >= $intervals['week'] && $diff < $intervals['month'])
{
$diff = floor($diff/$intervals['week']);
return $diff == 1 ? $diff . ' week ago' : $diff . ' weeks ago';
}
if ($diff >= $intervals['month'] && $diff < $intervals['year'])
{
$diff = floor($diff/$intervals['month']);
return $diff == 1 ? $diff . ' month ago' : $diff . ' months ago';
}
if ($diff >= $intervals['year'])
{
$diff = floor($diff/$intervals['year']);
return $diff == 1 ? $diff . ' year ago' : $diff . ' years ago';
}
}
?>
Sunday, January 29, 2017
GET CURRENT DATE TIME IN PHP
function date_time_generated(){
return 'Date and Time Generated : ' . date("m/d/Y H:i:s",time()+8*60*60 );
}
return 'Date and Time Generated : ' . date("m/d/Y H:i:s",time()+8*60*60 );
}
Time ago in PHP
function get_timeago( $ptime )
{
$estimate_time = time() - $ptime;
if( $estimate_time < 1 )
{
return 'less than 1 second ago';
}
$condition = array(
12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach( $condition as $secs => $str )
{
$d = $estimate_time / $secs;
if( $d >= 1 )
{
$r = round( $d );
return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
}
}
}
$timeago=get_timeago(strtotime('2017/01/30 1:57'));
echo $timeago;
format ‘YYYY-MM-DD H:M:S’
{
$estimate_time = time() - $ptime;
if( $estimate_time < 1 )
{
return 'less than 1 second ago';
}
$condition = array(
12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach( $condition as $secs => $str )
{
$d = $estimate_time / $secs;
if( $d >= 1 )
{
$r = round( $d );
return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
}
}
}
$timeago=get_timeago(strtotime('2017/01/30 1:57'));
echo $timeago;
format ‘YYYY-MM-DD H:M:S’
Friday, January 27, 2017
DELETE ROW WITHOUT REFRESHING PAGE IN PHP
<tr><a href="" id="'.$row['rec_id'].'" class="delbutton"><b>Delete</b></a></tr>
<script type="text/javascript" >
$(function() {
$(".delbutton").click(function() {
var del_id = $(this).attr("id");
var info = 'id=' + del_id;
var thisTableRow = $(this).closest('tr');
if (confirm("Sure you want to delete this post? This cannot be undone later.")) {
$.ajax({
type : "POST",
url : "delete_pdf.php", //URL to the delete php script
data : info,
success : function() {
alert("PDF Successfully deleted!");
$(thisTableRow).hide("fast");
}
});
}
return false;
});
});
</script>
delete_pdf.php
<?php
require_once('mysql_connect.php');
$id=$_POST['id'];
$delete = "DELETE FROM uploaded_tax_files WHERE rec_id='$id'";
$result = mysql_query($delete) or die(mysql_error());
?>
Monday, January 23, 2017
USE PHP INSIDE CSS
filename : style.php
<?php
header("Content-type: text/css");
$color1 = "#000";
$color2 = "#fff";
?>
body {
background-color: <?php echo $color1; ?>;
}
h1 {
color: <?php echo $color2; ?>;
}
Include your dynamic css something like this:
filename : index.php
<link href="styles.php" media="screen" rel="stylesheet" type="text/css" />
Friday, January 20, 2017
AUTO ADJUST IFRAME ACCORDING TO THE SIZE OF CONTENT
1 . Paste inside <head>
<script>
function autoSizeIframe(obj) {
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
obj.style.width = obj.contentWindow.document.body.scrollWidth + 'px';
}
</script>
2.add this to your code
<iframe src="your_link.php" frameborder="0" scrolling="no" onload="autoSizeIframe(this)" />
<script>
function autoSizeIframe(obj) {
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
obj.style.width = obj.contentWindow.document.body.scrollWidth + 'px';
}
</script>
2.add this to your code
<iframe src="your_link.php" frameborder="0" scrolling="no" onload="autoSizeIframe(this)" />
Wednesday, January 18, 2017
Create Database
<?php
$servername = "localhost";
$username = "root";//username
$password = "root";//password
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE databasename";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
$servername = "localhost";
$username = "root";//username
$password = "root";//password
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE databasename";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
PHP Definition
What is PHP?
PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
PHP: (Hypertext Preprocessor) is a scripting language that is especially u for web development and can be embedded into HTML.
Subscribe to:
Posts (Atom)