php ajax拖动购物车源码

 更新时间:2016年11月25日 15:52  点击:1397

以前我们的购物车都是点击打开网页,都是web 2.0形式的,本文章提供这款php教程 ajax拖动购物车源码,可以拖动商品放在购物车里面,这样更好的适合用户习惯了。
*/
//download by http://www.111cn.net

/* database config */

$db_host  = 'localhost';
$db_user  = 'root';
$db_pass  = 'dcfan2006';
$db_database = 'test';

$link = mysql教程_connect($db_host,$db_user,$db_pass) or die('unable to establish a db connection');
mysql_select_db($db_database,$link);
mysql_query("set names utf8");

?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>php ajax拖动购物车源码</title>
<script type="text/网页特效" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>

<script>
var purchased=new array();
var totalprice=0;

$(document).ready(function(){
 
 $('.product').simpletip({
  
  offset:[40,0],
  content:'<img src="img/ajax_load.gif" alt="loading" style="margin:10px;" />',
  onshow: function(){
   
   var param = this.getparent().find('img').attr('src');
   
   if($.browser.msie && $.browser.version=='6.0')
   {
    param = this.getparent().find('img').attr('style').match(/src="([^"]+)"/);
    param = param[1];
   }
   
   this.load('ajax/tips教程.php',{img:param});
  }

 });
 
 $(".product img").draggable({
 
 containment: 'document',
 opacity: 0.6,
 revert: 'invalid',
 helper: 'clone',
 zindex: 100
 
 });

 $("div.content.drop-here").droppable({
 
   drop:
     function(e, ui)
     {
      var param = $(ui.draggable).attr('src');
      
      if($.browser.msie && $.browser.version=='6.0')
      {
       param = $(ui.draggable).attr('style').match(/src="([^"]+)"/);
       param = param[1];
      }

      addlist(param);
     }
 
 });

});


function addlist(param)
{
 $.ajax({
 type: "post",
 url: "ajax/addtocart.php",
 data: 'img='+encodeuricomponent(param),
 datatype: 'json',
 beforesend: function(x){$('#ajax-loader').css教程('visibility','visible');},
 success: function(msg){
  
  $('#ajax-loader').css('visibility','hidden');
  if(parseint(msg.status)!=1)
  {
   return false;
  }
  else
  {
   var check=false;
   var cnt = false;
   
   for(var i=0; i<purchased.length;i++)
   {
    if(purchased[i].id==msg.id)
    {
     check=true;
     cnt=purchased[i].cnt;
     
     break;
    }
   }
   
   if(!cnt)
    $('#item-list').append(msg.txt);
    
   if(!check)
   {
    purchased.push({id:msg.id,cnt:1,price:msg.price});
   }
   else
   {
    if(cnt>=3) return false;
    
    purchased[i].cnt++;
    $('#'+msg.id+'_cnt').val(purchased[i].cnt);
   }
   
   totalprice+=msg.price;
   update_total();

  }
  
  $('.tooltip').hide();
 
 }
 });
}

function findpos(id)
{
 for(var i=0; i<purchased.length;i++)
 {
  if(purchased[i].id==id)
   return i;
 }
 
 return false;
}

function remove(id)
{
 var i=findpos(id);

 totalprice-=purchased[i].price*purchased[i].cnt;
 purchased[i].cnt = 0;

 $('#table_'+id).remove();
 update_total();
}

function change(id)
{
 var i=findpos(id);
 
 totalprice+=(parseint($('#'+id+'_cnt').val())-purchased[i].cnt)*purchased[i].price;
 
 purchased[i].cnt=parseint($('#'+id+'_cnt').val());
 update_total();
}

function update_total()
{
 if(totalprice)
 {
  $('#total').html('total: $'+totalprice);
  $('a.button').css('display','block');
 }
 else
 {
  $('#total').html('');
  $('a.button').hide();
 }
}
</script>
<style>
body,h1,h2,h3,p,td,quote,small,form,input,ul,li,ol,label{
 margin:0px;
 padding:0px;
 font-family:arial, helvetica, sans-serif;
}

body{
 color:#555555;
 font-size:13px;
 background-color:#282828;
}

.clear{
 clear:both;
}

#main-container{
 width:700px;
 margin:20px auto;
}

.container{
 margin-bottom:40px;
}

