Skip to content

Instantly share code, notes, and snippets.

@mustafix
Last active March 13, 2019 09:39
Show Gist options
  • Save mustafix/1f95f972c3a323928df19fbc2b0a369c to your computer and use it in GitHub Desktop.
Save mustafix/1f95f972c3a323928df19fbc2b0a369c to your computer and use it in GitHub Desktop.
----------------------------------------------------------------------------------------------
Database Connection
----------------------------------------------------------------------------------------------
// Error Reporting Turn On
ini_set('error_reporting', E_ALL);
//error_reporting('0');
// Setting up the time zone
date_default_timezone_set('Asia/Dhaka');
// Defining base url
define("BASE_URL", "http://localhost/2018/pike-admin/");
try{
//$dbf = new PDO ("mysql: host={$host}; dbname={$db_name}", $db_user_name, $db_password );
$dbf = new PDO ("mysql: host=localhost; dbname=pike-admin", 'root', '');
$dbf -> setAttribute(PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);
}
catch (PDOException $e){
echo "Connection Error:".$e->getMessage();
}
----------------------------------------------------------------------------------------------------
SESSION START:
----------------------------------------------------------------------------------------------------
ob_start();
session_start();
if($_SESSION['name'] != 'login'){
header('location:login.php');
}
----------------------------------------------------------------------------------------------------
Excerpt:
----------------------------------------------------------------------------------------------------
$excerpt = explode(" ", $row['post_description']);
$final_words = implode(" ", array_splice($excerpt, 0, 250));
----------------------------------------------------------------------------------------------------
Showing Gravatar Image
----------------------------------------------------------------------------------------------------
<?php
$gravatarMd5 = md5($row['c_email']);
//$gravatarMd5 = ""; // when no gravatar is found
?>
<img src="http://www.gravatar.com/avatar/<?php echo $gravatarMd5; ?>">
----------------------------------------------------------------------------------------------------
Date:
----------------------------------------------------------------------------------------------------
$date = date('Y-m-d');
$time = strtotime('H:i:s');
----------------------------------------------------------------------------------------------------
preg_match:
----------------------------------------------------------------------------------------------------
if(!(preg_match("/^(Mr|Mrs|Dr|Md)\.\ /", $name))){
echo "<span class='error'>Please enter a vaild name</span><br />";
}
if(!(preg_match("/^[A-Za-z]{1}[A-Za-z0-9]{5,12}$/",$u_name))){
echo "<span class='error'>Please Enter a valid user name</span><br />";
}
if(!(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email))){
throw new Exception("<div class='error'>".'Please enter a vaild email'."</div>");
}
----------------------------------------------------------------------------------------------------
PDO Insert Query:
----------------------------------------------------------------------------------------------------
$statement = $dbf->prepare("insert into st_table (st_name, st_roll, st_email) values (?,?,?)");
$statement->execute(array($name, $roll, $email));
----------------------------------------------------------------------------------------------------
PDO Update Query:
----------------------------------------------------------------------------------------------------
$statement = $dbf->prepare("update st_table set st_name=? where st_id =? ");
$statement->execute(array($name, $id));
----------------------------------------------------------------------------------------------------
PDO Delete Query:
----------------------------------------------------------------------------------------------------
$statement = $dbf->prepare("delete from st_table where st_id=?");
$statement->execute(array($id));
// select distinct (name/age/roll) from table;
// select * from table limit 2/3/4;
// select * from table where name like "m%";
// select * from table where age between 9 and 99;
----------------------------------------------------------------------------------------------------
Search Query( 0/1 )
----------------------------------------------------------------------------------------------------
$num = 0;
$result =$dbf->prepare("select * from login_table where username =? and password=? ");
$result ->execute(array($user_input_username,$md5_pass));
$num = $result -> rowCount();
if($num > 0){
// Statement;
}
----------------------------------------------------------------------------------------------------
Show Query
----------------------------------------------------------------------------------------------------
$statement = $dbf->prepare("select * from st_table order by post_id DESC");
$statement->execute(array());
$value = $statement->rowCount();
$result = $statement->fetchAll(PDO :: FETCH_ASSOC);
foreach($result as $row)
{
$row['st_id'];
}
----------------------------------------------------------------------------------------------------
Auto increment
----------------------------------------------------------------------------------------------------
$statement = $dbf->prepare("show table status like 'tbl_post'");
$statement->execute(array());
$result = $statement->fetchAll();
foreach($result as $row)
{
$auto_increment_value = $row[10];
}
$uploade_file = $_FILES['u_file']['name'];
$file_basename = substr($uploade_file, 0, strripos($uploade_file, '.'));
$file_extension = substr($uploade_file, strripos($uploade_file, '.'));
$new_file_name = $auto_increment_value.$file_extension; //strtotime(date('Y-m-d H:i:s')).$file_extension;
// Uploading file type/formate code here
// Uploading file size code here
move_uploaded_file($_FILES['u_file']['tmp_name'], 'upload/'.$new_file_name);
----------------------------------------------------------------------------------------------------
Checking uploading file type/formate ( png/ jpg/ txt/ docx/ html etc)
----------------------------------------------------------------------------------------------------
if( ($file_extension!='.png') && ($file_extension!='.JPG') && ($file_extension!='.PNG') && ($file_extension!='.jpg') ){
throw new exception ('This file can not be upload, You have must be uploaded /"PNG or JPG or docx or txt Files" file');
}
----------------------------------------------------------------------------------------------------
Checking uploading file size ( KB/ MB/ GB)
----------------------------------------------------------------------------------------------------
$filesize = filesize($_FILES['u_file']['tmp_name'])*.0009765625; // byte to kb
if($filesize > 1){
throw new exception ('you can maximum uploade 1kb image size');
}
$filesize = (filesize($_FILES['u_file']['tmp_name'])*.0009765625)*.0009765625; // byte to mb
if($filesize > 1){
throw new exception ('you can maximum uploade 1MB image size');
}
$filesize = ((filesize($_FILES['u_file']['tmp_name'])*.0009765625)*.0009765625)*.0009765625; // byte to GB
if($filesize > 1){
throw new exception ('you can maximum uploade 1GB image size');
}
----------------------------------------------------------------------------------------------------
Image er width and height er jonno
----------------------------------------------------------------------------------------------------
list($width, $height, $type, $attr)= getimagesize($_FILES['file1']['tmp_name']);
echo "width of image : ".$width.'<br>';
echo "height of image : ".$height.'<br>';
if(($width != 300) && ($height != 300)){
throw new Exception ('You must upload your image 300px * 300px');
}
----------------------------------------------------------------------------------------------------
Pagination With PHP
----------------------------------------------------------------------------------------------------
/* ===================== Pagination Code Starts ================== */
$adjacents = 7;
$statement = $dbf->prepare("SELECT * FROM tbl_post ORDER BY post_id DESC");
$statement->execute();
$total_pages = $statement->rowCount();
$targetpage = $_SERVER['PHP_SELF']; //your file name (the name of this file)
$post_per_page = 4; //how many items to show per page
$page = @$_GET['page'];
if($page)
$start = ($page - 1) * $post_per_page; //first item to display on this page
else
$start = 0;
$statement = $dbf->prepare("SELECT * FROM tbl_post ORDER BY post_id DESC LIMIT $start, $post_per_page");
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$post_per_page); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">&#171; previous</a>";
else
$pagination.= "<span class=\"disabled\">&#171; previous</span>";
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next &#187;</a>";
else
$pagination.= "<span class=\"disabled\">next &#187;</span>";
$pagination.= "</div>\n";
}
/* ===================== Pagination Code Ends ================== */
<div class="pagenavi">
<?php echo $pagination; ?>
</div>
----------------------------------------------------------------------------------------------
Pagination CSS
----------------------------------------------------------------------------------------------
.pagenavi {
margin: 0px;
padding: 20px 0 0;
}
.pagenavi a, .pagenavi span {
background-color: #ffffff;
border: 1px solid #efefef;
border-radius: 2px;
color: #555555;
display: inline-block;
font-weight: 600;
margin: 0 2px;
min-width: 12px;
padding: 5px 9px;
text-align: center;
}
.dark .pagenavi a, .complex .pagenavi a, .dark .pagenavi span, .complex .pagenavi span {
background-color: rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.05);
}
.pagenavi a:hover, .pagenavi span.current {
background-color: #3498db;
border: 1px solid #efefef;
color: #ffffff;
text-decoration: none;
}
----------------------------------------------------------------------------------------------
Delete Confirmation Message
----------------------------------------------------------------------------------------------
<script type="text/javascript">
function delete_confirm(){
return confirm('Are you sure want to delete this data');
}
</script>
<a onclick="return delete_confirm();" href="">Delete</a>
or
<a onclick="return confirm('Are you sure want to delete this data!');" href="" class="">Delete</a>
----------------------------------------------------------------------------------------------
Showing items result
----------------------------------------------------------------------------------------------
Showing <?php echo ($page-1)*$post_per_page+1; ?> to
<?php
if($page == $lastpage) {
echo $i;
}
else{
echo $page*$post_per_page;
}
?>
of <?php echo $i; ?> Items.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment