Php 通过单击不同的按钮显示不同的表
我有3个不同的表,我想用3个不同的按钮显示,但我不知道是否可以使用php和sql?到目前为止,我一直在挣扎,但我似乎想不出其中的逻辑。 “坦普广场”、“坦普1”和“世纪广场”是我的桌子 我的PHP和sql是最基本的,任何帮助将不胜感激Php 通过单击不同的按钮显示不同的表,php,mysql,sql,Php,Mysql,Sql,我有3个不同的表,我想用3个不同的按钮显示,但我不知道是否可以使用php和sql?到目前为止,我一直在挣扎,但我似乎想不出其中的逻辑。 “坦普广场”、“坦普1”和“世纪广场”是我的桌子 我的PHP和sql是最基本的,任何帮助将不胜感激 <form name="type" id="myForm" action="index.php" method="post"> <input type="submit" name="tamp_mall" value="tamp_mall">
<form name="type" id="myForm" action="index.php" method="post">
<input type="submit" name="tamp_mall" value="tamp_mall"> ;
<input type="submit" name="tamp1" value="tamp1"> ;
<input type="submit" name="century_square" value="century_square"> ;
</form>
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "csv_db");
$tmall = "tamp_mall";
$tamp1 = "tamp1";
$square = "century_square";
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT KEYWORD, COUNT(*) Count_Duplicate
FROM $square
GROUP BY KEYWORD
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
LIMIT 5";
if ($result = mysqli_query($link, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo "<table>";
echo "<tr>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['KEYWORD'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
创建onclick事件,在该事件中发送AJAX请求以获取该按钮上所需表的数据,然后以表格式显示结果
<button class = 'btn1'>B1</button>
<button class = 'btn2'>B1</button>
<button class = 'btn3'>B1</button>
<div class='disp_tab'></div>
<script>
$('.btn1').click(function(){
ajaxRequest('tab1');
});
$('.btn2').click(function(){
ajaxRequest('tab2');
});
$('.btn3').click(function(){
ajaxRequest('tab3');
});
function ajaxRequest(tab){
$.ajax({
method : 'POST', //can be GET or post
url : 'foo/foo/xyz.php', // URL to your php page
data : {data : tab}, // passing which tab to be displayed
dataType : 'json', //Data type is json
success : function(data){
//fetched data is processed to print in table format inside the div
},
error:function(x,y,z){
//error handling
},
});
}
</script>
PHP:foo/foo/xyz.PHP
<?php
$tab= $_POST['data'];
switch($tab){
case 'tab1':
//fetch content of table 1 into an array say $arr
echo json.encode($arr);
break;
case 'tab2':
//fetch content of table 2 into an array say $arr
echo json.encode($arr);
break;
case 'tab3':
//fetch content of table 4 into an array say $arr
echo json.encode($arr);
break;
}
?>
希望这对你有帮助 请检查下面我的逻辑 PHP: JS:
我不想使用AJAX,因为不需要在点击按钮时启动查询。非常简单。在php文件中,尝试以下操作:
//write connection code here like
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "csv_db");
$tmall = "tamp_mall";
$tamp1 = "tamp1";
$square = "century_square";
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if($_POST["tamp_mall"]){
//write you code to display from 'tamp_mall' table here
}elseif($_POST["tamp1"]){
//write you code to display from 'tamp1' table here
}else{
//write you code to display from 'century_square' table here
}
希望这有帮助。Hi,我应该把js放在哪一部分?我应该把它放在同一个php文件下吗?或者我必须创建一个单独的js文件并将其包含在php文件中吗?不,您可以在同一个php中使用。无需为此创建另一个文件。仅供参考,包括JQ文件。我执行了代码,但我得到的只是来自“$tamp_mallData”表的数据来自“$tamp1Data”表的数据来自“$century_squareData”表的数据来自这里,3按钮您是否按照我的指示只是为了获取信息,包括JQ文件。请检查浏览器控制台是否存在JS错误。也许它说$不是一个函数。这意味着您需要添加JQuery。
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
<script>
//Initially hide every div except first
$('.btnDiv').hide();
$('#tamp_mall').show();
//Hide/show div 'tamp_mall'
$('#btn_mall').on('click', function (){
$('.btnDiv').hide();
$('#tamp_mall').show();
});
//Hide/show div 'tamp1'
$('#btn_tmp1').on('click', function (){
$('.btnDiv').hide();
$('#tamp1').show();
});
//Hide/show div 'century_square'
$('#btn_square').on('click', function(){
$('.btnDiv').hide();
$('#century_square').show();
});
</script>
//write connection code here like
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "csv_db");
$tmall = "tamp_mall";
$tamp1 = "tamp1";
$square = "century_square";
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if($_POST["tamp_mall"]){
//write you code to display from 'tamp_mall' table here
}elseif($_POST["tamp1"]){
//write you code to display from 'tamp1' table here
}else{
//write you code to display from 'century_square' table here
}