Php 如何获取动态下拉列表的搜索结果计数?

Php 如何获取动态下拉列表的搜索结果计数?,php,mysql,ajax,Php,Mysql,Ajax,我在PHP中有一个计数问题 我正在从MySQL动态创建一个下拉列表。当我选择第一个下拉菜单时,如何获得搜索结果计数。。。比如数据库中的10个产品 这是我的密码: ajax..文件: var xmlHttp function showCount(str) { xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } var url="p

我在PHP中有一个计数问题

我正在从MySQL动态创建一个下拉列表。当我选择第一个下拉菜单时,如何获得搜索结果计数。。。比如数据库中的10个产品

这是我的密码:

ajax..文件:

var xmlHttp

function showCount(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Your browser does not support AJAX!");
 return;
 }
var url="phpApplication.php";
url=url+"?action=count2";
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("countR").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
   {
   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
   }
 catch (e)
   {
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 }
return xmlHttp;
}
PHP应用程序:

class Application {


     public function __construct () { 

    } 


    function conn($sql)
{   

      $host      = "localhost";
      $username  = "root";
      $pwd       = "";
      $dbname    = "example";

    //echo "commnecing connection to local db<br>"
    if (!($conn=mysql_connect($host, $username, $pwd)))  {
        printf("error connecting to DB by user = $username and pwd=$pwd");
        exit;
    }
    $db3=mysql_select_db($dbname,$conn) or die("Unable to connect to local database");


    $result = mysql_query($sql) or die ("Can't connect because ". mysql_error());

    return $result;

}//end function  


function dropdown($field, $table)
{ 
  //initialize variables
  $oHTML  = '';
  $result = '';

  //check to see if the field is passed correctly
  if (($field == "")||($table == ""))
  {
    die("No column or table specified to create drop down from!");
  }

  $sql = "select distinct($field) from $table";

  //call the db function and run the query
  $result = $this->conn($sql);

  //if no results are found to create a drop down return a textbox
  if ((!$result) ||(mysql_num_rows($result)==0))
  {
    $oHTML .= "<input type='text' name='$field' value='' size='15'>";
  }elseif (($result)&&(mysql_num_rows($result)>0)){


    //build the select box out of the results
    $oHTML .= "<select name='$field' onchange='showCount(this.value)'>\n<option value='all'>All</option>\n";
    while ($rows = mysql_fetch_array($result))
    {
      $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n";

    }
    $oHTML .= "</select>\n";

  }
  //send the value back to the calling code
  return $oHTML;
}//end function


function countResult(){

  $sql2 = "select * from produkt_finder_table where 1 ";


  if ( $q == (!empty($_POST['Bauform_d']))&&($_POST['Bauform_d'] != 'all'))
  {
    $sql2 .= " and Bauform_d like '". addslashes($_POST['Bauform_d'])."%' ";
  }


  if ((!empty($_POST['Beschreibung_d']))&&($_POST['Beschreibung_d'] != 'all'))
  {
    $sql2 .= " and Beschreibung_d like '". addslashes($_POST['Beschreibung_d'])."%' ";
  }


   if ((!empty($_POST['SelectSchutzart_Cust_Bereich_d']))&&($_POST['SelectSchutzart_Cust_Bereich_d'] != 'all'))
  {
    $sql2 .= " and SelectSchutzart_Cust_Bereich_d like '". addslashes($_POST['SelectSchutzart_Cust_Bereich_d'])."%' ";
  }


   if ((!empty($_POST['SelectErfassungswinkel_Cust_Bereich_d']))&&($_POST['SelectErfassungswinkel_Cust_Bereich_d'] != 'all'))
  {
    $sql2 .= " and SelectErfassungswinkel_Cust_Bereich_d like '". addslashes($_POST['SelectErfassungswinkel_Cust_Bereich_d'])."%' ";
  }


   if ((!empty($_POST['ArtikelProduktgruppe_d']))&&($_POST['ArtikelProduktgruppe_d'] != 'all'))
  {
    $sql2 .= " and ArtikelProduktgruppe_d like '". addslashes($_POST['ArtikelProduktgruppe_d'])."%' ";
  }



$result2 = $this->conn($sql2);
  //run query

  if (!$result2){ die("No results due to database error.<br>".mysql_error());  }

  if (mysql_num_rows($result2)==0)
  {
    echo "0";
  }else{

    echo mysql_num_rows($result2);
}
}



function count1(){

   if ((!empty($_POST['Bauform_d']))&&($_POST['Bauform_d'] != 'all')) {


}

    $sql2 .= " and Bauform_d like '". addslashes($_POST['Bauform_d'])."%' ";
     $sql2 = "select * from produkt_finder_table where 1 ";
     $result2 = $this->conn($sql2);
      if (!$result2){ die("No results due to database error.<br>".mysql_error());  }

  if (mysql_num_rows($result2)==0)
  {
    echo "0";
  }else{

    echo mysql_num_rows($result2);
  }


  //run query



}



function countResultAll(){

 $sql = "select * from produkt_finder_table where 1 ";



  if ((!empty($_POST['searchfield']))&&($_POST['searchfield'] != 'all'))
  {
    $sql .= " and ArtikelName like '". addslashes($_POST['searchfield'])."%' ";
  }

$result = $this->conn($sql);
  //run query

  if (!$result){ die("No results due to database error.<br>".mysql_error());  }

  if (mysql_num_rows($result)==0)
  {
    echo "0";
  }else{

    echo mysql_num_rows($result);
}
}



function search()
{

//base sql
  $sql = "select * from produkt_finder_table where 1 ";

//get the values from the form


  if ((!empty($_POST['Bauform_d']))&&($_POST['Bauform_d'] != 'all'))
  {
    $sql .= " and Bauform_d like '". addslashes($_POST['Bauform_d'])."%' ";
  }

  if ((!empty($_POST['Beschreibung_d']))&&($_POST['Beschreibung_d'] != 'all'))
  {
    $sql .= " and Beschreibung_d like '". addslashes($_POST['Beschreibung_d'])."%' ";
  }

   if ((!empty($_POST['SelectSchutzart_Cust_Bereich_d']))&&($_POST['SelectSchutzart_Cust_Bereich_d'] != 'all'))
  {
    $sql .= " and SelectSchutzart_Cust_Bereich_d like '". addslashes($_POST['SelectSchutzart_Cust_Bereich_d'])."%' ";
  }

   if ((!empty($_POST['SelectErfassungswinkel_Cust_Bereich_d']))&&($_POST['SelectErfassungswinkel_Cust_Bereich_d'] != 'all'))
  {
    $sql .= " and SelectErfassungswinkel_Cust_Bereich_d like '". addslashes($_POST['SelectErfassungswinkel_Cust_Bereich_d'])."%' ";
  }

   if ((!empty($_POST['ArtikelProduktgruppe_d']))&&($_POST['ArtikelProduktgruppe_d'] != 'all'))
  {
    $sql .= " and ArtikelProduktgruppe_d like '". addslashes($_POST['ArtikelProduktgruppe_d'])."%' ";
  }


 //run query
  $result = $this->conn($sql);

  if (!$result){ die("No results due to database error.<br>".mysql_error());  }


  if (mysql_num_rows($result)==0)
  {
    echo "No Results found!";
  }else{


     echo "<table border='1' class='corner'><th width='332'><a href='product.php'>Back</a></th>";
     echo "<th>Product Result</th>";
     echo "<td width='40' align='center' style=' font-size:16px'><div id='countR'>";
     $this->countResult();
     echo "</div></td>";
        echo "</table>";

    // echo '<div style="float:right; font-size:16px">'.$this->countResultAll().'</div>';
    // echo "<div style='float:left; font-size:16px'>Produkt Result</div>";

    echo "<table border='1' class='corner'>"; 

     $i = 1;
    if((mysql_num_rows($result) % 2) == 0){
    $complete = false;
     }else{
    $complete = true;
     }

    while ($rows= mysql_fetch_array($result))
    {

      echo "<td>";
      echo "<ul><li class='addResult'><span class='red'>".$rows['Bauform_d']."<br /><span class='black'>".$rows['ArtikelName']."</span></span><img src='{imag}' alt='' /> <a href='#'>More Info</a> </li></ul>";
      echo "</td>";

   if(($i % 2) == 0){

       echo "</tr><tr>";
    }   

      $i++;
}
if($complete == true){

    echo "<td>&nbsp;</td>";

}     


    echo "</tr></table>";
      echo "<table border='1' class='corner'><th width='332'><a href='product.php'>Back</a> <div></div></th>";
     echo "<th>Product Result</th>";
     echo "<td width='40' align='center' style=' font-size:16px'>";
     $this->countResult();
     echo "</td>";
        echo "</table>";

}
}


function show_form()
{

  $Bauform   = $this->dropdown('Bauform_d','produkt_finder_table');
  $Beschreibung  = $this->dropdown('ArtikelGruppe_d','produkt_finder_table');
   $SelectSchutzart  = $this->dropdown('SelectSchutzart_Cust_Bereich_d','produkt_finder_table');
    $SelectErfassungswinkel  = $this->dropdown('SelectErfassungswinkel_Cust_Bereich_d','produkt_finder_table');
     $ArtikelProduktgruppe  = $this->dropdown('ArtikelProduktgruppe_d','produkt_finder_table');

  echo "<form name='search' action='productResult.php?action=search' method='POST'>
        <table width='50%' align='center' valign='center' class='corner'>
        <tr>
          <td colspan='2' align='center'> Search Form  <div id='countR' style='float:right; font-size:16px'>Count</div></td>
        </tr>
        <tr>
          <td align='right'>Montageart:</td><td>$Bauform</td>
        </tr>
         <tr>
          <td align='right'>ProduktGruppe:</td><td>$Beschreibung</td>
        </tr>
         <tr>
          <td align='right'>Speisung:</td><td>$SelectSchutzart</td>
        </tr>
        <tr>
          <td align='right'>Reichweite:</td><td> $SelectErfassungswinkel </td>
        </tr>
        <tr>
          <td align='right'>Erfwinkel:</td><td>$ArtikelProduktgruppe </td>
        </tr>

        <tr>
           <td colspan='2' align='center'><input type='submit' name='submit' value='Go!'></td>
        </tr>
        </table>
        </form>";

}//end function




function searchAll()
{

//base sql
  $sql = "select * from produkt_finder_table where 1 ";

//get the values from the form
//NOTE: You should do way more valdation on the values before you attempt to process anything


  if ((!empty($_POST['searchfield']))&&($_POST['searchfield'] != 'all'))
  {
    $sql .= " and ArtikelName like '". addslashes($_POST['searchfield'])."%' ";
  }

  //run query
  $result = $this->conn($sql);

  if (!$result){ die("No results due to database error.<br>".mysql_error());  }


  if (mysql_num_rows($result)==0)
  {
    echo "No Results found!";
  }else{


     echo "<table border='1' class='corner'><th width='332'><a href='product.php'>Back</a></th>";
     echo "<th>Product Result</th>";
     echo "<td width='40' align='center' style=' font-size:16px'><div id='countR'>";
     $this->countResultAll();
     echo "</div></td>";
     echo "</table>";

    // echo '<div style="float:right; font-size:16px">'.$this->countResultAll().'</div>';
    // echo "<div style='float:left; font-size:16px'>Produkt Result</div>";

    echo "<table border='1' class='corner'>"; 

     $i = 1;
    if((mysql_num_rows($result) % 2) == 0){
    $complete = false;
     }else{
    $complete = true;
     }

    while ($rows= mysql_fetch_array($result))
    {

      echo "<td>";
      echo "<ul><li class='addResult'><span class='red'>".$rows['Bauform_d']."<br /><span class='black'>".$rows['ArtikelName']."</span></span><img src='{imag}' alt='' /> <a href='#'>More Info</a> </li></ul>";
      echo "</td>";

   if(($i % 2) == 0){

       echo "</tr><tr>";
    }   

      $i++;
}
if($complete == true){

    echo "<td>&nbsp;</td>";

}     
     echo "</tr></table>";
     echo "<table border='1' class='corner'><th width='332'><a href='product.php'>Back</a> <div></div></th>";
     echo "<th>Product Result</th>";
     echo "<td width='40' align='center' style=' font-size:16px'>";
     $this->countResultAll();
     echo "</td>";
     echo "</table>";

}
}
}