.top-label{
 background:url(img/label_bg.png) no-repeat;
 display:inline-block;
 margin-left:20px;
 position:relative;
 margin-bottom:-15px;
}

.label-txt{
 background:url(img/label_bg.png) no-repeat top right;
 display:inline-block;
 font-size:10px;
 height:36px;
 margin-left:10px;
 padding:12px 15px 0 5px;
 text-transform:uppercase;
}

.content-area{
 background:url(img/container_top.png) no-repeat #fcfcfc;
 padding:15px 20px 0 20px;
}

.content{
 padding:10px;
}

.drag-desired{
 background:url(img/drag_desired_label.png) no-repeat top right;
 padding:30px;
}

.drop-here{
 background:url(img/drop_here_label.png) no-repeat top right;
}


.bottom-container-border{
 background:url(img/container_bottom.png) no-repeat;
 height:14px;
}

.product{
 border:2px solid #f5f5f5;
 float:left;
 margin:15px;
 padding:10px;
}

.product img{
 cursor:move;
}

p.descr{
 padding:5px 0;
}

small{
 display:block;
 margin-top:4px;
}

.tooltip{
 position: absolute;
 top: 0;
 left: 0;
 z-index: 3;
 display: none;

 background-color:#666666;
 border:1px solid #666666;
 color:#fcfcfc;

 padding:10px;
 
 -moz-border-radius:12px;
 -khtml-border-radius: 12px;
 -webkit-border-radius: 12px;
 border-radius:12px;
}

#cart-icon{
 width:128px;
 float:left;
 position:relative;
}

#ajax-loader{
 position:absolute;
 top:0px;
 left:0px;
 visibility:hidden;
}

#item-list{
 float:left;
 width:490px;
 margin-left:20px;
 padding-top:15px;
}

a.remove,a.remove:visited{
 color:red;
 font-size:10px;
 text-transform:uppercase;
}

#total{
 clear:both;
 float:right;
 font-size:10px;
 font-weight:bold;
 padding:10px 12px;
 text-transform:uppercase;
}

#item-list table{
 background-color:#f7f7f7;
 border:1px solid #efefef;
 margin-top:5px;
 padding:4px;
}

a.button,a.button:visited{
 display:none;

 height:29px;
 width:136px;

 padding-top:15px;
 margin:0 auto;
 overflow:hidden;

 color:white; 
 font-size:12px;
 font-weight:bold;
 text-align:center;
 text-transform:uppercase;
 
 background:url(img/button.png) no-repeat center top;
}

a.button:hover{
 background-position:bottom;
 text-decoration:none;
}


a, a:visited {
 color:#00bbff;
 text-decoration:none;
 outline:none;
}

a:hover{
 text-decoration:underline;
}

h1{
 font-size:28px;
 font-weight:bold;
 font-family:"trebuchet ms",arial, helvetica, sans-serif;
}

h2{
 font-weight:normal;
 font-size:20px;
 
 color:#666666;
 text-indent:30px;
 margin:20px 0;
}

.tutorialzine h1{
 color:white;
 margin-bottom:10px;
 font-size:48px;
}

.tutorialzine h3{
 color:#f5f5f5;
 font-size:10px;
 font-weight:bold;
 margin-bottom:30px;
 text-transform:uppercase;
}

.tutorial-info{
 color:white;
 text-align:center;
 padding:10px;
 margin-top:-20px;
}

</style>

<!--[if lt ie 7]>
<style type="text/css">
 .pngfix { behavior: url(pngfix/iepngfix.htc);}
    .tooltip{width:200px;};
</style>
<![endif]-->

</head>

