Mysql

Mysql queries

Upload image plus all form data using jquery

Html

<form action="" method="post" enctype="multipart/form-data" id="form_portfolio_save">
				<input type="text" name="title" id="title" placeholder=""/><br/>
				<textarea id="about_project" name="about_project">About project</textarea><br/>
				<input type="file" name="photo" id="port_image" placeholder="Add Files"/><br/>
				<input type="text" name="caption" id="caption" placeholder="Image caption"/><br/>
				<input type="button" id="submit" value="Submit" class="button submit" onclick="save_portfolio();return false;"/>
				<input type="button" class="button cancel" id="submit" value="Cancel" />
			  </form>

JQuery

function save_portfolio(){
		var fd = new FormData();
		var file_data = $('input[name^="photo"]')[0].files; 
		for(var i = 0;i<file_data.length;i++){
			fd.append("file_"+i, file_data[i]);
		}
		var  other_data = $("#form_portfolio_save").serializeArray();
		 $.each(other_data,function(key,input){
			fd.append(input.name,input.value);
		});
	   
		$.ajax({
		url : 'save_portfolio.php',
		type : "post",
		data : fd,
		cache: false,
		contentType: false,
		processData: false,
		success : function(data){
				$("#append_portfolio").append(data);
				
		}
	});
	}

PHP

include("includes/session.php");
include("includes/database.php");
if(!empty($_FILES['file_0'])){
		$filename = time().$_FILES['file_0']['name'];
		$path= "upload/user_portfolio/".$filename;
		move_uploaded_file($_FILES['file_0']['tmp_name'],$path);
	}
$query = "insert into user_portfolio(title,description,images,image_captions,employee_id) values('".$_POST['title']."','".$_POST['about_project']."','".$filename."','".$_POST['caption']."','".$_SESSION['user_details']['id']."')";
mysql_query($query);
Advertisements

Filter on search results

js code

