Month: January 2018

How to add and remove attribute with jquery?

If you want to add and remove attribute using jquery then you can check this code. It will remove id and then when click remove then this id will remove. I also add css for understanding it.

This is html and css code


<div id="page_navigation1" style="border: 2px solid #ccc;height: 70px;width: 300px">Main Content</div>
<button id="add">Add</button>
<button id="remove">Remove</button>

This is css code

<style>
    #page_navigation1 {
        color: red;
    }
</style>

This is jQuery code which add and remove attribute

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#page_navigation1").addClass("page_navigation");

        $("#add").click(function(){
            $(".page_navigation").attr("id","page_navigation1");
        });
        $("#remove").click(function(){
            $(".page_navigation").removeAttr("id");
        });
    });
</script>

When run this code you will see this effect, If you find any problem then please inform me.

Comment

php remove duplicates and sum of them from array

If you want to remove duplicates then sum of them from array then this code will help you. this is code which remove duplicate and sum of them.

$result = array();
foreach ($data as $val) {
    if (!isset($result[$val->reservations_detail_article]))
        $result[$val->reservations_detail_article] = $val;
    else
        $result[$val->reservations_detail_article]['reservations_detail_qte'] += $val['reservations_detail_qte'];
}

$result = array_values($result); 
print_r($result);

If you face any problem then inform me, i will try to help you.

Comment

How to auto reload and clear cache using Javascript?

If you want to reload page automatically and at a time you want to clear cache then you can do this using javascript. This is simple code which reload after 3 seconds

 

<pre class="brush: xml; title: ; notranslate" title=""><!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
setInterval(function(){cache_clear()},3000);
});
function cache_clear()
{
window.location.reload(true); // if you do not want to clear cache then use window.location.reload();
}
</script>
</head>

<body>
<p>Auto reload and clear cache</p>
</body>
</html></pre>

Hope it will help you. If you face any problem then please inform me.

Comment

how to set class to selected “li” dynamically using jQuery?

If you want to add class in click li and remove all other class from all li then you can use simple jQuery code. Sometime we need to set active class for menu or other in this case this code is very helpful. This is simple code which set new class and remove all class from li.

$(document).ready(function(){
        $('.list li').click(function() {
             $( '.list li' ).removeClass( "active" ); // remove active class from all li 
            $(this).addClass('active'); // add active class for click li
        });
    });

then this is html code

active {
color:red;
}
   li {
   cursor: pointer;
  }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list">
            <li>Test 1</li>
            <li>Test 2</li>
            <li>Test 3</li>
            <li>Test 4</li>
            <li>Test 5</li>
            <li>Test 6 </li>
            <li>Test 7</li>
        </ul>

When you click any li then remove all class and add active class , i also add a css which color is red then easily can understand it.
If you face any problem then please inform me. I will try to help you.

Comment

Phpstrom Editor Some Important Tips


-Xms128m
-Xmx512m
-XX:MaxPermSize=250m
-XX:ReservedCodeCacheSize=64m
-XX:+UseCodeCacheFlushing
-ea
Just change Xmx 512 m this value. then save it. sometime this file not save normally then save it other folder then replace this in bin folder. Restart phpstrom editor .You will see new define memory in right bottom side.

2. How to show line number ?

Ans: goto file->setting from top menu then you will see a screen . goto <span style="color: #993300;">editor tab-> appearance</span> then you will see

Show line number checkbox . Check it.

3. Goto line number ->Ctrl+g

4. Find Ctrl+f

5. Find and replace Ctrl+r

6. Find in path Ctrl+shift+f

7. Replace in path Ctrl+shift+r

8. Any comment ctrl+shift+/

9. Goto end tag ctrl+}

10. Format code ctrl+alt+l

11. goto include file then click this file name