<body>
<div id="main-container">

 <div class="tutorialzine">
    <h1>shopping cart</h1>
    <h3>the best products at the best prices</h3>
    </div>


    <div class="container">
   
     <span class="top-label">
            <span class="label-txt">products</span>
        </span>
       
        <div class="content-area">
   
      <div class="content drag-desired">
             
                <?php

    $result = mysql_query("select * from internet_shop");
    while($row=mysql_fetch_assoc($result))
    {
     echo '<div class="product"><img src="img/products/'.$row['img'].'" alt="'.htmlspecialchars($row['name']).'" width="128" height="128" class="pngfix" /></div>';
    }

    ?>
               
               
                <div class="clear"></div>
            </div>

        </div>
       
        <div class="bottom-container-border">
        </div>

    </div>

 

    <div class="container">
   
     <span class="top-label">
            <span class="label-txt">shopping cart</span>
        </span>
       
        <div class="content-area">
   
      <div class="content drop-here">
             <div id="cart-icon">
              <img src="img/shoppingcart_128x128.png" alt="shopping cart" class="pngfix" width="128" height="128" />
     <img src="img/ajax_load_2.gif" alt="loading.." id="ajax-loader" width="16" height="16" />
                </div>

    <form name="checkoutform" method="post" action="order.php">
               
                <div id="item-list">
                </div>
               
    </form>               
                <div class="clear"></div>

    <div id="total"></div>

                <div class="clear"></div>
               
                <a href="" onclick="document.forms.checkoutform.submit(); return false;" class="button">checkout</a>
               
          </div>

        </div>
       
        <div class="bottom-container-border">
        </div>

    </div>
 
</div>
</body>
</html>

tips.php

<?php

 

if(!$_post['img']) die("there is no such product!");

$img=mysql_real_escape_string(end(explode('/',$_post['img'])));

$row=mysql_fetch_assoc(mysql_query("select * from internet_shop where img='".$img."'"));

if(!$row) die("there is no such product!");

echo '<strong>'.$row['name'].'</strong>

<p class="descr">'.$row['description'].'</p>

<strong>price: $'.$row['price'].'</strong>
<small>drag it to your shopping cart to purchase it</small>';
?>

addtocard.php加入购物车

<?php

define('include_check',1);
require "../connect.php";

if(!$_post['img']) die("there is no such product!");

$img=mysql_real_escape_string(end(explode('/',$_post['img'])));
$row=mysql_fetch_assoc(mysql_query("select * from internet_shop where img='".$img."'"));

echo '{status:1,id:'.$row['id'].',price:'.$row['price'].',txt:'

<table width="100%" id="table_'.$row['id'].'">
  <tr>
    <td width="60%">'.$row['name'].'</td>
    <td width="10%">$'.$row['price'].'</td>
    <td width="15%"><select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option></slect>
 
 </td>
 <td width="15%"><a href="#" onclick="remove('.$row['id'].');return false;" class="remove">remove</a></td>
  </tr>
</table>'}';
?>

查看购物车页面

<div id="main-container">

    <div class="container">
   
     <span class="top-label">
            <span class="label-txt">your order</span>
        </span>
       
        <div class="content-area">
   
      <div class="content">
             
                <?php
    
    $cnt = array();
    $products = array();
    
    foreach($_post as $key=>$value)
    {
     $key=(int)str_replace('_cnt','',$key);
    
     $products[]=$key;
     $cnt[$key]=$value;
    }

    $result = mysql_query("select * from internet_shop where id in(".join($products,',').")");
    
    if(!mysql_num_rows($result))
    {
     echo '<h1>there was an error with your order!</h1>';
    }
    else
    {
     echo '<h1>you ordered:</h1>';
     
     while($row=mysql_fetch_assoc($result))
     {
      echo '<h2>'.$cnt[$row['id']].' x '.$row['name'].'</h2>';
      
      $total+=$cnt[$row['id']]*$row['price'];
     }
  
     echo '<h1>total: $'.$total.'</h1>';
    }
    ?>
               
               
                <div class="clear"></div>
            </div>

        </div>
       
        <div class="bottom-container-border">
        </div>

    </div>

</div>

源码下载地址。
http://down.111cn.net/down/code/php/qitayuanma/2010/1102/21586.html

关于文件上传我们讲了很多,这只是一款最基本的简单的文件上传功能,同时本教程也介绍了关于上传的原理以及各种函数的使用与file的参数说明,以及php.ihi设置上传文件大小配置等。

1  如果实现小文件的上传(2mb)一下是不需要对php教程.ini 中的配置进行修改的,如果要是大文件的上传就必须进行配置的修改

2 修改php.ini 中的内容有:

       post_max_size 指php通过表单post所能接收的最大值

       upload_max_filesize 指允许上传文件的最大值 