$(document).ready(function() {
    function showValues() {
		
		$("#wrapper").css("opacity","0.4");
		$( "#loading-image" ).show(0);	
		var mainarray = new Array();
		
		var subcategoriesarray = new Array();		
		$('input[name="subcategories"]:checked').each(function(){			
			subcategoriesarray.push($(this).val());		
			
		});
		
		var subcategories_checklist = "&subcategories="+subcategoriesarray;
				
		var job_typearray = new Array();		
		$('input[name="job_type"]:checked').each(function(){			
			job_typearray.push($(this).val());	
			
		});
		
		var job_type_checklist = "&job_type="+job_typearray;
		
		
		var budgetarray = new Array();		
		$('input[name="budget"]:checked').each(function(){			
			budgetarray.push($(this).val());
					
		});
		
		var budget_checklist = "&budget="+budgetarray;
		
		
		var experiencearray = new Array();		
		$('input[name="experience"]:checked').each(function(){			
			experiencearray.push($(this).val());
					
		});
		
		var experience_checklist = "&experience="+experiencearray;
		
		var job_durationarray = new Array();		
		$('input[name="job_duration"]:checked').each(function(){			
			job_durationarray.push($(this).val());
					
		});
		
		var job_duration_checklist = "&job_duration="+job_durationarray;
		var category = $("#category_selectbox").val();
		if(category.length > 0){
		var categoryname = "&category="+category;
		var main_string = subcategories_checklist+job_type_checklist+budget_checklist+experience_checklist+job_duration_checklist+categoryname;
		}
		else {
		var main_string = subcategories_checklist+job_type_checklist+budget_checklist+experience_checklist+job_duration_checklist;
		}
		main_string = main_string.substring(1, main_string.length)
		//alert(main_string);
		
		
		$.ajax({
			type: "POST",
			url: "ajax/filter_jobs.php",
			data: main_string, 
			cache: false,
			success: function(html){
				//alert(html);
				$("#place_content_here").html(html);		
				$("#wrapper").css("opacity","1");
				$( "#loading-image" ).hide(0);
				
				
				
			}
			});
		
		
	}
	
	$("input[type='checkbox']").on( "click", showValues );
    $("select").on( "change", showValues );

PHP filter queries




<?php
//error_reporting(E_all);
include("../includes/database.php");
$subcategory = $_REQUEST['subcategories'];
$workload = $_REQUEST['job_type'];
$budget = $_REQUEST['budget'];
$experience = $_REQUEST['experience'];
$job_duration = $_REQUEST['job_duration'];
$category = $_REQUEST['category'];

$query = "select * from job where";
$where = array();
if(!empty($category)){
$where[] = " category = $category";
}
if(!empty($subcategory)){
$where[] = " subcategory IN($subcategory)";
}
if(!empty($workload)){
$workload = explode(",",$workload);
$workload = "'".implode("','", $workload)."'";
$where[] = " workload IN($workload)";
}
if(!empty($budget)){
$budget = explode(",",$budget);
$budget = "'".implode("','", $budget)."'";
$where[] = " budget IN($budget)";
}
if(!empty($experience)){
$experience = explode(",",$experience);
$experience = "'".implode("','", $experience)."'";
$where[] = " desired_experience IN($experience)";
}
if(!empty($job_duration)){
$job_duration = explode(",",$job_duration);
$job_duration = "'".implode("','", $job_duration)."'";
$where[] = " duration IN($job_duration)";
}
    

$where = implode(" or ",$where);
$query .= $where;
//echo $query;
$jobs = mysql_query($query);
function time_ago($date)

											{

											if(empty($date)) {

											return "No date provided";

											}

											$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");

											$lengths = array("60","60","24","7","4.35","12","10");

											$now = time();

											$unix_date = strtotime($date);

											// check validity of date

											if(empty($unix_date)) {

											return "Bad date";

											}

											// is it future date or past date

											if($now > $unix_date) {

											$difference = $now - $unix_date;

											$tense = "ago";

											} else {

											$difference = $unix_date - $now;
											$tense = "from now";}

											for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {

											$difference /= $lengths[$j];

											}

											$difference = round($difference);

											if($difference != 1) {

											$periods[$j].= "s";

											}

											return "$difference $periods[$j] {$tense}";

											}
?>
						<div class="search_btm_text">
                    	
						<?php
						   
						   if(mysql_num_rows($jobs) > 0){
	                       while($job = mysql_fetch_array($jobs))
			                { //echo "<pre>";print_r($job);
                              ?>
                        <div class="work_div">
                        	<h4><a href="job_detail.php?number=<?php echo base64_encode($job['id']);?>"><?php echo $job['title'];?></a></h4>
                            <div class="supported_info">
                            	<p><strong class="fixedprice"><?php echo $job['workload'];?>:<?php echo ucfirst($job['desired_experience']);?> level </strong>Budget :<strong><?php echo $job['budget'];?></strong> - Posted <?php echo time_ago($job['date']);?></p>
                               
                            </div><!--supported_info-->
                            <div class="cDescription"><p><?php echo $job['description'];?>....<a href="job_details.php?job=<?php echo $job['id'];?>">more</a></p></div>
                            <div class="supported_info">
                            	<p><strong class="ipaid">Client: </strong><img src="assets/images/star4.png" alt=""><strong>Working Hours:<?php echo $job['duration'];?> </strong> 
								<?php 
										if(!empty($job['qualification'])){
										$quali = mysql_query("select * from skills where id IN(".$job['qualification'].")");
											$array = array(); 
											while($skil = mysql_fetch_array($quali)){
												$array = $skil['skill'];
											}
											echo implode(",",$array);
										}
										
										?>
								</p>
                                
                            </div>
                        </div><!--work_div-->
						
						<?php } } else { ?>
						 <div class="work_div">
						 <h4>No matching results were found !</h4>
						 </div>
						<?php } ?>
                       <?php //echo $pagination;?>
                      
                    </div><!--search_btm_text-->

	
	
	
});	


Html code