12. Goto start tag ctrl+{
13. Find total function list and file list which include ctrl+F12

For more setting go to file->setting from top menu then go to keymap from "editor->keymap".



 

if you face any problem then please inform me.

Comment

check value using in_array with two dimension array php

Suppose you need to check value from two dimension array then normally in_array not work so you can check this value using this code which check value from two dimension array. this is simple code

<?php

Array ( [0] => Array ( [id] => 9545 [store] => Some Store Name )[1] => Array ( [id] => 9544 [store] => Some Store Name ); // this array 
 
 
$id = 9545; this is check value 
$store = 'Some Store Name'; this is check value 
if (in_array(array('id' => $id, 'store' => $store), $array)) {
 echo "Found!";
}
?>

then output is : Found.

if you face any problem then inform me, i will try to help you

Comment

Input tab index auto jump using jQuery.

In html form or other some time need tab auto jump when max length is covered. We can do it easily javascript code.

First create simple html code

<input size="2" tabindex="1" id="one"
maxlength="2" onkeyup="toUnicode(this)" />

<input size="2" tabindex="2" id="two"
maxlength="2" onkeyup="toUnicode(this)" />

<input size="2" tabindex="3" id="three"
maxlength="2" onkeyup="toUnicode(this)" />

then create javascript code. this is javascript code.

function toUnicode(elmnt)
{
  var next;
  if (elmnt.value.length==elmnt.maxLength)
  {
    next=elmnt.tabIndex + 1;
    //look for the fields with the next tabIndex
    var f = elmnt.form;
    for (var i = 0; i < f.elements.length; i++)
    {
      if (next<=f.elements[i].tabIndex)
      {
        f.elements[i].focus();
        break;
      }
    }
  }
}

then browse it and press onkeyup then automatic go to next input field.

Comment

How can I load jQuery if it is not loaded?

If you need to check that a specific jQuery load or not load if not loaded then load it or not load . You can do this using jquery . This is simple code.

<script type="text/javascript">
 var baseurl="shafiqul.wordpress.com";
 var doc=document.getElementsByTagName("head").item(0);
 var jQueryScriptOutputted = false;
 var jQueryScriptOutputtedplaceholder = false;
 window.onload=function()
 {
 initJQuery();
 }
 function initJQuery() {
 if (typeof(jQuery) == "undefined") {
 if (! jQueryScriptOutputted) {
 jQueryScriptOutputted = true;
 includeJs("/js/jquery-1.4.4.min.js", true);
| }
 }
 if (! jQueryScriptOutputtedplaceholder) {
 jQueryScriptOutputtedplaceholder = true;
 includeJs("/scripts/html5_placeholder.js", true);
 }
 setTimeout("initJQuery()", 100);
 }
 function includeJs(fileName, jQ, head){
 var js=document.createElement("script");
 js.setAttribute("language", "javascript");
 js.setAttribute("type", "text/javascript");
 js.setAttribute("src", baseurl+fileName);
 if(jQ===true){
 doc.insertBefore(js, doc.firstChild);
 return false;
 }
 if(head===true){
 doc.appendChild(js);
 return false;
 }
 document.getElementsByTagName("body").item(0).appendChild(js);
 return false;
 }
 </script>

This code first check jquery already or not, if not loaded then load it. If you face any problem then please inform me.

Comment

Show Content if class attribute exists in page.

If you want to show content when class exit in this page then you can use this code .It will give alert ok when class attribute exit in this page , you can change also class to id if you want.

<body>
<div class="home-container">
    <div class="home">

        Content

    </div>
    <span style="cursor: pointer; background: #0f192a;color: #00BD9B" onclick="test()">Click</span>
</div>
</body>

then this is javascript code which give alert if class attribute exit in this page

function test() {
        if (document.querySelector('.home') !== null) {
            alert('ok');
        } else {
            alert('not');
        }
    }

In this code home attribute exit so it will give alert ok.
If you find any problem then please inform me.

Comment

Image crop and upload using PHP & Jquery.

If you want to crop image and also upload this crop image then you can do this using jquery and php. This is simple hmtl code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
    select,textarea,input[type="text"],input[type="password"],input[type="datetime"],input[type="datetime-local"],input[type="date"],input[type="month"],input[type="time"],input[type="week"],input[type="number"],input[type="email"],input[type="url"],input[type="search"],input[type="tel"],input[type="color"],.uneditable-input{display:inline-block;height:20px;padding:4px;margin-bottom:9px;font-size:13px;line-height:18px;color:#555555;}
    textarea{height:auto;}
    select,textarea,input[type="text"],input[type="password"],input[type="datetime"],input[type="datetime-local"],input[type="date"],input[type="month"],input[type="time"],input[type="week"],input[type="number"],input[type="email"],input[type="url"],input[type="search"],input[type="tel"],input[type="color"],.uneditable-input{background-color:#ffffff;border:1px solid #cccccc;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;-webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.075);-moz-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.075);box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.075);-webkit-transition:border linear 0.2s,box-shadow linear 0.2s;-moz-transition:border linear 0.2s,box-shadow linear 0.2s;-ms-transition:border linear 0.2s,box-shadow linear 0.2s;-o-transition:border linear 0.2s,box-shadow linear 0.2s;transition:border linear 0.2s,box-shadow linear 0.2s;}textarea:focus,input[type="text"]:focus,input[type="password"]:focus,input[type="datetime"]:focus,input[type="datetime-local"]:focus,input[type="date"]:focus,input[type="month"]:focus,input[type="time"]:focus,input[type="week"]:focus,input[type="number"]:focus,input[type="email"]:focus,input[type="url"]:focus,input[type="search"]:focus,input[type="tel"]:focus,input[type="color"]:focus,.uneditable-input:focus{border-color:rgba(82, 168, 236, 0.8);outline:0;outline:thin dotted \9;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(82,168,236,.6);-moz-box-shadow:inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(82,168,236,.6);box-shadow:inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(82,168,236,.6);height: 20px;}
    select,input[type="radio"],input[type="checkbox"]{margin:3px 0;*margin-top:0;line-height:normal;cursor:pointer;}
    select,input[type="submit"],input[type="reset"],input[type="button"],input[type="radio"],input[type="checkbox"]{width:auto;}
    .uneditable-textarea{width:auto;height:auto;}
    #country{height: 30px;}
	.highlight
	{
		border: 1px solid red !important;
	}
</style>
<script>
function test()
{
 var isFormValid = true;

    $(".bs-example input").each(function(){
        if ($.trim($(this).val()).length == 0){
		    $(this).addClass("highlight");
            isFormValid = false;
			$(this).focus();
        }
        else{
            $(this).removeClass("highlight");
        }
    });

    if (!isFormValid) { 
		alert("Please fill in all the required fields (indicated by *)");
	}

    return isFormValid;
}	
</script>
</head>
<body>
<div class="bs-example">
    <form onsubmit="return test()">
        <div class="form-group">
            <label for="inputEmail">Email</label>
            <input type="text" class="form-control" id="inputEmail" placeholder="Email">
        </div>
        <div class="form-group">
            <label for="inputPassword">Password</label>
            <input type="password" class="form-control" id="inputPassword" placeholder="Password">
        </div>
        <button type="submit" class="btn btn-primary">Login</button>
    </form>
</div>
</body>
</html>       

then create php file. This is php code

<?php
 
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$targ_w =$_POST['w'];
$targ_h =$_POST['h'];
 
$jpeg_quality = 90;
 
if(!isset($_POST['x']) || !is_numeric($_POST['x'])) {
die('Please select a crop area.');
}
 
$src = $_POST['image_name'];
$system = explode(".", $src);
 
if (preg_match("/jpg|jpeg/", $system[1]))
{
$src_img=imagecreatefromjpeg($src);
}
if (preg_match("/png/", $system[1]))
{
$src_img = imagecreatefrompng($src);
}
if (preg_match("/gif/", $system[1]))
{
$src_img = imagecreatefromgif($src);
}
 
$dst_r = ImageCreateTrueColor($targ_w, $targ_h);
 
imagecopyresampled($dst_r,$src_img,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
 
if (preg_match("/png/", $system[1]))
{
imagepng($dst_r, $_POST['image_name']);
}
else if (preg_match("/gif/", $system[1]))
{
imagegif($dst_r, $_POST['image_name']);
}
else
{
imagejpeg($dst_r, $_POST['image_name']);
}
 
imagedestroy($dst_r);
imagedestroy($src_img);
 
exit;
}
 
?>

then run html code then php code crop image and upload it. If you face any problem then please inform me.

Comment