3 上传表单的设置

       <form  method=”post”  action=””  enctype=”multipart/form-data”>

          <input  name=”upfile”  type=”file”>

          <input  type=”hidden”  name=”max_file_size”  value=”1024000”>

       </form>

      解释: 1首先上传时entype属性必须设为multipart/form-data

             2 表单中最好加上 hidden隐藏域 name值为max_file_size  ,该隐藏域的作用不是真正去限制上传文件大小的,而是为了避免用户误传大文件而陷入无尽的等待中。 真正限制大小的还是刚才php.ini 中的两个修改项。

4 预定义变量$_files

         首先他是一个二维的数组,

          $_files[‘upfile’][‘name’]  上传文件的文件名

          $_files[‘upfile’][‘size’]   上传文件的大小

          $_files[‘upfile’][‘tmp_name’]  文件上传时,文件首先被保存为临时文件,改文件是临时文件名

          $_files[‘upfile’][‘type’]   上传文件的类型

          $_files[‘upfile’][‘error’]   错误代码

 

注意 $_files[‘upfile’][‘name’]  这里的upfile 是与表单<input  name=”upfile”  type=”file”> 的name对应,千万别错


5 具体实现 

 

  <?

     $filename=$_files['upfile']['name'];

        $tmp=$_files['upfile']['tmp_name'];

        $error=$_files['upfile']['error'];

        $path="./images/";

        if($error==0){

            if(is_uploaded_file($tmp)){

                  if(move_uploaded_file($tmp,$path.$filename)){

                          echo "上传成功!";

 

                     }else{

                          echo "<script> alert('文件不合法');history.go(-1);</script>";

 

                     }

 


               }else{

                  echo "<script> alert('非法操作!');history.go(-1);</script>";

 

               }


        }else{

 

               echo"<script> alert('上传错误,错误类型:".$error."');history.go(-1);</script>";

 

        }

 

?>

 

 

在 用户注册检测用户名是否存在我们要提供告诉用户你要注册的用户名是否可用,那么我们就得利用ajax技术来实例,下面是一款ajax php当用户输入完用户名时提示用户是否可用用的代码

<!--
t.php代码

 代码如下 复制代码

<?
$title = isset($_get['title'])?$_get['title']:'';
if( $title )
{
 $sql ='select id from filecontent where title=''.$title.''';
 $q = mysql教程_query( $sql ) or die( mysql_error());
 if( mysql_num_rows( $q )  )
 {
  echo 1;
 }
 else
 {
  echo 0;
 }
}
else
{
 echo 0;
}
?>

 

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title> 用户注册检测用户名是否存在ajax + php代码</title>
<script>
//  用户注册检测用户名是否存在ajax + php代码


function createxmlhttprequest(){//创建xmlhttprequest对象
 if(window.activexobject){//ie
  try {
   return new activexobject("microsoft.xmlhttp");
  } catch(e){
   return;
  }
 }else if(window.xmlhttprequest){//mozilla,firefox
  try {
   return new xmlhttprequest();
  } catch(e){
   return;
  }
 }
}

function getrenews(value){//主调函数
 var xmlhttp=createxmlhttprequest();
 var url = "t.php?action=check&title="+value+"&mt="+math.random(300000);
 if (value==""){  
  return false ;
 }
 if (xmlhttp){
  callback = getreadystatehandler(xmlhttp);
  xmlhttp.onreadystatechange = callback;
  xmlhttp.open("get", url,true);
  xmlhttp.send(null);
 }
}
//返回0代表用户名可用,否则提示己被注册。

function getreadystatehandler(xmlhttp){//服务器返回后处理函数
 return function (){
  if(xmlhttp.readystate == 4){
   if(xmlhttp.status == 200){
       
     
     if (xmlhttp.responsetext==1){
       document.getelementbyid("checkid").innerhtml="<font color='red'>对不起,你输入的用户名己被注册!</font>";     
     }else{
      document.getelementbyid("checkid").innerhtml="可以注册";     
     }      
   }
  }
 }
}
</script>
</head>

<body>
给input框增加onblur事件,当用户输入完用户名就检测用户名,并给出提示。
输入用户名<input name="title" type="text" id="title" size="40" onblur="getrenews(this.value);"><span id="checkid"></span>
</body>
</html>

定义和用法
mkdir() 函数创建目录。

若成功,则返回 true,否则返回 false。

语法
mkdir(path,mode,recursive,context)参数 描述
path 必需。规定要创建的目录的名称。
mode 必需。规定权限。默认是 0777。
recursive 必需。规定是否设置递归模式。
context 必需。规定文件句柄的环境。context 是可修改流的行为的一套选项。

 代码如下 复制代码