<div class="my_categories">
                    	<h5>Choose Category</h5>
                        <select class="categories_list" id="category_selectbox" name="category" onchange="get_subcategory($(this).val());">
						<?php $cats  = mysql_query("select * from categories"); 
						 while($c = mysql_fetch_array($cats)){
						?>
                        	<option value="<?php echo $c['id'];?>"><?php echo $c['name'];?></option>
                           
							<?php } ?>
                        </select>
                    </div><!--my_categories-->
					<div class="my_categories">
                    	<h5>Sub Categories</h5>
                        <ul class="categories_list" id="subcategory_checkbox12">
						<?php $subcats = mysql_query("select * from subcategories where category_id = 7");
						while($subcat = mysql_fetch_array($subcats)){
						?>
						<li><input type="checkbox" class="subcat_checkbox" name="subcategories" value="<?php echo $subcat['id'];?>"/><?php echo $subcat['name'];?></li>
						<?php } ?>
                        </ul>
                    </div><!--my_categories-->
					<div class="my_categories">
                    	<h5>Job type</h5>
                        <ul class="categories_list">
						
                        	<li><input type="checkbox" name="job_type" class="job_type" value="Part Time"/>Part Time</li>
                           <li><input type="checkbox" name="job_type" class="job_type" value="Full Time"/>Full Time</li>
						
                        </ul>
                    </div><!--my_categories-->
					<div class="my_categories">
                    	<h5>Budget</h5>
                        <ul class="categories_list">
						
                        	<li><input type="checkbox" name="budget" class="budgets" value="Less than 100$"/>Less than 100$</li>
                           <li><input type="checkbox" name="budget" class="budgets" value="Between $100 & $500"/>Between $100 & $500</li>
						   <li><input type="checkbox" name="budget" class="budgets" value="Between $500 & $1000"/>Between $500 & $1000</li>
                           <li><input type="checkbox" name="budget" class="budgets" value="Greater then $1000"/>Greater then $1000</li>
						   <li><input type="checkbox" name="budget" class="budgets" value="Not sure.."/>Not sure..</li>
                        </ul>
                    </div><!--my_categories-->
					<div class="my_categories">
                    	<h5>Experience</h5>
                        <ul class="categories_list">
						
                        	<li><input type="checkbox" name="experience" class="experience" value="entry"/>Entry Level</li>
                           <li><input type="checkbox" name="experience" class="experience" value="intermediate"/>Intermediate Level</li>
						   <li><input type="checkbox" name="experience" class="experience" value="expert"/>Expert Level</li>
						
                        </ul>
                    </div><!--my_categories-->
					<div class="my_categories">
                    	<h5>Job Duration</h5>
                        <ul class="categories_list">
						
                        	<li><input type="checkbox" name="job_duration" class="job_duration" value="Less then 1 week"/>Less than 1 Week</li>
                           <li><input type="checkbox" name="job_duration" class="job_duration" value="Less then 1 months"/>Less than 1 Month</li>
						   <li><input type="checkbox" name="job_duration" class="job_duration" value="1 to 3 months"/>1 to 3 Months</li>
							<li><input type="checkbox" name="job_duration" class="job_duration" value="3 to 6 months"/>3 to 6 Months</li>
							<li><input type="checkbox" name="job_duration" class="job_duration" value="More then 6 months"/>More than 6 Months</li>
                        </ul>
                    </div><!--my_categories-->
                

Get product details from wordpress database and save to other database php,mysql,wordpress


$allpro = mysql_query(" SELECT wp_posts.ID,wp_posts.post_content, wp_posts.post_title, pm1.meta_value AS field1, pm2.meta_value AS field2
FROM wp_posts
LEFT JOIN wp_postmeta AS pm1 ON ( wp_posts.ID = pm1.post_id
AND pm1.meta_key = '_price' )
LEFT JOIN wp_postmeta AS pm2 ON ( wp_posts.ID = pm2.post_id
AND pm2.meta_key = '_thumbnail_id' )
WHERE wp_posts.post_type = 'product'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
");

$productall = array();
while($pr = mysql_fetch_array($allpro))
{

		$im = mysql_fetch_array(mysql_query("select `guid` from `wp_posts` where `ID` = '".$pr['field2']."'"));
		//print_r($im['guid']);
		$productall['ID'][] = $pr['ID'];
		$productall['post_title'][] = $pr['post_title'];
		$productall['post_content'][] = $pr['post_content'];
		$productall['price'][] = $pr['field1'];
		$productall['image'][] = $im['guid'];
}

$totalcount = count($productall['ID']);
//connect to database you want to save products to
for($p=1;$p&lt; $totalcount;$p++)
{
		$num = mysql_num_rows(mysql_query("select * from `bb_products` where `postid_in_wp` = '".$productall['ID'][$p]."'"));
		if($num)
		{
		
		}
		else
		{
				mysql_query("insert into `bb_products`(`title`,`description`,`price`,`image`,`postid_in_wp`) values('".$productall['post_title'][$p]."','".$productall['post_content'][$p]."','".$productall['price'][$p]."','".$productall['image'][$p]."','".$productall['ID'][$p]."')");
		}


}

Simple shopping cart application using php,mysql,ajax,jquery

1)
Create table named `bb_prodducts` and fields
number_of_bucks,name,price_per_package,price_per_credit,number_of_bucks,id as auto increment
2)
create a file named packages.php and paste the code below

