下面的代码放在 userfun.php
function chengyuJielong($lastWord, $limit) {
global $empire, $dbtbpre;
$result = array();
$sql = "SELECT title, wei, titleurl FROM {$dbtbpre}ecms_cy WHERE SUBSTRING(title, 1, 1) = '$lastWord' LIMIT $limit";
$queryResult = $empire->query($sql);
if ($queryResult) {
while ($row = $empire->fetch($queryResult)) {
$result[] = array(
'word' => $row['title'],
'url' => $row['titleurl']
);
}
}
return $result;
}
下面代码放在模板中
<h3>[!--title--]:成语接龙顺接</h3>
<hr>
<ul class="btn w4">
<?
// 获取当前成语的最后一个字
$chengyu = $navinfor[title];
$lastWord = mb_substr($chengyu, -1);
// 查询连续的18个成语接龙
$nextChengyu = array();
for ($i = 0; $i < 18; $i++) {
$currentChengyu = chengyuJielong($lastWord, 10); // 查询多个选项
if (count($currentChengyu) > 0) {
$randomIndex = array_rand($currentChengyu); // 随机选择一个选项
$nextChengyu[] = $currentChengyu[$randomIndex];
$lastWord = mb_substr($currentChengyu[$randomIndex]['word'], -1);
} else {
break;
}
}
if (count($nextChengyu) > 0) {
foreach ($nextChengyu as $chengyu) {
$firstChar = mb_substr($chengyu['word'], 0, 1);
$lastChar = mb_substr($chengyu['word'], -1);
$wordWithoutFirstChar = mb_substr($chengyu['word'], 1, -1);
echo '<li><a href="' . $chengyu['url'] . '" title="'.$chengyu[word].'"><font color="#c5653e">' . $firstChar . '</font>' . $wordWithoutFirstChar . '<font color="#c5653e">' . $lastChar . '</font></a></li>';
}
} else {
echo '<p style="text-align:center;">无法找到符合条件的成语</p>';
}
?>
</ul>