<?php
mkdir("testing");
?>

删除目录

定义和用法
rmdir() 函数删除空的目录。

若成功,则该函数返回 true。若失败,则返回 false。

语法
rmdir(dir,context)参数 描述
dir 必需。规定要删除的目录。
context 必需。规定文件句柄的环境。context 是可修改流的行为的一套选项。

说明
尝试删除 dir 所指定的目录。 该目录必须是空的,而且要有相应的权限。

 代码如下 复制代码
<?php
$path = "images";
if(!rmdir($path))
  {
  echo ("could not remove $path");
  }
?>

 

 代码如下 复制代码
<?php
$dirfile="文件夹";
$dirfile=iconv("utf-8","gb2312",$dirfile);//转码,否则会看到windows里面是乱码,但程序能正常操作,读取目录时反过来才看到目录的真正名字。
if(!file_exists($dirfile))//用于判断目录或文件是否存在
mkdir($dirfile);//创建目录
rmdir($dirfile);//删除目录,必须为空目录,否则要先删除里面的所有文件,后面有删除方法
echo "<br>";
?>

 文件创建、删除、读取、转数组

 

 代码如下 复制代码
<?php
$filename="文件.txt";
$filename=iconv("utf-8","gb2312",$filename);//转码,否则会看到windows里面是乱码
file_put_contents($filename,'');//自动创建空文件,如果已存在则删除再创建,具体可以增加file_exists判断,比fopen、fputs、fclose等函数简单。
unlink($filename);//注意文件名都是gb2312编码
file_put_contents($filename,"大家好!rn大家好啊!",file_append);
//看到没,写入两行, 第三个参数可选,表示是以增加方式写入,否则清空内容再写入
echo file_get_contents($filename);//忽略换行读取整个文件
echo "<br>";
$arr=file($filename);//文件按行读到数组里
print_r($arr);
echo "<br>";
readfile($filename);//文件直接输出到屏幕
echo "<br>";
?>

获取url信息、客户端ip地址

 代码如下 复制代码

<?php
//获取域名或主机地址
echo $_server['http_host']."<br>";
//获取网页地址(中间部分)
echo $_server['php_self']."<br>";
//获取网址参数(?后面部分)
echo $_server["query_string"]."<br>";
//来源客户端ip地址
if($_server['http_client_ip']){
$onlineip=$_server['http_client_ip'];
}elseif($_server['http_x_forwarded_for']){
$onlineip=$_server['http_x_forwarded_for'];
}else{
$onlineip=$_server['remote_addr'];
}
echo $onlineip;
echo "<br>";
?>

获取文件修改时间戳、遍历目录文件

 

 代码如下 复制代码
<?php
$filename="文件.txt";
$filename=iconv("utf-8","gb2312",$filename);
$passtime=time()-filectime($filename);//创建时间差,不准,一般不用
echo $passtime;
echo "<br>";
$passtime=time()-filemtime($filename);//修改时间差,用于更新判断,缓冲等判断
echo $passtime;
echo "<br>";
$dir="../";
print_r($arr=scandir($dir));//获得主目录的所有文件和文件夹名称
foreach($arr as $value){
if (!is_dir($dir.$value)) //是否目录,目录还包括"."、".."两个数组,通过判断可以知道是文件还是目录,以及是什么类型的后序名
echo iconv("gb2312","utf-8",$value)."<br>rn";
}
?>

详细实例

 

 代码如下 复制代码

$test="bb|cccs|aaa";

  $array =explode('|',$test);
   $j =1;
   foreach($array as $key){    
    $vote='vote_r'.$j;
    if(0==$rs['vote_times']){
     $n=1;
    }else{
     $n=$rs['vote_times'];www.111cn.net    }
    echo $j,'、',$key,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',$rs["$vote"],'票&nbsp;&nbsp;&nbsp;',($rs["$vote"]/$n)*100,'%<br>';
    $j++;
   
   }

一款免费的jquery php ajax 投票程序源码,你可以在本教程最后找到这款ajax投票源码下载地址免费下载。
 代码如下 复制代码

/* database config */

$db_host  = '';
$db_user  = '';
$db_pass  = '';
$db_database  = '';

/* end config */

 

