回答:我正在阅读所有内容并决定,谢谢您哈哈哈:D I
对于这个问题的含糊不清和缺乏研究,我深表歉意,但是我真的不知道如何用谷歌搜索这个问题。

我正在将圣经经文和圣经章节传递到一个页面中,并接受该章节并将其从圣经数据库中拉出来,就像这样

?book = Genesis&chapter_number = 1&verse_number = 1

但是我想做的是发送多行经文,中间用“-”分隔

像这样:

?book =创世记&chapter_number = 1&verse_number = 1-2

我真的不知道这将如何工作。有任何想法吗?

最佳答案

这可行。它允许您给出一系列经文,例如1,3,4-10。如果没有给出具体的经文,它将返回整章内容。

// Get the book and chapter
$book = isset($_GET['book'])?(string)$_GET['book']:'';
$chapter = isset($_GET['chapter'])?(string)$_GET['chapter']:'';

// Make sure you escape the string to prevent SQL injection
$book = mysql_real_escape_string($book);
$chapter = mysql_real_escape_string($chapter);

// Get the verses as a string.
$verses = isset($_GET['verses'])?trim($_GET['verses']):'';

if ($verses ==='') {
    // If no verses are given, TRUE will trick the database
    // into returning all verses for the given book.
    $verseWhere = 'TRUE';
} else {
    // Split it on the commas
    $verseRanges = explode(',', $verses);
    $verseConditions = array();

    // Split each value on '-', if any
    foreach($verseRanges as $verseRange) {
        $verseRange = explode('-', $verseRange);
        // Build a condition
        if (count($verseRange) === 1) {
            $verseConditions[] = "verse = ".(int)$verseRange[0];
        } else {
            $verseConditions[] = "verse BETWEEN ".(int)$verseRange[0]." AND ".(int)$verseRange[1];
        }
    }

    // Implode the array to build a list of conditions
    $verseWhere = "(".implode(' OR ', $verseConditions).")";
}

// Create the SQL statement
$query = "
    SELECT
        *
    FROM
        Bible
    WHERE
        book = '$book' AND
        chapter = '$chapter' AND
        $verseWhere";


[编辑]
进行了一些小的更改,删除了错字并实际运行了脚本来对其进行测试。 :)

关于php - php-mysql有关获取多节经文的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4371769/

10-11 05:39