session_start();
 $title = "BabyBoomApps - Content Page";
$page_description = "BabyBoomApps is the place to help fund and gift a baby's future through contests, drawings and online fun!";
include("includes/header.php");
include('includes/checksession.php');
include('includes/connect.php');
$packs = mysql_query("SELECT * FROM `bb_packages`");


<!--breadcrumbs start-->
<div class="breadcrumbs">
  <div class="container">
    <div class="row">
      <div class="col-lg-4 col-sm-4">
        <h1 id="main_conhdr">MEMBERS</h1>
      </div>
      <div class="col-lg-8 col-sm-8">
        <ol class="breadcrumb pull-right">
          <li><a href="#">Home</a></li>
          <li class="active">BoomBucks
          <li>
        </ol>
      </div>
    </div>
  </div>
</div>
<!--breadcrumbs end-->
<!--container start-->
<div class="container">
  <div class="row">

    <div class="col-lg-9 about">
	<section class="credits-header">
          <div id="buy-credits-default">
            <h3>Buy BoomBucks</h3>
          
            <div class="clear"></div>
         
        </section>
     <div class="boombucks_section">
           
            <section class="tile-container smaller-boombucks_section">
			<?php while($pk = mysql_fetch_array($packs)) { ?>
             <section class="boombucks" >
                <header></header>
                <h3><?php echo $pk['number_of_bucks']; ?><BR><?php echo $pk['name']; ?> </h3>
                <span class="boombuck">$<?php echo ($pk['price_per_package']); ?>&nbsp;USD</span> <span class="boombucks-unit-price">($<?php echo $pk['price_per_credit']; ?>/credit)</span>
          
               
                 <span id="s<?php echo $pk['id']; ?>"> <input type="submit" name="submit" value="Buy Now" onclick="addtocart('<?php echo $pk['id']; ?>','<?php echo $pk['number_of_bucks']; ?>','<?php echo $pk['name']; ?>','<?php echo $pk['price_per_credit']; ?>','<?php echo $pk['price_per_credit']; ?>');" class="btn btn-info"  /></span>
           
              </section>
             <?php } ?>
            
            </section>
            <div class="clear"></div>
          
            
           
          </div>
		  <span class="row button-next btn btn-info" style="float:right;width:20%;" onclick="window.location.href='checkout.php'" id="proced_checkout">Proceed to Checkout</span>
    </div>
	
    <div class="col-lg-">
      <?php include("includes/sidebar-account.php"); ?>
    </div>
	
  </div>
  <div  class="row">
  </div>
</div>
<!--container end-->
<?php include("includes/footer.php"); ?>

<script>
function addtocart(id,numbucks,name,total,ppc)
{
$("#s"+id).html("Added!");
$.ajax({
     url:"add-to-cart.php",
	 type:"post",
	 data:{'crtidpck':id,'numbucks':numbucks,'name':name,'total':total,'ppc':ppc},
	 success:function(da){
	      alert('added');
		  
    }
});
}
</script>

3)The packages added to cart will be saved to session,use the file below add-to-cart.php

<?php
if(!session_id()){
	session_start();
}
include('includes/checksession.php');
/* unset($_SESSION['cart']);  */
$_SESSION['cart']['id'][] = $_POST['crtidpck'];
$_SESSION['cart']['numbucks'][] = $_POST['numbucks'];
$_SESSION['cart']['name'][] = $_POST['name'];
$_SESSION['cart']['total'][] = $_POST['total'];
$_SESSION['cart']['ppc'][] = $_POST['ppc'];
print_r($_SESSION['cart']);
?>