$link = mysql教程_connect($db_host,$db_user,$db_pass) or die('unable to establish a db connection');

mysql_select_db($db_database,$link);
mysql_query("set names utf8");

?>


<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>jquery php ajax 投票程序源码</title>

<link rel="stylesheet" type="text/css教程" href="demo.css" />

<script type="text/网页特效" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>

<script type="text/javascript" src="script.js"></script>

</head>

<body>

<div id="main">
 

 <hr />

 
<?php

// checking whether the user has voted today:

$voted=false;

$vcheck=mysql_query(" select 1 from sort_votes
      where ip='".$_server['remote_addr']."'
      and date_submit=curdate()");

if(mysql_num_rows($vcheck)==1)
 $voted=true;

// if we are not on the data.php?results page:
if(!array_key_exists('results',$_get))
{
 echo '<ul class="sort">';

 // showing the tutorials by random
 $res = mysql_query("select * from sort_objects order by rand()");

 while($row=mysql_fetch_assoc($res))
 {?>
     <li id="li<?php echo $row['id']?>">
  <div class="tut">
            <div class="tut-img">
                <img src="<?php echo $row['img']?>" width="100" height="100" alt="<?php echo $row['title']?>" />
                <div class="drag-label"></div>
            </div>
           
            <div class="tut-title">
             <a href="<?php echo $row['url']?>" target="_blank" title="open it in a new window!"><?php echo $row['title']?></a>
            </div>
           
            <div class="tut-description"><?php echo $row['description']?></div>
            <div class="clear"></div>
        </div>
        </li>
 <?php } ?>
   
   </ul>
 
 <div class="button-holder">
     <?php if(!$voted):?><a href="" id="submitpoll" class="button">submit poll<span></span></a><?php endif;?>
        <a href="?results" class="button">view the results<span></span></a>
    </div>
 
<?php
}
else require "results.php";
// the above require saves us from having to style another separate page

?>
   
 <div class="clear"></div>   
   

<!-- the form below is not directly available to the user -->

<form action="?results" id="sform" method="post">
<input name="sortdata" id="sortdata" type="hidden" value="" />
</form>

</body>
</html>


results.php

if($_post['sortdata'])
{
 // the data arrives as a comma-separated string,
 // so we extract each post ids:
 $data=explode(',',str_replace('li','',$_post['sortdata']));

 // getting the number of objects
 list($tot_objects) = mysql_fetch_array(mysql_query("select count(*) from sort_objects"));

 if(count($data)!=$tot_objects) die("wrong data!");

 foreach($data as $k=>$v)
 {
  // building the sql query:
  $str[]='('.(int)$v.','.($tot_objects-$k).')';
 }
 
 $str = 'values'.join(',',$str);
 
 // this will limit voting to once a day per ip:
 mysql_query(" insert into `sort_votes` (ip,date_submit,dt_submit)
     values ('".$_server['remote_addr']."',now(),now())");

 // if the user has not voted before today:
 if(mysql_affected_rows($link)==1)
 {
  mysql_query(' insert into `sort_objects` (id,votes) '.$str.'
      on duplicate key update votes = votes+values(votes)');
 }
}

// selecting the sample tutorials and ordering
// them by the votes each of them received:
$res = mysql_query("select * from sort_objects order by votes desc");

$maxvote=0;
$bars=array();

while($row=mysql_fetch_assoc($res))
{
 $bars[]=$row;
 
 // storing the max vote, so we can scale the bars of the chart:
 if($row['votes']>$maxvote) $maxvote = $row['votes'];
}

$barstr='';

// the colors of the bars:
$colors=array('#ff9900','#66cc00','#3399cc','#dd0000','#800080');

foreach($bars as $k=>$v)
{
 // buildling the bar string:
 $barstr.='
  <div class="bar" style="width:'.max((int)(($v['votes']/$maxvote)*450),100).'px;background:'.$colors[$k].'">
   <a href="'.$v['url'].'" title="'.$v['title'].'">'.$v['short'].'</a> 
  </div>';
}

// the total number of votes cast in the poll:
list($totvotes) = mysql_fetch_array(mysql_query("select count(*) from sort_votes"));

?>


<div class="chart">

<?php echo $barstr?>

</div>

<a href="demo.php" class="button">go back<span></span></a>

<div class="tot-votes"><?php echo $totvotes?> votes</div>

源码下载


http://down.111cn.net/down/code/php/toupiaodiaocha/2010/1028/21481.html

[!--infotagslink--]

相关文章

  • ASP.NET购物车实现过程详解

    这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • jQuery 2.0.3 源码分析之core(一)整体架构

    拜读一个开源框架,最想学到的就是设计的思想和实现的技巧。废话不多说,jquery这么多年了分析都写烂了,老早以前就拜读过,不过这几年都是做移动端,一直御用zepto, 最近抽出点时间把jquery又给扫一遍我也不会照本宣科的翻译...2014-05-31
  • JS实现购物车中商品总价计算

    这篇文章主要为大家详细介绍了JS实现购物车中商品总价的计算 ,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-07
  • vue3源码剖析之简单实现方法

    源码的重要性相信不用再多说什么了吧,特别是用Vue 框架的,一般在面试的时候面试官多多少少都会考察源码层面的内容,下面这篇文章主要给大家介绍了关于vue3源码剖析之简单实现的相关资料,需要的朋友可以参考下...2021-09-07
  • js实现div在页面拖动效果

    这篇文章主要介绍了js实现div在页面拖动效果,涉及JavaScript动态操作页面元素与数值计算的相关技巧,需要的朋友可以参考下...2016-05-05
  • React列表栏及购物车组件使用详解

    这篇文章主要为大家详细介绍了React列表栏及购物车组件使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-06-28
  • Underscore源码分析

    Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象。这篇文章主要介绍了underscore源码分析相关知识,感兴趣的朋友一起学习吧...2016-01-02
  • vue实现简单购物车案例

    这篇文章主要为大家详细介绍了vue实现简单购物车案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-06-01
  • jQuery实现加入购物车飞入动画效果

    HTML 首先载入jQuery库文件和jquery.fly.min.js插件。复制代码 代码如下: <script src="jquery.js"></script> <script src="jquery.fly.min.js"></script> 接着,将商品信息html结构布置好,本例中,我们用四个商品并排...2015-03-15
  • 基于jquery fly插件实现加入购物车抛物线动画效果

    在购物网站中,加入购物车的功能是必须的功能,有的网站在用户点击加入购物车按钮时,就会出现该商品从点击出以抛物线的动画相似加入购物车,这个功能看起来非常炫,对用户体验也有一定的提高。下面介绍基于jquery fly插件实现加入购物车抛物线动画效果...2016-04-06
  • jQuery实现鼠标拖动div改变位置、大小的实践

    这篇文章主要介绍了jQuery实现鼠标拖动div改变位置、大小的实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-10
  • jQuery使用cookie与json简单实现购物车功能

    这篇文章主要介绍了jQuery使用cookie与json简单实现购物车功能的方法,介绍了jQuery实现购物车的步骤与关键代码,需要的朋友可以参考下...2016-04-18
  • 一文帮你理解PReact10.5.13源码

    这篇文章主要介绍了一文帮你理解PReact10.5.13源码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-04
  • Android Studio如何查看源码并调试的方法步骤

    这篇文章主要介绍了Android Studio如何查看源码并调试的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-05-15
  • b2c网站购物车的设计思路

    对于大部分B2C网站来说,购物车是网站的咽喉之地,订单是白花花的银子,所有银子都必然流经购物车,购物车不能有失。优秀的购物车设计至少需要完成两项使命:一是方便用户多买...2016-09-20
  • 帝国CMS商品加入购物车链接代码

    商品加入购物车链接地址为:[!--news.url--]e/ShopSys/doaction.php?enews=AddBuycar&classid=[!--classid--]&id=[!--id--]...2015-12-30
  • 原生js实现购物车

    这篇文章主要为大家详细介绍了原生js实现购物车,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-09-24
  • jQuery基于json与cookie实现购物车的方法

    这篇文章主要介绍了jQuery基于json与cookie实现购物车的方法,涉及jQuery操作json格式数据与cookie存储购物车信息的相关技巧,需要的朋友可以参考下...2016-04-18
  • jQuery实现的购物车物品数量加减功能代码

    这篇文章主要介绍了jQuery实现的购物车物品数量加减功能,涉及jQuery针对鼠标事件的响应及页面元素动态操作相关技巧,需要的朋友可以参考下...2016-11-22