Location: PHPKode > projects > Bible Reading Program > get_verse.php
<?php //(C)Copyright 2005-2006 John D. Nash, Jr. All Rights Reserved, Worldwide.
// file: get_verse2.php
// Bible Verse of the Day Retrieval Functions
//
/***************************************************************************
 *   Copyright (C) 2007 by John D Nash, Jr. All Rights Reserved Worldwide. *
 *   hide@address.com                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
// function get_verse()
// expects global variables $topic and $page_content
// $topic must be either "daily" or "youth"
// Returns TRUE if successful, FALSE if fails
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function get_verse($topic="daily") {
	global $baselink;
	global $default_page_name;
	global $myyear;
	global $mymonth;
	global $myday;

	$table_name = "reading_list";
	if ($topic == "daily"){
		$default_page_name = "Daily Bible Reading";
		$my_program = "adult"; // table index is on month+day+program (as string)
	} elseif ($topic == "youth"){
		$default_page_name = "Youth Daily Bible Reading";
		$my_program = "youth";
	} else {
		$topic = "daily";
		$default_page_name = "Daily Bible Reading";
		$my_program = "adult";
	} // end of if ($topic == "daily")

	$date_array = getdate();
	$myyear = sprintf("%04d",$date_array['year']);
	if (!$mymonth OR !$myday 
		OR (intval($mymonth)<1) OR (intval($mymonth)>12) 
		OR (intval($myday)<1) OR (intval($myday)>31) ) { 
		// initialize if first time running this code
		$mymonth = sprintf("%02d",$date_array['mon']);
		$myday = sprintf("%02d",$date_array['mday']);
	} //  end of if (!$mymonth OR !$myday OR ...

		// use specified date if date was specified
	$date_array = getdate(mktime(0,0,0,intval($mymonth),intval($myday),intval($myyear)));
//////////////////////////////////////////////////////////////////////////////////////////////////////
// test point to insert date \\
//$date_array = getdate(mktime(0,0,0,12,24,2005));		 /////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

		// NOTE: $date_array[xxx] = yyy where:
		//	[seconds] => 40
		//	[minutes] => 58
		//	[hours]   => 21
		//	[mday]    => 17
		//	[wday]    => 2
		//	[mon]     => 6
		//	[year]    => 2003
		//	[yday]    => 167
		//	[weekday] => Tuesday
		//	[month]   => June
		//	[0]       => 1055901520
		//	
	
	$today = sprintf("%04d%02d%02d",$date_array['year'],$date_array['mon'],$date_array['mday']);
	$today_month = sprintf("%02d",$date_array['mon']);
	$today_day = sprintf("%02d",$date_array['mday']);
	
//	// skip feb 29
//	if (($today_month == "02") AND ($today_day == "29") ) {
//		$today_day = "28";
//	}

	// calculate one day before today, in case needed
	if (($today_month == "01") AND ($today_day == "01") ) {
		$prior_month = "12";
		$prior_day = "31";
	} else {
		$date_prior_array = getdate(mktime(0,0,0,intval($mymonth),intval($myday)-1,intval($myyear)));
		$prior_month = sprintf("%02d",$date_prior_array['mon']);
		$prior_day = sprintf("%02d",$date_prior_array['mday']);
	} // end of if (($today_month == "01") AND ($today_day == "01") ) 

	// calculate one day after today, in case needed
	if (($today_month == "12") AND ($today_day == "31") ) {
		$next_month = "01";
		$next_day = "01";
	} else {
		$date_next_array = getdate(mktime(0,0,0,intval($mymonth),intval($myday)+1,intval($myyear)));
		$next_month = sprintf("%02d",$date_next_array['mon']);
		$next_day = sprintf("%02d",$date_next_array['mday']);
	} // end of if (($today_month == "12") AND ($today_day == "31") ) 

////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                    Create Grey Header Prior Next bar                               //
////////////////////////////////////////////////////////////////////////////////////////////////////////

	// Arrange Content in a self-contained table
	$page_content .= "\n\n<table width=\"100%\">\n";
	$page_content .= "<tr bgcolor=\"#770000\">\n";
	
	// Output the "Prior, First" links in header
	$page_content .= "<td valign=\"top\"><a href=\"/".$baselink."?topic=".$topic."&myyear=".$myyear."&mymonth=01&myday=01\"><small><small>&lt;&lt;<br>First</small></small></a></td>\n";
	$page_content .= "<td valign=\"top\"><a href=\"/".$baselink."?topic=".$topic."&myyear=".$myyear."&mymonth=".$prior_month."&myday=".$prior_day."\"><small><small>&lt;<br>Prior</small></small></a></td>\n";
	
	// Output Text title in heading
	$page_content .= "<td><div class=\"story\" align=\"Justify\" valign=\"top\"><center>Scripture for ";
	$page_content .= $date_array['weekday'].", ".$date_array['month']." ".$date_array['mday'];
	$page_content .= ", ".$date_array['year'];

//	// login to mysql database
//	$db_bread = myconnect2("MyBible","test");

	$db_bread = mysql_connect ("localhost", "MyBible", "test") or die ('I cannot connect to the database because: ' . mysql_error());
	// connect to database
	mysql_select_db ("brp");

		///////////////////////////////////////////////////
		// connect to bread table to get verse reference //
		///////////////////////////////////////////////////
		$sql  = "SELECT * FROM `".$table_name."` WHERE `month`=".$today_month;
		$sql .= " AND `day`=".$today_day." AND `program`='".$my_program."';";
		$result1 = mysql_query($sql, $db_bread) or die(mysql_error());

		//////////////////////////////////////////////////////
		// display scripture references for today's reading //
		//////////////////////////////////////////////////////
		$temp_page_content = ""; // initialize temp variable to hold verse text
		while ($bread = mysql_fetch_array($result1)) { // first while loop
	
			// save start & end references to lookup verses
			$item = $bread['item'];
			$start_book = $bread['start_book'];
			$start_chapt = $bread['start_chapt'];
			$start_verse = $bread['start_verse'];
			$end_book = $bread['end_book'];
			$end_chapt = $bread['end_chapt'];
			$end_verse = $bread['end_verse'];

			// fix missing data here
			if ($start_book < 1 ){ $start_book = 1; }
			if ($start_book > 66){ $start_book = 66; }
			if ($end_book < $start_book){ $end_book = $start_book; }
			if ($end_book > 66){ $end_book = 66; }
				
			// search books table to find friendly book name(s)
			// start book name
			$sql = "SELECT * FROM `books` WHERE `number`=".$start_book." LIMIT 1;";
			$result2 = mysql_query($sql, $db_bread) or die(mysql_error());
			$books = mysql_fetch_array($result2);
			$start_book_name = $books['book']; // Friendly Name of Bible Book
			$start_book_chapters = $books['chapters']; // Max number of chapters in the book
			mysql_free_result($result2);
			// fix missing data here
			if ($start_chapt < 1 ){ $start_chapt = 1; }
			if ($start_chapt > $start_book_chapters ){ $start_chapt = $start_book_chapters; }
	
			// end book name
			$sql = "SELECT * FROM `books` WHERE `number`=".$end_book." LIMIT 1;";
			$result2 = mysql_query($sql, $db_bread) or die(mysql_error());
			$books = mysql_fetch_array($result2);
			$end_book_name = $books['book']; 
			$end_book_chapters = $books['chapters'];
			mysql_free_result($result2);
			// fix missing data here
			if ($end_chapt < 1 ){ $end_chapt = 1; }
			if ($end_chapt > $end_book_chapters ){ $end_chapt = $end_book_chapters; }

			// start chapter max verses
			$sql  = "SELECT * FROM `bible` WHERE `book`=".$start_book;
			$sql .= " AND `chapter`=".$start_chapt;
			$result2 = mysql_query($sql, $db_bread) or die(mysql_error());
			mysql_data_seek($result2,mysql_num_rows($result2)-1);
			$verses = mysql_fetch_array($result2);
			$start_last_verse = $verses['verse']; // Max number of verses in the book
			mysql_free_result($result2);
			// fix missing data here
			if ((!$start_verse) OR ($start_verse < 1) ){ $start_verse = 1; }
			if ($start_verse > $start_last_verse ){ $start_verse = $start_last_verse; }

			// end chapter min/max verses
			$sql  = "SELECT * FROM `bible` WHERE `book`=".$end_book;
			$sql .= " AND `chapter`=".$end_chapt;
			$result2 = mysql_query($sql, $db_bread) or die(mysql_error());
			mysql_data_seek($result2,mysql_num_rows($result2)-1);
			$verses = mysql_fetch_array($result2);
			$end_last_verse = $verses['verse']; // Max number of verses in the book
			mysql_free_result($result2);
			
			// end book, chapter, and verse must be same or after start book, chapter, and verse
			if (($end_book > $start_book) OR ($end_chapt > $start_chapt)) { // different chapters
				// fix missing data here
				if ((!$end_verse) OR ($end_verse < 1) ){ $end_verse = $end_last_verse; }
				if ($end_verse > $end_last_verse ){ $end_verse = $end_last_verse; }
			} else { // same book and chapter
				// fix missing data here
				if ($end_verse < $start_verse ){ $end_verse = $end_last_verse; }
				if ($end_verse > $end_last_verse ){ $end_verse = $end_last_verse; }
			} // end of if (($end_book > $start_book) OR ($end_chapt > $start_chapt))

			$page_content .= "<br>\n<strong>".$start_book_name." ".$start_chapt.":".$start_verse."</strong>\n";
//			$page_content .= " through <strong>".$end_book_name." chapter ".$end_chapt." verse ".$end_verse."</strong>\n";
			if ($end_book_name != $start_book_name) { 
				$page_content .= "- <strong>".$end_book_name." ".$end_chapt.":".$end_verse."</strong>\n";
			} elseif ($end_chapt != $start_chapt) { //book is same but chapters might be different
				$page_content .= "- <strong>".$end_chapt.":".$end_verse."</strong>\n";
			} elseif ($end_verse != $start_verse) { //book and chapters are the same but verse might be different
				$page_content .= "- <strong>".$end_verse."</strong>\n";
			} else { // book, chapter, and verse are the same: Therefore there is only one verse to show.
				$page_content .= " ";
			} // end of if ($end_book_name != $start_book_name)
			
			////////////////////////////////////////////
			// Output Verse Text to page_content here //
			////////////////////////////////////////////
			// As long as we now know what the criteria are for this Bible seek,
			// fetch the content and save it temporarily until all the heading is built
			// then append it to the end of page_content

			// Find last row to include in content
			$sql  = "SELECT * FROM `bible` WHERE `book`=".$start_book;
			$sql .= " AND `chapter`=".$start_chapt;
			$sql .= " AND `verse`=".$start_verse;
			$sql .= ";";
			$result2 = mysql_query($sql, $db_bread) or die(mysql_error());
			$row=mysql_fetch_array($result2);
			$start_item = $row['item'];
			mysql_free_result($result2);

			// Find last row to include in content
			$sql  = "SELECT * FROM `bible` WHERE `book`=".$end_book;
			$sql .= " AND `chapter`=".$end_chapt;
			$sql .= " AND `verse`=".$end_verse;
			$sql .= ";";
			$result2 = mysql_query($sql, $db_bread) or die(mysql_error());
			$row=mysql_fetch_array($result2);
			$end_item = $row['item'];
			mysql_free_result($result2);

			// Select complete set of text to display
			$sql  = "SELECT * FROM `bible` WHERE `item`>=".$start_item;
			$sql .= " AND `item`<=".$end_item;
			$sql .= ";";
			$result2 = mysql_query($sql, $db_bread) or die(mysql_error());
			$start_item = "";
			$end_item = "";
			
			$previous_book_num = ""; //initialize
			$previous_chapter = ""; //initialize
			$mybookname = ""; 		//initialize
			while ($row=mysql_fetch_array($result2)) { //second while loop
			/////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
			/////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
				// Print Chapter Heading
				$mybooknum = $row['book'];
				$mychapter = $row['chapter'];
				if (($mybooknum != $previous_book_num) OR ($mychapter != $previous_chapter) ) {

					// End last verse paragraph if it is not the first pass this loop
					if ($mybookname) {$temp_page_content .= "</p>\n";} 

					// get the new book's name
					$sql = "SELECT * FROM `books` WHERE `number`=".$mybooknum." LIMIT 1;";
					$result3 = mysql_query($sql, $db_bread) or die(mysql_error());
					$books = mysql_fetch_array($result3);
					$mybookname = $books['book']; // Friendly Name of Bible Book
					mysql_free_result($result3);
					
					// Output chapter heading
					$temp_page_content .= "<center><h3>".$mybookname." chapter ".$mychapter."</h3></center>\n";
					$previous_book_num = $mybooknum;
					$previous_chapter = $mychapter;
					$temp_page_content .= "<p>";
					$verse_count = 0;
				} // end of if (($mybooknum != $previous_book_num) OR ($mychapter != $previous_chapter) )

				// Copy verse to the temp content
				$temp_page_content .= sprintf(" (%d) %s", $row['verse'], $row['text']);
				++$verse_count;
				if ( $verse_count%4  == 0 ){
					$temp_page_content .= "</p>\n<p>"; // skip a line after 4 verses
				} // end of if ( $verse_count%4  == 0 )

			} // end of second while ($row=mysql_fetch_array($result2))
			mysql_free_result($result2);
		} // end of the first while ($row=mysql_fetch_array($result1)) {
		mysql_free_result($result1);
		
		// Output the "Next, Last" links in header
		$page_content .= "</center></div></td>\n";
		$page_content .= "<td align=\"right\" valign=\"top\"><a href=\"/".$baselink."?topic=".$topic."&myyear=".$myyear."&mymonth=".$next_month."&myday=".$next_day."\"><small><small>&gt;<br>Next</small></small></a></td>\n";
		$page_content .= "<td align=\"right\" valign=\"top\"><a href=\"/".$baselink."?topic=".$topic."&myyear=".$myyear."&mymonth=12&myday=31\"><small><small>&gt;&gt;<br>Last</small></small></a></td>\n";
		$page_content .= "</tr>\n<tr>\n<td colspan=\"5\"><div class=\"story\" align=\"Justify\">\n";

		///////////////////////////////////////////////////////////////
		// Output Verse Text from temp variable to page_content here //
		///////////////////////////////////////////////////////////////
		$page_content .= $temp_page_content;

		// repeat prior next links
		$page_content .= "</tr>\n<tr bgcolor=\"#770000\">\n";
		$page_content .= "<td valign=\"top\"><a href=\"/".$baselink."?topic=".$topic."&myyear=".$myyear."&mymonth=01&myday=01\"><small><small>&lt;&lt;<br>First</small></small></a></td>\n";
		$page_content .= "<td valign=\"top\"><a href=\"/".$baselink."?topic=".$topic."&myyear=".$myyear."&mymonth=".$prior_month."&myday=".$prior_day."\"><small><small>&lt;<br>Prior</small></small></a></td>\n";
		$page_content .= "<td><div class=\"story\" align=\"Justify\" valign=\"top\"><center>Scripture for ";
		$page_content .= $date_array['weekday'].", ".$date_array['month']." ".$date_array['mday'];
		$page_content .= ", ".$date_array['year'];
		$page_content .= "</center></div></td>\n";
		$page_content .= "<td align=\"right\" valign=\"top\"><a href=\"/".$baselink."?topic=".$topic."&myyear=".$myyear."&mymonth=".$next_month."&myday=".$next_day."\"><small><small>&gt;<br>Next</small></small></a></td>\n";
		$page_content .= "<td align=\"right\" valign=\"top\"><a href=\"/".$baselink."?topic=".$topic."&myyear=".$myyear."&mymonth=12&myday=31\"><small><small>&gt;&gt;<br>Last</small></small></a></td>\n";

		// and finish content section
		$page_content .= "</tr>\n</table>\n";

return $page_content;
} //end of function get_verse()

?>
Return current item: Bible Reading Program