gpt4 book ai didi

php - Joomla - 使用另一个 SELECT 查询中的变量更新表字段

转载 作者:行者123 更新时间:2023-11-30 00:57:51 24 4
gpt4 key购买 nike

我的网站在 Joomla 1.5.26 中运行,我使用 SELECT 查询将文件名路径转换为上次修改日期并打印页面中的内容:

$db =& JFactory::getDBO();
$query = "
SELECT `filename`
FROM `#__joomlatable`
WHERE `filename` != '';
";
$db->setQuery($query);
$options = $db->loadAssocList();
foreach ( $options as $o ) {
if (file_exists($o[filename])) {
echo date ("F d Y H:i:s.", filemtime($o[filename]));
$test = date ("F d Y H:i:s.", filemtime($o[filename]));

}
}

如您所见,我正在屏幕上打印各种日期,同时将日期分配给值$test

我的表格还包含一个名为日期的字段,其中包含空值。我想要做的是将 %test 的值填充到表内的日期字段值中。

所以在我写下第一段代码之后:

$db2 =& JFactory::getDBO();
$query2 = "
UPDATE `#__joomlatable`
SET `date` = '$test'
WHERE `filename` != '';
";
$db2->setQuery($query2);
$options2 = $db2->loadAssocList();

但它的作用是用相同的日期值填充所有记录中的日期列,这似乎是第一个查询请求的最后一个日期值。我认为它需要循环,但由于我对 PHP/Mysql 缺乏经验,我想不出任何东西。

你能帮忙吗?

最佳答案

事实上,您需要对结果进行循环并分别更新每个文件记录。

代码如下所示:

$db =& JFactory::getDBO();
$query = "
SELECT `filename`
FROM `#__joomlatable`
WHERE `filename` != '';
";
$db->setQuery($query);
$options = $db->loadAssocList();

$fileInfo = array();

//store in fileInfo the modified time for each file
foreach ( $options as $o ) {
if (file_exists($o['filename'])) {
$fileInfo[ $o['filename'] = filetime( $o['filename'] );
}
}

//now for each file we found we store the modified time in the database
foreach( $fileInfo as $filename => $time ){
$query2 = "
UPDATE `#__joomlatable`
SET `date` = '$time'
WHERE `filename` = '$filename';
";
$db2->setQuery($query2);
$options2 = $db2->query();
}

关于php - Joomla - 使用另一个 SELECT 查询中的变量更新表字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20404640/

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