我有3个不同的表,我想用3个不同的按钮显示,但我不知道它是否可以使用php和sql?到目前为止,我一直在努力,但我似乎无法想到逻辑。 ' tamp_mall',' tamp1',' century_square'是我的桌子。
我的PHP和sql是最基本的,任何帮助都将不胜感激。
<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);
答案 0 :(得分:0)
创建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;
}
?>
希望这能帮到你!
答案 1 :(得分:0)
请在下面查看我的逻辑。
<强> PHP:强>
<?php
/* 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");
$tamp_mallData = getData('tamp_mall', $link);
$tamp1Data = getData('tamp1', $link);
$century_squareData = getData('century_square', $link);
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
//This function return data from db...
function getData($tblName, $link){
$returnData = array();
// Attempt select query execution
$sql = "SELECT KEYWORD, COUNT(*) Count_Duplicate
FROM $tblName
GROUP BY KEYWORD
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
LIMIT 5";
if ($result = mysqli_query($link, $sql)) {
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
$returnData = $row;
}
// 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);
}
return $returnData;
}
// Close connection
mysqli_close($link);
?>
<div class="btnDiv" id="tamp_mall">Data from '$tamp_mallData' table comes here</div>
<div class="btnDiv" id="tamp1">Data from '$tamp1Data' table comes here</div>
<div class="btnDiv" id="century_square">Data from '$century_squareData' table comes here</div>
<input type="button" name="tamp_mall" value="tamp_mall" id="btn_mall" />
<input type="button" name="tamp1" value="tamp1" id="btn_tmp1" />
<input type="button" name="century_square" value="century_square" id="btn_square" />
<强> JS:强>
<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>
我不想使用AJAX,因为没有必要在按钮点击时触发查询。
答案 2 :(得分:0)
非常简单。在你的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
}
希望这会有所帮助..