$action = $_GET['action'];

$proFin = new Application();

switch($action) {

        case 'show':
            $proFin->show_form();
        break;

        case 'search':
            $proFin->search();
        break; 

        case 'searchAll':
            $proFin->searchAll();
        break; 

        case 'count':
            $proFin->countResult();
        break; 

        case 'count1':
            print 'hallo';
        break; 

        case 'count2':

            if (!empty($_POST))
{


        var_dump($_POST);
}


        break; 

        case 'count3':
            echo $rows[$field];
        break; 
        }
类应用程序{
公共函数u_构造(){
} 
函数conn($sql)
{   
$host=“localhost”;
$username=“root”;
$pwd=“”;
$dbname=“示例”;
//echo“与本地数据库的通信连接
” 如果(!($conn=mysql\u connect($host、$username、$pwd))){ printf(“用户连接数据库时出错=$username,pwd=$pwd”); 出口 } $db3=mysql\u选择\u db($dbname,$conn)或die(“无法连接到本地数据库”); $result=mysql\u query($sql)或die(“无法连接,因为“.mysql\u error()”); 返回$result; }//端函数 函数下拉列表($field,$table) { //初始化变量 $oHTML=''; $result=''; //检查字段是否正确传递 如果(($field==”)| |($table==”) { die(“没有指定要从中创建下拉列表的列或表!”); } $sql=“从$table中选择不同的($field)”; //调用db函数并运行查询 $result=$this->conn($sql); //如果未找到创建下拉列表的结果,则返回文本框 if((!$result)| |(mysql_num_rows($result)==0)) { $oHTML=”; }elseif(($result)&(mysql_num_rows($result)>0)){ //根据结果构建选择框 $oHTML.=“\nAll\n”; while($rows=mysql\u fetch\u array($result)) { $oHTML.=“”.$rows[$field]。“\n”; } $oHTML.=“\n”; } //将值发送回调用代码 返回$oHTML; }//端函数 函数countResult(){ $sql2=“从produkt\u finder\u表中选择*,其中1”; 如果($q==(!空($\u POST['Bauform\u d'])和($\u POST['Bauform\u d'])!='all')) { $sql2.=“and Bauform_d like”“”.addslashes($\u POST['Bauform_d'])。“%”; } 如果((!空($_POST['Beschreibung\u d'])和($_POST['Beschreibung\u d'])!='all')) { $sql2.=“和Beschreibung_d like”“”.addslashes($\u POST['Beschreibung_d'])。“%”; } 如果(!空($发布['SelectSchutzart\u Cust\u Bereich\u d'])和($发布['SelectSchutzart\u Cust\u Bereich\u d']!='all')) { $sql2.=“并选择Schutzart_Cust_Bereich_d like”“。添加斜杠($\u POST['SelectSchutzart_Cust_Bereich_d'])。“%”; } 如果(!空($\u POST['SelectErfassungswinkel\u Cust\u Bereich\u d'])和($\u POST['SelectErfassungswinkel\u Cust\u Bereich\u d']!='all')) { $sql2.=“and selecterPassungSwinkel\u Cust\u Bereich\u d like”“。添加斜杠($\u POST['selecterPassungSwinkel\u Cust\u Bereich\u d'])。”%”; } 如果(!空($_POST['ArtikelProduktgruppe_d'])和($_POST['ArtikelProduktgruppe_d']!='all')) { $sql2.=“和ArtikelProduktgruppe_d like'”.addslashes($_POST['ArtikelProduktgruppe_d'])。“%”; } $result2=$this->conn($sql2); //运行查询 if(!$result2){die(“由于数据库错误而没有结果。
”.mysql_error());} if(mysql_num_rows($result2)==0) { 回声“0”; }否则{ echo mysql_num_行($result2); } } 函数count1(){ 如果((!空($_POST['Bauform\u d'])和($_POST['Bauform\u d']!='all')){ } $sql2.=“and Bauform_d like”“”.addslashes($\u POST['Bauform_d'])。“%”; $sql2=“从produkt\u finder\u表中选择*,其中1”; $result2=$this->conn($sql2); if(!$result2){die(“由于数据库错误而没有结果。
”.mysql_error());} if(mysql_num_rows($result2)==0) { 回声“0”; }否则{ echo mysql_num_行($result2); } //运行查询 } 函数countResultAll(){ $sql=“从produkt\u finder\u表中选择*,其中1”; 如果((!空($\u POST['searchfield'])和($\u POST['searchfield']!='all')) { $sql.=“和ArtikelName类似'”.addslashes($\u POST['searchfield'])。“%”; } $result=$this->conn($sql); //运行查询 if(!$result){die(“由于数据库错误而没有结果。
”.mysql_error());} if(mysql_num_rows($result)==0) { 回声“0”; }否则{ echo mysql_num_行($result); } } 函数搜索() { //基本sql $sql=“从produkt\u finder\u表中选择*,其中1”; //从表单中获取值 如果((!空($_POST['Bauform\u d'])和($_POST['Bauform\u d']!='all')) { $sql.=“and Bauform_d like”“”.addslashes($\u POST['Bauform_d'])。“%”; } 如果((!空($_POST['Beschreibung\u d'])和($_POST['Beschreibung\u d'])!='all')) { $sql.=“和Beschreibung_d like”“”.addslashes($\u POST['Beschreibung_d'])。“%”; } 如果(!空($发布['SelectSchutzart\u Cust\u Bereich\u d'])和($发布['SelectSchutzart\u Cust\u Bereich\u d']!='all')) { $sql.=“并选择Schutzart_Cust_Bereich_d like”“。添加斜杠($\u POST['SelectSchutzart_Cust_Bereich_d'])。“%”; } 如果(!空($\u POST['SelectErfassungswinkel\u Cust\u Bereich\u d'])和($\u POST['SelectErfassungswinkel\u Cust\u Bereich\u d']!='all')) { $sql.=“and selecterPassungSwinkel_Cust_Bereich_d’”。添加斜杠($\u POST['selecterPassungSwinkel_Cust_Bereich_d'])。“%”; } 如果(!空($_POST['ArtikelProduktgruppe_d'])和($_POST['ArtikelProduktgruppe_d']!='all')) { $sql.=“和ArtikelProduktgruppe_d like”“.addslashes($_POST['ArtikelProduktgruppe_d'])。“%”; } //运行查询 $result=$this->conn($sql); if(!$result){die(“由于数据库错误而没有结果。
”.mysql_error());} if(mysql_num_rows($result)==0) { 回显“未找到结果!”; }否则{ 回声“; 呼应“产品结果”; 回声“; $this->countResult(); 回声“; 回声“; //回显“”。$this->countResultAll(); //回显“生产结果”; 回声“; $i=1; if((mysql_num_rows($result)%2)=0){ $complete=false; }否则{ $complete=true; } while($rows=mysql\u fetch\u array($result)) { 回声“; echo“
  • ”$rows['Bauform_d']。”
    “$rows['ArtikelName']。”
”; 回声“; 如果($i%2)=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.w3.org/1999/xhtml">
<head>
<title>Test ajax</title>
</head>
<script language="javascript">
var xmlHttp

function showCount(str)
{
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }
    var url="testajax.php";
    url=url+"?action=count2";
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("POST",url,true);
    xmlHttp.send(null);
}

function stateChanged()
{
    if (xmlHttp.readyState==4)
    {
        document.getElementById("countR").innerHTML=xmlHttp.responseText;
    }
}

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
</script>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#000080" alink="#FF0000" onload="showCount( 'heelo' )" >
<span id="countR">empty</span>
</body>
</html>
<?

if( $_REQUEST[ 'action' ] == 'count2' )
{
    header( 'Content-type: text/xml' );
    echo <<<XMLTOP
<?xml version="1.0" encoding="UTF-8"?>

XMLTOP;
        echo( '<countr>23</countr>' );
}
?>
$query = "SELECT SQL_CALC_FOUND_ROWS ...";

$result = mysql_query( $query ) or LogFatalError( "Database error: " . mysql_error() );
$count  = mysql_result( mysql_query( 'SELECT FOUND_ROWS()' ), 0, 0 );