gpt4 book ai didi

javascript - PHP 中的 JQuery 表

转载 作者:太空狗 更新时间:2023-10-29 15:13:21 26 4
gpt4 key购买 nike

我如何才能同时使用 sql 查询和 JQuery 来处理 php 文件?。最终目标是获取多个表作为 http://jsfiddle.net/96Lhog5g/3/演示但使用下面的 php 代码。类似这样但在 php 中 enter image description here-PHP代码:

    <?php
$dbconn = pg_connect(
$sql1 = "SELECT unaccent(name) from base1;";
$sql2 = "select id from servic;";

$name = pg_query($sql1);
$ident= pg_query($sql2);
$data1 = pg_fetch_all_columns($name);
$data2 = pg_fetch_all_columns($ident);
$count = count($data1);
echo '<table id="mainTable" border="1" style="width:450px;position:relative;left:80px;">';
echo '<tr >';
echo '<th style="background: #3498db; width:5px;text-align: center ;font-size:12px;font-family:Arial Narrow">ID</th>';
echo '<th style="background: #3498db; width:10px;text-align: center ;font-size:12px;font-family:Arial Narrow">NAME</th>';
echo '</tr>';
for ($i = 0; $i < $count; $i++) {
$data1[$i];
$data2[$i];
echo '<tr ><td>' . $data2[$i] . '</td>';
echo '<td>' . $data1[$i] . '</td></tr>';
}
echo '</table>';
pg_free_result($name);
pg_close($dbconn);
?>
  • JQuery 代码:

    var $main = $('#mainTable'),

    $head = $main.find('tr:first'),

    $extraRows = $main.find('tr:gt(2)');

    for( var i = 0; i < $extraRows.length; i = i+4){
    $('<table>').append($head.clone(),
    $extraRows.slice(i,i+2)).appendTo($main.parent());
    }

,来自http://jsfiddle.net/96Lhog5g/3/ .我曾尝试将此代码改编为 php,但我对它的了解有限。提前致谢。

最佳答案

在不知道数据结构的情况下很难判断。你肯定需要做一个 SQL join在两个数据库列上,以便您使用正确的数据。

如果没有 SQL 连接,您在两个查询中获得的数据可能长度不同,而且肯定不会匹配。 (即它会在不属于它的 ID 旁边显示名称)

PHP

/*
//This is an example SQL join, guessing at how your data is structured based on the queries used in your question.
$sql = "SELECT unaccent(base1.name),servic.id FROM base1 INNER JOIN servic ON base1.id = servic.id;";
$result = pg_query($sql);
$data = pg_fetch_all($result)
*/


//lacking any SQL data, we'll build a test dataset for now
$data = array();
function buildTestData() {
global $data;
for($i = 1; $i < 24; $i++) {
$testData = array("name" => "name ".chr($i+64), "id" => $i);
array_push($data,$testData);
}
}

buildTestData();
//end of test data


//set the number of rows you'd like each table to have
$splitPoint = 4;


$count = count($data);

for ($i = 0; $i < $count; $i++) {

if($i % $splitPoint == 0) {
echo "\n<table id=\"mainTable\" border=\"1\" style=\"width:450px;position:relative;left:80px;\">";
echo "\n\t<tr>";
echo "\n\t\t<th style=\"background: #3498db; width:5px;text-align: center ;font-size:12px;font-family:Arial Narrow\">ID</th>";
echo "\n\t\t<th style=\"background: #3498db; width:10px;text-align: center ;font-size:12px;font-family:Arial Narrow\">NAME</th>";
echo "\n\t</tr>";
}


echo "\n\t<tr>";
echo "\n\t\t<td>" . $data[$i]['id'] . "</td>";
echo "\n\t\t<td>" . $data[$i]['name'] . "</td>";
echo "\n\t</tr>";

if(($i +1) % $splitPoint == 0 || $i == $count-1) {
echo "\n</table>";
}
}

结果

PHP 将生成如下格式化的 HTML:

<table id="mainTable" border="1" style="width:450px;position:relative;left:80px;">
<tr>
<th style="background: #3498db; width:5px;text-align: center ;font-size:12px;font-family:Arial Narrow">ID</th>
<th style="background: #3498db; width:10px;text-align: center ;font-size:12px;font-family:Arial Narrow">NAME</th>
</tr>
<tr>
<td>1</td>
<td>name A</td>
</tr>
<tr>
<td>2</td>
<td>name B</td>
</tr>
<tr>
<td>3</td>
<td>name C</td>
</tr>
<tr>
<td>4</td>
<td>name D</td>
</tr>
</table>
<table id="mainTable" border="1" style="width:450px;position:relative;left:80px;">
<tr>
<th style="background: #3498db; width:5px;text-align: center ;font-size:12px;font-family:Arial Narrow">ID</th>
<th style="background: #3498db; width:10px;text-align: center ;font-size:12px;font-family:Arial Narrow">NAME</th>
</tr>
<tr>
<td>5</td>
<td>name E</td>
</tr>
<tr>
<td>6</td>
<td>name F</td>
</tr>
<tr>
<td>7</td>
<td>name G</td>
</tr>
<tr>
<td>8</td>
<td>name H</td>
</tr>
</table>
<table id="mainTable" border="1" style="width:450px;position:relative;left:80px;">
<tr>
<th style="background: #3498db; width:5px;text-align: center ;font-size:12px;font-family:Arial Narrow">ID</th>
<th style="background: #3498db; width:10px;text-align: center ;font-size:12px;font-family:Arial Narrow">NAME</th>
</tr>
<tr>
<td>9</td>
<td>name I</td>
</tr>
<tr>
<td>10</td>
<td>name J</td>
</tr>
<tr>
<td>11</td>
<td>name K</td>
</tr>
<tr>
<td>12</td>
<td>name L</td>
</tr>
</table>
<table id="mainTable" border="1" style="width:450px;position:relative;left:80px;">
<tr>
<th style="background: #3498db; width:5px;text-align: center ;font-size:12px;font-family:Arial Narrow">ID</th>
<th style="background: #3498db; width:10px;text-align: center ;font-size:12px;font-family:Arial Narrow">NAME</th>
</tr>
<tr>
<td>13</td>
<td>name M</td>
</tr>
<tr>
<td>14</td>
<td>name N</td>
</tr>
<tr>
<td>15</td>
<td>name O</td>
</tr>
<tr>
<td>16</td>
<td>name P</td>
</tr>
</table>
<table id="mainTable" border="1" style="width:450px;position:relative;left:80px;">
<tr>
<th style="background: #3498db; width:5px;text-align: center ;font-size:12px;font-family:Arial Narrow">ID</th>
<th style="background: #3498db; width:10px;text-align: center ;font-size:12px;font-family:Arial Narrow">NAME</th>
</tr>
<tr>
<td>17</td>
<td>name Q</td>
</tr>
<tr>
<td>18</td>
<td>name R</td>
</tr>
<tr>
<td>19</td>
<td>name S</td>
</tr>
<tr>
<td>20</td>
<td>name T</td>
</tr>
</table>
<table id="mainTable" border="1" style="width:450px;position:relative;left:80px;">
<tr>
<th style="background: #3498db; width:5px;text-align: center ;font-size:12px;font-family:Arial Narrow">ID</th>
<th style="background: #3498db; width:10px;text-align: center ;font-size:12px;font-family:Arial Narrow">NAME</th>
</tr>
<tr>
<td>21</td>
<td>name U</td>
</tr>
<tr>
<td>22</td>
<td>name V</td>
</tr>
<tr>
<td>23</td>
<td>name W</td>
</tr>
</table>

您可以在这个 PHP 沙箱中看到代码的结果:http://sandbox.onlinephpfunctions.com/code/e2d73696445f709840084f8a7f40311353b0d8fc

关于javascript - PHP 中的 JQuery 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com