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条)

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置