gpt4 book ai didi

PHP 和 MySQL WP

转载 作者:行者123 更新时间:2023-11-29 18:31:09 24 4
gpt4 key购买 nike

我试图用这段代码完成两件事。我正在使用 INSERT PHP 插件将 PHP 直接写入 WP 页面。

1)查询MySQL数据库,将结果从一列返回为四列。

2) 使每个结果都有自己的超链接,将用户引导到一个新的 WP 页面,该页面在该页面上运行新的查询。

我可以让查询显示结果,甚至可以将这些结果转换为动态超链接,但是我无法获取格式。现在它只是将结果返回到一列中。这是我所拥有的:

[insert_php]

$servername = "localhost";
$username = "login"; //edited for privacy
$password = "password"; //edited for privacy
$database = "database"; //edited for privacy

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

//get results from database
$result = mysqli_query($conn,"SELECT mine FROM mines ORDER BY mine");
$all_property = array(); //declare an array for saving property

//showing property
echo '<table class="data-table">
<tr class="data-heading">'; //initialize table tag

while ($property = mysqli_fetch_field($result)) {
echo '<td>' . $property->name . '</td>'; //get field name for header
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag

//showing all data

while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
foreach ($all_property as $item) {
echo "<td><a href='urlhere/$row[$item]'>".$row[$item]. "</a>"."</td>"; //get items using property value
}
echo '</tr>';
}
echo "</table>";

[/insert_php]

感谢您提供的任何帮助。

我希望结果如下所示(每个结果都是一个超链接):

Image link below

最佳答案

我能够解决这个问题。仍然没有得到正确超链接的结果,但数组现在填充了超过四列。这是我的代码:

$sql = "SELECT mine FROM mines ORDER by mine";
$result = mysqli_query($conn, $sql);

if ($result) {
$data = array();
while($row = $result->fetch_assoc()) {
$data[] = $row['mine'];
}
}

if (is_array($data) && count($data)) { // if array's not empty, create HTML

//create a table & header row
$htmlout="<table><tr>
<th colspan='4'>Mine</th></tr>
<tr>
";

$counter = 1; //keep count of data

foreach ($data as $mines) {
//table cell for datum
$htmlout .= "<td>$mines</td>\n";

//rows of 4
if ($counter % 4 == 0) {
$htmlout .= "</tr>\n<tr>\n";
}
$counter++;
}
}
$htmlout .= "</tr></table>";

echo $htmlout;

关于PHP 和 MySQL WP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45683038/

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