4)And the checkout file managing removal of items from cart and displaying them on the page on file checkout.php as follows


<?php
session_start();
 $title = "BabyBoomApps - Content Page";
$page_description = "BabyBoomApps is the place to help fund and gift a baby's future through contests, drawings and online fun!";
include("includes/header.php");
include('includes/checksession.php');
include('includes/connect.php');

?>

<!--breadcrumbs start-->
<div class="breadcrumbs">
  <div class="container">
    <div class="row">
      <div class="col-lg-4 col-sm-4">
        <h1 id="main_conhdr">MEMBERS</h1>
      </div>
      <div class="col-lg-8 col-sm-8">
        <ol class="breadcrumb pull-right">
          <li><a href="#">Home</a></li>
          <li class="active">BoomBucks
          <li>
        </ol>
      </div>
    </div>
  </div>
</div>
<!--breadcrumbs end-->
<!--container start-->
<div class="container">
  <div class="row">

    <div class="col-lg-9 about">
	<section class="credits-header">
          <div id="buy-credits-default">
            <h3>BoomBucks Cart</h3>
          
            <div class="clear"></div>
         
        </section>
     <div class="boombucks_section">
           
            <section class="tile-container smaller-boombucks_section">
			
			
			<!-------this section runs when cart is updated----------->
			<?php if($_POST['upcart']) { 
					if($_GET['index'])
					{
					$removeindex = $_GET['index'];
					unset($_POST[$removeindex]);
					$_POST = array_values($_POST);
					}
			?>
			<table style="width:100%">
			<thead style="border-bottom:1px dashed #ccc">
			<tr>
			<th>Package</th><th>Package Price</th><th>Quantity</th><th>Sub Total</th><th>&nbsp;</th>
			</tr>
			</thead>
			<tbody>
			
			<?php 
			//echo "<pre>";print_r($_SESSION['cart']['id']); print_r($_POST);
			$countt = count($_SESSION['cart']['id']);
			$total = 0;
			for($l=0;$l<$countt;$l++)
			{
			?>
			<tr>
			<td>
			<?php echo $_SESSION['cart']['numbucks'][$l];?>&nbsp;BoomBucks
			</td>
			
			<td>
			$<span id="ppc<?php echo $_SESSION['cart']['id'][$l];?>"><?php echo $_SESSION['cart']['ppc'][$l];?></span>
			</td>
			<td>
			<form action="" method="post">
			<input type="text" value="<?php echo $_POST[$l];?>" name="<?php echo $l ;?>" id="<?php echo $_SESSION['cart']['id'][$l];?>" style=" border: 1px solid #CCCCCC;
    height: 30px;
    width: 50px;"/>
			</td>
			<td>
			$<span id="s<?php echo $l;?>">
			
			<?php echo ($_SESSION['cart']['numbucks'][$l])*($_SESSION['cart']['ppc'][$l])*($_POST[$l]);?>
			
			
			</span>
			</td>
			<td>
			<span class="removespan" id="re<?php echo $l;?>" onclick="removeitem('<?php echo $l;?>')">Remove</span>
			</td>
			</tr>
			<?php 
			$total+=  ($_SESSION['cart']['numbucks'][$l])*($_SESSION['cart']['ppc'][$l])*($_POST[$l]);
			} ?>
			<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>Grand Total</td><td>$<span id="grand_total"><?php echo $total; ?></span></td><td>&nbsp;</td></tr>
			</tbody>
			</table>
					
            <?php }else { ?>
			<!------------------>
		
			
			<table style="width:100%">
			<thead style="border-bottom:1px dashed #ccc">
			<tr>
			<th>Package</th><th>Package Price</th><th>Quantity</th><th>Sub Total</th><th>&nbsp;</th>
			</tr>
			</thead>
			<tbody>
			
			<?php 
			
			$countt = count($_SESSION['cart']['id']);
			$total = 0;
			for($l=0;$l<$countt;$l++)
			{
			?>
			<tr>
			<td>
			<?php echo $_SESSION['cart']['numbucks'][$l];?>&nbsp;BoomBucks
			</td>
			
			<td>
			$<span id="ppc<?php echo $_SESSION['cart']['id'][$l];?>"><?php echo $_SESSION['cart']['ppc'][$l];?></span>
			</td>
			<td>
			<form action="" method="post">
			<input type="text" value="1" name="<?php echo $l;?>" id="<?php echo $_SESSION['cart']['id'][$l];?>" style=" border: 1px solid #CCCCCC;
    height: 30px;
    width: 50px;"/>
			</td>
			<td id="sub<?php $l;?>">
			$<span id="s<?php echo $l;?>"><?php echo ($_SESSION['cart']['numbucks'][$l])*($_SESSION['cart']['ppc'][$l]);?></span>
			</td>
			<td>
			<span class="removespan" id="re<?php echo $_SESSION['cart']['id'][$l];?>" onclick="removeitem('<?php echo $l;?>')">Remove</span>
			</td>
			</tr>
			<?php 
			$total+=  ($_SESSION['cart']['numbucks'][$l])*($_SESSION['cart']['ppc'][$l]);
			} ?>
			<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>Grand Total</td><td>$<span id="grand_total"><?php echo $total; ?></span></td><td>&nbsp;</td></tr>
			</tbody>
			</table>
			<?php } ?>
            </section>
            <div class="clear"></div>
          
            
           
          </div>
		  <br/><br/>
		  <input type="submit" name="upcart" class="row button-next btn btn-info" value="Update Cart" style="float:left;width:20%;"></form><span class="row button-next btn btn-info" style="float:left;width:20%;margin-left:5px;" onclick="removeall();">Remove All</span><span class="row button-next btn btn-info" style="float:right;width:20%;" onclick="window.location.href='checkout.php'" id="proced_checkout">Proceed to Checkout</span>
    </div>
	
    <div class="col-lg-">
      <?php include("includes/sidebar-account.php"); ?>
    </div>
	
  </div>
  <div  class="row">
  </div>
