首先,我几乎没有php编码技能,所以如果我问的是非常基本的问题,请原谅。
我想要创建一个php文件,我可以通过cron作业来执行,它在一个非modx-数据库中创建friendly_URLs。我有一个表"myuri“,其中有3列"id”"title“"uri”(uri默认为NULL)。
下面是脚本应该做的事情
根据我对正在做的事情的基本理解,我只将一些由php和xPDO组成的代码片段混合在一起,这是在做一些事情,但不是严格地说是我所需要的。
到目前为止,我掌握的是modx xPDO和SQL的混合。
// [PHP/SQL] db connect
mysql_connect("hostname", "user", "password");
mysql_select_db("database");
// [xPDO] path
define('MODX_CORE_PATH', '/website/domain.tld/core/');
define('MODX_CONFIG_KEY','config');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
// [xPDO] Criteria for foreign Database
$host = 'hostname';
$username = 'user';
$password = 'password';
$dbname = 'database';
$port = 3306;
$charset = 'utf-8';
$dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
$xpdo = new xPDO($dsn, $username, $password);
// [PHP/SQL] Function that removes all unfriendly signs from the title example: "Hello! World" to "hello-world"
function Slug($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
// [xPDO] Issue queries against the foreign database, for testing limited to a few ids instead of "WHERE id is null"
$output = '';
$sql = "SELECT * FROM myuri WHERE id BETWEEN 564780000 AND 564781000";
foreach ($xpdo->query($sql) as $row) {
$output .= $row['title'];
}
// [PHP/SQL] take the xPDO query results and run "function Slug($string)" on it (i guess this needs to go into the "foreach" somehow)
$user = $output;
$item = "test-parent-folder/".Slug($user).".html";
// [PHP/SQL] Update the column "uri", for testing limited to a few ids instead of "WHERE id is null" (this needs to go into the "foreach" too)
mysql_query("UPDATE myuri SET uri = '$item' id BETWEEN 564780000 AND 564781000");所以基本上我需要做两件大事。
1.)用PHP/SQL组件替换所有xPDO组件,使其能够作为cron作业运行。
2.)将这些东西包含到每个函数中,这里是我的php技能达到它的极限
)(可选)(可选)根据我在一些帖子中所读到的内容,使用“内爆”来运行它可能是明智的,因为表中有1.000.000行。
如果有人能帮我的话就太好了。
发布于 2013-07-02 14:20:31
只是一张便条,
mysql_query("UPDATE myuri SET uri = '$item' id BETWEEN 564780000 AND 564781000");我想,这个函数将像5647800000到564781000之间的所有值一样被一个新值覆盖。在下面的代码中,我已经按照您的需要修复了这个问题(可能,这就是URL短程序的工作方式)
<?php
define('MODX_CORE_PATH', '/website/domain.tld/core/');
define('MODX_CONFIG_KEY','config');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
//FILL THIS IN//
$host = 'hostname';
$username = 'user';
$password = 'password';
$dbname = 'database';
$port = 3306;
$charset = 'utf-8';
//DON'T CHANGE BELOW//
mysql_connect($host,$username,$password);
mysql_select_db($dbname);
// [PHP/SQL] Function that removes all unfriendly signs from the title example: "Hello! World" to "hello-world"
function Slug($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
$items = mysql_query("SELECT * FROM myuri WHERE id BETWEEN 564780000 AND 564781000");
while($item = mysql_fetch_object($items)) {
mysql_query("UPDATE myuri SET uri='test-parent-folder/".Slug($item->title).".html' WHERE id='".$item->id."'");
}https://stackoverflow.com/questions/17424697
复制相似问题