使用php将友好的url存储在从db字段创建的数据库中作为源


store friendly-url with php in database created from db-field as source

在前面,我几乎没有php编码技能,所以如果我问的是非常基本的问题,请原谅。

我想创建一个php文件,我可以通过cron作业执行,该作业在非modx数据库中创建friendly_URL。我有一个表"myuri",它有3列"id"title"uri"(uri默认为NULL)

以下是脚本应该做的

  • 连接到数据库服务器
  • 选择数据库
  • 使用创建的友好URL更新所有为NULL的"uri"字段从相应"标题"字段中的每个标题

由于我对自己正在做的事情有着非常基本的理解,我只设法将php和xPDO的一些代码剪切后的混合物粘在一起,这正在做一些事情,但并不是我所需要的。

我的sofar是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.)将所有[xPDO]组件替换为[PHP/SQL]组件,以便能够将其作为cron作业运行

2.)为每个函数将内容包括在内,以下是我的php技能达到极限的

3.)(可选)根据我在一些帖子中读到的内容,用"内爆"来运行它可能是明智的,因为表中有1.000.000行

如果有人能帮我,那就太好了。

只是一个简短的说明,

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."'");
}