</div>
<!--container end-->
<?php include("includes/footer.php"); ?>


<script>
function addtocart(id)
{
$.ajax({
     url:"add-to-cart.php",
	 type:"post",
	 data:{'crtidpck':id},
	 success:function(da){
	      alert('added');
		  $("#s"+id).html("Added!");
    }
});
}
function removeitem(id)
{

var subtotal = $("#s"+id).text();

var grand_total = $("#grand_total").text();
var new_grand_total = parseInt(grand_total) - parseInt(subtotal);
$("#grand_total").text(new_grand_total);
$("#re"+id).parent("td").parent("tr").hide(0);
 $.ajax({
     url:"remove-from-cart.php",
	 type:"post",
	 data:{'crtidpck':id},
	 success:function(da){
	      //alert('Removed!');
		  window.location.href="checkout.php?index="+id;
		 // $("#s"+id).html("Removed!");
    }
}); 
}
function removeall()
{
 $.ajax({
     url:"remove-all-from-cart.php",
	 type:"post",
	 success:function(da){
	     $("tbody tr:last").prevAll("tr").hide(0);
		 $("#grand_total").text("00.00");
    }
}); 
}
</script>

5)The other ajax request files used in checkout.php are
a)remove-from-cart.php

<?php
if(!session_id()){
	session_start();
}
include('includes/checksession.php');
$index = $_POST['crtidpck'];

unset($_SESSION['cart']['id'][$index]);
$_SESSION['cart']['id'] = array_values($_SESSION['cart']['id']);

unset($_SESSION['cart']['numbucks'][$index]);
$_SESSION['cart']['numbucks'] = array_values($_SESSION['cart']['numbucks']);

unset($_SESSION['cart']['name'][$index]);
$_SESSION['cart']['name'] = array_values($_SESSION['cart']['name']);

unset($_SESSION['cart']['total'][$index]);
$_SESSION['cart']['total'] = array_values($_SESSION['cart']['total']);

unset($_SESSION['cart']['ppc'][$index]);
$_SESSION['cart']['ppc'] = array_values($_SESSION['cart']['ppc']);


?> 

b)remove-all-from-cart.php

<?php
if(!session_id()){
	session_start();
}
include('includes/checksession.php');
unset($_SESSION['cart']);
?>