Upfront,我几乎没有PHP编码技能,所以请原谅我是否提出非常基本的问题。
我想创建一个我可以通过cron job执行的php文件,它在非modx数据库中创建了friendly_URL。我有一个表“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技能达到其极限
3。)(可选)从我在一些帖子中读到的内容中可能很聪明地使用“implode”运行它以使其运行得更快,因为该表有1.000.000行
如果有人可以帮助我,那会很棒。
答案 0 :(得分:0)
快速说明,
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."'");
}