Friday, 12 December 2014

show hide div with java script


<script>
function myFunction() {
        document.getElementById("my_file").style.display = "block";

}
</script>


<input type="button" onclick="myFunction()" id="addmore" width="30px" name="addmore" value="Add More Images"/>

<input type="file" id="my_file" name="Photo1" style="display: none;" />

fetching value from multidimentional array

fetching value from this kind of array -

 print_r($user_last) gives result -
  then fetching city code - $scity =  $user_last['city'][0];
Array
(
    [nickname] => Array
        (
            [0] => rakesh
        )
    [first_name] => Array
        (
            [0] => rakesh
        )

    [last_name] => Array
        (
            [0] => roshan
        )
    [city] => Array
        (
            [0] => mohali
        )
)

wordpress logged in user related things

global $current_user  - > this shows current logged in user.

print_r($current_user);  - will give all details of current user
$cid =  $current_user->ID;  -- fetch id of current user.
echo $cid;   gives value
$custom_fields = get_post_custom($cid);  pass this id to this function and get the values of custom fields
related with current user.

print_r($custom_fields);  will display all details of custom fields
  die;

GET_USER_META - gives all the values associated with logged in user
$user_last = get_user_meta( $cid );

Thursday, 11 December 2014

Complete Search result Form for custom table query in wordpress

This is complete template Example for custom table query search.
<?php

/**

* Template Name: Advanced Search Result Page

*/
?>
<?php get_header(); ?>
<div class="main_content">
<div class="page_content_wrapper">
<div class="page_content the_content">
             <div class="ad-search">
<form role="search" method="get"  action="/advanced-serach-result">
   Advanced Search <input type="text" value="" name="adsearch" />
           <input type="submit" value="Advanced Search" name="submit" />
           </form>
</div>
<?php
           if(isset($_GET['submit'])){
if(preg_match("/^[a-zA-Z 0-9]+/", $_GET['adsearch'])){
                                $term = $_GET['adsearch'];
$admin = admin_url();
$url = plugins_url();
$sql="SELECT * FROM wp_usersinfo WHERE name LIKE '%" . $term .  "%' OR city LIKE '%" . $term ."%' OR race LIKE '%" . $term ."%' OR state LIKE '%" . $term ."%' OR hometown LIKE '%" . $term ."%' OR crime LIKE '%" . $term ."%' OR eyes LIKE '%" . $term ."%' OR hair LIKE '%" . $term ."%' OR height LIKE '%" . $term ."%' OR weight LIKE '%" . $term ."%' OR gender LIKE '%" . $term ."%'";

$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo "Total Inmates Found : - $num_rows \n";
while($row = mysql_fetch_array($result)){
  $ID = $row['id'];
  echo "<ul class='ad-right-detail'>\n";
  echo "<li><span><strong>" . "<a  href=\"my-detail-page?id=$ID\">"   .$row['name'] ."</a></span></strong></li>\n";
                                   echo "</ul>";
 }    
}
}
?>

</div>
</div>
</div>

<?php get_footer(); ?>

How to Create a Search Feature with PHP and MySQL

<?php
  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){
  $name=$_POST['name'];
  //connect  to the database
  $db=mysql_connect  ("servername", "username",  "password") or die ('I cannot connect to the database  because: ' . mysql_error());
  //-select  the database to use
  $mydb=mysql_select_db("yourDatabase");
  //-query  the database table
  $sql="SELECT  ID, FirstName, LastName FROM Contacts WHERE FirstName LIKE '%" . $name .  "%' OR LastName LIKE '%" . $name ."%'";
  //-run  the query against the mysql query function
  $result=mysql_query($sql);
  //-create  while loop and loop through result set
  while($row=mysql_fetch_array($result)){
          $FirstName  =$row['FirstName'];
          $LastName=$row['LastName'];
          $ID=$row['ID'];
  //-display the result of the array
  echo "<ul>\n";
  echo "<li>" . "<a  href=\"search.php?id=$ID\">"   .$FirstName . " " . $LastName .  "</a></li>\n";
  echo "</ul>";
  }

For Responsive menu add this js in header

<script src="<?php echo get_template_directory_uri(); ?>/js/jquery.slicknav.js" type="text/javascript"></script>

<script type="text/javascript">

jQuery( document ).ready(function( $ ) {
$('#mainnav-menu').slicknav();
// Code that uses jQuery's $ can follow here.
});
</script>

Wednesday, 10 December 2014

add class with jquery

include latest jquery file on top.
write code -
<script>
$(document).ready(function(){
    $("#menu-item-2308").addClass("blue");
});
</script>

here menu-item-2308 is id of div where you want to add class and blue is class name.

when using java script use this one

  <script type="text/javascript">
 window.onload = function() {
                document.getElementById('menu-item-134').className = 'blue';
               };
   </script>

Or if you're using jQuery:

$(function() {
  $('#about').addClass('expand');
});