gpt4 book ai didi

php - 如何处理 PHP/SQL 支持的文件镜像上的多个文件版本?

转载 作者:可可西里 更新时间:2023-11-01 07:38:47 25 4
gpt4 key购买 nike

这是我第一次使用 PHP 或 SQL。

我正在制作一个简单的网站,其中托管应用程序及其名称、作者、版本、描述等。PHP 脚本从我创建的 MySQL 数据库中获取此信息。每个应用程序都有一个分配给它的唯一标识符。多个版本将重复使用相同的标识符。

我让它显示只有一个版本的条目。

然而...

当应用程序的更新版本发布时,我希望它在最新版本旁边的下拉列表中列出旧版本(只是链接到旧版本链接的版本号)。我创建数据库时假设我会想出一种很好地显示它的方法,因此当我将应用程序的更新版本添加到数据库时,我将未更改的内容留空(如名称、作者、描述) , 给了它与旧版本相同的标识符,只添加了新版本号和新版本的文件名。参见 here举个例子。

然而,我假设错了。

我不知道如何进行。我有一个问题:

SELECT * FROM apps WHERE identifier IN ( SELECT identifier FROM apps GROUP BY identifier HAVING count(*) > 1)

但是,这只会选择所有具有重复“标识符”的条目。我不知道如何通过循环运行它,以回显旧条目的描述/名称但回显较新条目的版本号/链接(同时还能够为我的下拉菜单回显旧版本号/链接)。哦,而且它不会将名称/描述/等分配给具有重复标识符但与其他应用程序标识符不同的应用程序。抱歉,这很难解释。

我当前的循环是:

$result=mysqli_query($connection,$query);
$num=mysqli_num_rows($result);
$i=0;while ($i < $num) {while ($row = mysqli_fetch_array($result)) {
// do stuff (echo)
} ;$i++;};

只是分别回显条目,一个是旧版本,一个是新版本(不显示描述、名称或作者)。

我应该如何进行?

最佳答案

这样的事情怎么样:

您从表中选择所有数据,按标识符 排序,然后按版本 排序。查询是这样的:

--                              ↓ orders data by identifier, thereby 'grouping' together records with the same identifier
SELECT * FROM apps ORDER BY identifier, version DESC
-- ↑ orders each 'group' by version number descending - highest / 'newest' first

然后,根据这些数据,您可以在 PHP 中创建一个数组结构,这将使您能够轻松地遍历数据。类似于以下内容:

$result = mysqli_query($connection,$query);
$apps = array(); // Create apps array

while ($row = mysqli_fetch_array($result)) {
// Assign the Name, Description, Author of the row
// These will be blank until the last (oldest version) which has these values
// $app[$row[1]] will create a key for each identifier or use the existing one
$apps[$row[1]]["name"] = $row[2];
$apps[$row[1]]["description"] = $row[3];
$apps[$row[1]]["author"] = $row[4];

// If there is no current version stored
if(empty($apps[$row[1]]["current_version"])){
$apps[$row[1]]["current_version"]["version"] = $row[5];
$apps[$row[1]]["current_version"]["source"] = $row[6];
$apps[$row[1]]["current_version"]["filename"] = $row[7];
} else { // If not, this means this row is an older version
$v = array(
"version" => $row[5],
"source" => $row[6],
"filename" => $row[7],
);
$apps[$row[1]]["older_versions"][] = $v; // Add the array to the older_versions array
}
}

这会产生以下数组结构:

Array
(
[vitashell] => Array
(
[name] => VitaShell
[description] => VitaShell is an alternative replacement...
[author] => TheFloW
[current_version] => Array
(
[version] => 0.86
[source] => http://www.example.com/link/version/0_86
[filename] => vitashell_0_86.vpk
)

[older_versions] => Array
(
[0] => Array
(
[version] => 0.8
[source] => http://www.example.com/link/version/0_8
[filename] => vitashell_0_8.vpk
)
)
)

[identifier] => Array (...)
)

然后,您可以使用几个 foreach 循环遍历 $apps 数组,根据需要打印出数据。


我做了一个打印数据的简单例子。出于显示目的,我向数据集添加了一些虚拟行:

<?php foreach($apps as $app){ ?>
<div class="app">
Name: <?php echo $app["name"]; ?><br />
Description: <?php echo $app["description"]; ?> <br />
Author: <?php echo $app["author"]; ?> <br />
Version: <a href="<?php echo $app["current_version"]["source"]; ?>"><?php echo $app["current_version"]["version"]; ?></a> <br />

<?php if(!empty($app["older_versions"])){ ?>
Older Versions:
<select name="older_versions">
<?php foreach($app["older_versions"] as $version){ ?>
<option value="<?php echo $version["source"] ?>"><?php echo $version["version"]; ?></option>
<?php } ?>
</select>
<?php } ?>
</div>
<hr>
<?php } ?>

它创建:

enter image description here

注意:“TIMSTUFF”只有 1 个版本,因此没有针对旧版本的下拉列表

关于php - 如何处理 PHP/SQL 支持的文件镜像上的多个文件版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39287815/

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