dounuo7954 2010-12-06 22:53
浏览 22
已采纳

关于获得多节经文的php-mysql问题

ANSWER FOUND: I am reading all and deciding, THANK YOU SOO MUCH hahaha :D I <3 stackoverflow

I apologize for the vagueness of this question and the lack of research, but I really had no idea how to google this question.

I am passing a bible verse and bible chapter into a page that takes it and pulls the verse from the bible db likes this

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

But what I want to be able to do is send in multiple verses separated by a "-"

like this:

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

I don't really know how this would work. Any ideas?

  • 写回答

3条回答 默认 最新

  • dtkjthe4025 2010-12-06 23:13
    关注

    This works. It allows you to give a range of verses like 1,3,4-10. It'll return the whole chapter if no specific verses are given.

    // 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";
    

    [edit] Made some minor changes, removed the typo's and actually run the script to test it. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题