Solved Can someone help me with PHP echo'ing

Microsoft / Vista
November 15, 2010 at 21:43:54
Specs: Windows Vista, 2GB
So I am trying to perfect a script and the best example I could think of to use for this is the Bible, so I took Genesis Chapter 8 and basically, what I want to be able to happen is a user can enter a verse number from Genesis Chapter 8 and then hit submit which will bring them to another page with that verse on it. So far, that is working fine, but now I want to be able to enter multiple verses like 5-9 or something. And that I am having trouble with. I know I could always add more text boxes and then basically duplicate the PHP used to fit the second and first text box, but then that would only be able to feed back two verses. Is there an easier way to do this? Like maybe using two drop down menus with all the verse numbers in each and then the user can just use the first to select the first verse and then use the second to see all the verses through that number? i.e. If a user selects 4 in the first menu and 18 in the second, the user will see Genesis 8:4-18 when he/she hits the submit button.

Also, you can see the site that I have here: http://testsites.99k.org/Tests/form...

and from there you can examine the source codes and stuff and maybe give me a better, modified code from that. Thank you.

And if you can't get the source codes, tell me and I will put them here.


See More: Can someone help me with PHP echoing

Report •


✔ Best Answer
November 18, 2010 at 12:56:49
Sure; I didn't test for "revelation" as being a valid book in the above snippit, but something like below may be easier to implement.

<?php

if(isset($_POST["cmd"]) && $_POST["cmd"] === "Search") { 

   sscanf($_POST["test"], "%s %d : %d - %d", $book, $chap, $start, $end);

   if(empty($book) || (preg_match('/(genesis|revelation)/i', $book) == 0)) {

      /* if $book is invalid or $book is not in the $verses array, then
         there is no need to process further. */  

      oops();

   } else if(!empty($chap) && !empty($start)) {

      /* $chap, $start, and $end (if entered) should be checked against
         the book to make sure the ranges are valid; verify step is omitted */
 
      if(!empty($end)) {
         $out = display(strtolower($book), $chap, $start, $end);
      } else {
         $out = display(strtolower($book), $chap, $start, null);
      }

      echo $out;

   } else {
      oops();
   }
}

function oops() {

   echo "unexpected or unrecognized format";
}

function display($b, $c, $s, $e) {

   // $verses[book][chapter][verse]

   $ret = "";

   $verses = array("genesis" => 
                         array(1 =>
                                 array(1 => "verse 1; chapter 1; book of genesis",
                                       2 => "verse 2; chapter 1; book of genesis"),
                               8 =>
                                 array(1 => "verse 1; chapter 8; book of genesis",
                                       2 => "verse 2; chapter 8; book of genesis",
                                       3 => "verse 3; chapter 8; book of genesis",
                                       4 => "verse 4; chapter 8; book of genesis",
                                       5 => "verse 5; chapter 8; book of genesis",
                                       6 => "verse 6; chapter 8; book of genesis")),
                      "revelation" =>
                         array(1 =>
                                 array(1 => "verse 1; chapter 1; book of revelation",
                                       2 => "verse 2; chapter 1; book of revelation"))
                 );

   if(is_null($e)) {
     $ret = $verses[$b][$c][$s];
   } else {
      for($i = $s; $i <= $e; $i++) {
         $ret .= $verses[$b][$c][$i] . "<br />";
      }
   }

   return($ret);
}

?>
<h4>search example: Genesis 8: 2-5</h4>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
Query <input name="test" type="text" size="60" maxlength="32" value="genesis 8: 2-5">
<input name="cmd" value="Search" type="submit">
</form>

You'll need to add a check to ensure that the chapter ($chap) and verse numbers ($start and $end) are valid for the chosen book. If you want to add other books, modify the preg_match and add the book in the $verses array.

hope that helps



#1
November 16, 2010 at 01:12:56
post the php code for you're doing for the multiple chapters along with a sample of what you'd expect the user to type.

as an idea, say the user types "genesis 8: 4-18"

you've got deliminators that you could process such as the colon and the hyphen. you could split the input on the colon, which would give you two pairs "genesis 8" and "4-18". if the colon and hyphen aren't in the input, you could spit out an error message indicating what input is expected. If they do exist, process each pair to determine the ranges, chapter, etc.


Report •

#2
November 16, 2010 at 19:50:12
Okay here are the source codes:

http://testsites.99k.org/Tests/form.html

<html>
<body>

<form action="welcome.php" method="post">
Find a verse from Genesis Chapter 8<br />
Verse: <input type="text" name="verse" />
<input type="submit" />
</form>

</body>
</html>


http://testsites.99k.org/Tests/welcome.php


<html>
<body>

Genesis Chapter 8<br />
Verse <?php echo $_POST["verse"]; ?><br />
<hr>
<?php
					$one="<sup>1</sup>But God remembered Noah and all the beasts and all the cattle that were with him in the ark.  And God made a wind blow over the earth, and the waters subsided; ";
					$two="<sup>2</sup>the fountains of the deep and the windows of the heavens were closed, the rain from the heavens was restrained, </a>";
					$three="<sup>3</sup>and the waters receded from the earth continually.  At the end of a hundred and fifty days the waters had abated; </a>";
					$four="<sup>4</sup>and in the seventh month, on the seventeenth day of the month, the ark came to rest upon the mountains of Ar'arat.  </a>";
					$five="<sup>5</sup>And the waters continued to abate until the tenth month; in the tenth month, on the first day of the month, the tops of the mountains were seen.</a>";

					$six="<sup>6</sup>At the end of forty days Noah opened the window of the ark which he had made, </a>";
					$seven="<sup>7</sup>and sent forth a raven; and it went to and fro until the waters were dried up from the earth.  </a>";
					$eight="<sup>8</sup>Then he sent forth a dove from him to see if the waters had subsided from the face of the ground; </a>";
					$nine="<sup>9</sup>but the dove found no place to set her foot, and she returned to him to the ark, for the waters were still on the face of the whole earth.  So he put forth his hand and took her and brought her into the ark with him.  </a>";
					$ten="<sup>10</sup>He waited another seven days, and again he sent forth the dove out of the ark; </a>";
					$eleven="<sup>11</sup>and the dove came back to him in the evening, and lo, in her mouth a freshly plucked olive leaf; so Noah knew that the waters had subsided from the earth.  </a>";
					$twelve="<sup>12</sup>Then he waited another seven days, and sent forth the dove; and she did not return to him any more.  </a>";

					$thirteen="<sup>13</sup>In the six hundred and first year, in the first month, the first day of the month, the waters were dried from the earth; and Noah removed the covering of the ark, and looked, and behold, the face of the ground was dry.  </a>";
					$fourteen="<sup>14</sup>In the second month, on the twenty-seventh day of the month, the earth was dry.  </a>";
					$fifteen="<sup>15</sup>Then God said to Noah, </a>";
					$sixteen="<sup>16</sup>'Go forth from the ark, you and your wife, and your sons and your sons' wives with you.  </a>";
					$seventeen="<sup>17</sup>Bring forth with you every living thing that is with you of all flesh—birds and animals and every creeping thing that creeps on the earth—that they may breed abundantly on the earth, and be fruitful and multiply upon the earth.'  </a>";
					$eighteen="<sup>18</sup>So Noah went forth, and his sons and his wife and his sons' wives with him.  </a>";
					$nineteen="<sup>19</sup>And every beast, every creeping thing, and every bird, everything that moves upon the earth, went forth by families out of the ark.</a>";

					$twenty="<sup>20</sup>Then Noah built an alter to the LORD, and took of every clean animal and of every clean bird, and offered burnt offerings on the altar.  </a>";
					$twentyone="<sup>21</sup>And when the LORD smelled the pleasing odor, the LORD said in his hearth, 'I will never again curse the ground because of man, for the imagination of man's heart is evil from his youth; neither will I ever again destroy every living creature as I have done.  </a>";
					$twentytwo="<sup>22</sup>While the earth remains, seedtime and harvest, cold and heat, summer and winter, day and night, shall not cease.'  </a>";

?>
<?php 
	if ($_POST["verse"]=="1" ) echo $one;
	elseif ($_POST["verse"]=="2") echo $two;
	elseif ($_POST["verse"]=="3") echo $three;
	elseif ($_POST["verse"]=="4") echo $four;
	elseif ($_POST["verse"]=="5") echo $five;
	elseif ($_POST["verse"]=="6") echo $six;
	elseif ($_POST["verse"]=="7") echo $seven;
	elseif ($_POST["verse"]=="8") echo $eight;
	elseif ($_POST["verse"]=="9") echo $nine;
	elseif ($_POST["verse"]=="10") echo $ten;
	elseif ($_POST["verse"]=="11") echo $eleven;
	elseif ($_POST["verse"]=="12") echo $twelve;
	elseif ($_POST["verse"]=="13") echo $thirteen;
	elseif ($_POST["verse"]=="14") echo $fourteen;
	elseif ($_POST["verse"]=="15") echo $fifteen;
	elseif ($_POST["verse"]=="16") echo $sixteen;
	elseif ($_POST["verse"]=="17") echo $seventeen;
	elseif ($_POST["verse"]=="18") echo $eighteen;
	elseif ($_POST["verse"]=="19") echo $nineteen;
	elseif ($_POST["verse"]=="20") echo $twenty;
	elseif ($_POST["verse"]=="21") echo $twentyone;
	elseif ($_POST["verse"]=="22") echo $twentytwo;
	else echo "The verse you entered is not a valid verse for Genesis Chapter 8."

 ?>
<hr>
<br />

</body>
</html>

I also made kind of the same thing only with this one, multiple verses can be selected, but they have to be checked with checkboxes

http://testsites.99k.org/Tests/genesischapter8customverse.html



<html>
<head>
<script type="text/javascript">
function selectAll(x) {
for(var i=0,l=x.form.length; i<l; i++)
if(x.form[i].type == 'checkbox' && x.form[i].name != 'sAll')
x.form[i].checked=x.form[i].checked?false:true
}
</script>
</head>
<body>

<form action="genesischapter8customverse.php" method="get">
Select verses from Genesis Chapter 8<br />
<input type="checkbox" name="verse1" value="1" />1
<input type="checkbox" name="verse2" value="2" />2
<input type="checkbox" name="verse3" value="3" />3
<input type="checkbox" name="verse4" value="4" />4
<input type="checkbox" name="verse5" value="5" />5
<input type="checkbox" name="verse6" value="6" />6
<input type="checkbox" name="verse7" value="7" />7
<input type="checkbox" name="verse8" value="8" />8
<input type="checkbox" name="verse9" value="9" />9
<input type="checkbox" name="verse10" value="10" />10
<input type="checkbox" name="verse11" value="11" />11
<input type="checkbox" name="verse12" value="12" />12
<input type="checkbox" name="verse13" value="13" />13
<input type="checkbox" name="verse14" value="14" />14
<input type="checkbox" name="verse15" value="15" />15
<input type="checkbox" name="verse16" value="16" />16
<input type="checkbox" name="verse17" value="17" />17
<input type="checkbox" name="verse18" value="18" />18
<input type="checkbox" name="verse19" value="19" />19
<input type="checkbox" name="verse20" value="20" />20
<input type="checkbox" name="verse21" value="21" />21
<input type="checkbox" name="verse22" value="22" />22


<input type="checkbox" value="1-22" name="sAll" onclick="selectAll(this)""/>All


<input type="submit" />



</form>

</body>
</html>

http://testsites.99k.org/Tests/genesischapter8customverse.php

<html>
<body>

Genesis Chapter 8<br />
Verses: <?php echo "$_GET[verse1] "; echo "$_GET[verse2] "; echo "$_GET[verse3] "; echo "$_GET[verse4] "; echo "$_GET[verse5] "; echo "$_GET[verse6] "; echo "$_GET[verse7] "; echo "$_GET[verse8] "; echo"$_GET[verse9] "; echo "$_GET[verse10] "; echo "$_GET[verse11] "; echo "$_GET[verse12] "; echo "$_GET[verse13] "; echo "$_GET[verse14] "; echo "$_GET[verse15] "; echo "$_GET[verse16] "; echo "$_GET[verse17] "; echo "$_GET[verse18] "; echo "$_GET[verse19] "; echo "$_GET[verse20] "; echo "$_GET[verse21] "; echo "$_GET[verse22]"; ?>
<br />
<hr>
<?php
$one="<sup>1</sup>But God remembered Noah and all the beasts and all the cattle that were with him in the ark. And God made a wind blow over the earth, and the waters subsided;
";
$two="<sup>2</sup>the fountains of the deep and the windows of the heavens were closed, the rain from the heavens was restrained,
";
$three="<sup>3</sup>and the waters receded from the earth continually. At the end of a hundred and fifty days the waters had abated;
";
$four="<sup>4</sup>and in the seventh month, on the seventeenth day of the month, the ark came to rest upon the mountains of Ar'arat.
";
$five="<sup>5</sup>And the waters continued to abate until the tenth month; in the tenth month, on the first day of the month, the tops of the mountains were seen.
";

$six="<sup>6</sup>At the end of forty days Noah opened the window of the ark which he had made,
";
$seven="<sup>7</sup>and sent forth a raven; and it went to and fro until the waters were dried up from the earth.
";
$eight="<sup>8</sup>Then he sent forth a dove from him to see if the waters had subsided from the face of the ground;
";
$nine="<sup>9</sup>but the dove found no place to set her foot, and she returned to him to the ark, for the waters were still on the face of the whole earth. So he put forth his hand and took her and brought her into the ark with him.
";
$ten="<sup>10</sup>He waited another seven days, and again he sent forth the dove out of the ark;
";
$eleven="<sup>11</sup>and the dove came back to him in the evening, and lo, in her mouth a freshly plucked olive leaf; so Noah knew that the waters had subsided from the earth.
";
$twelve="<sup>12</sup>Then he waited another seven days, and sent forth the dove; and she did not return to him any more.
";

$thirteen="<sup>13</sup>In the six hundred and first year, in the first month, the first day of the month, the waters were dried from the earth; and Noah removed the covering of the ark, and looked, and behold, the face of the ground was dry.
";
$fourteen="<sup>14</sup>In the second month, on the twenty-seventh day of the month, the earth was dry.
";
$fifteen="<sup>15</sup>Then God said to Noah,
";
$sixteen="<sup>16</sup>'Go forth from the ark, you and your wife, and your sons and your sons' wives with you.
";
$seventeen="<sup>17</sup>Bring forth with you every living thing that is with you of all flesh—birds and animals and every creeping thing that creeps on the earth—that they may breed abundantly on the earth, and be fruitful and multiply upon the earth.'
";
$eighteen="<sup>18</sup>So Noah went forth, and his sons and his wife and his sons' wives with him.
";
$nineteen="<sup>19</sup>And every beast, every creeping thing, and every bird, everything that moves upon the earth, went forth by families out of the ark.
";

$twenty="<sup>20</sup>Then Noah built an alter to the LORD, and took of every clean animal and of every clean bird, and offered burnt offerings on the altar.
";
$twentyone="<sup>21</sup>And when the LORD smelled the pleasing odor, the LORD said in his hearth, 'I will never again curse the ground because of man, for the imagination of man's heart is evil from his youth; neither will I ever again destroy every living creature as I have done.
";
$twentytwo="<sup>22</sup>While the earth remains, seedtime and harvest, cold and heat, summer and winter, day and night, shall not cease.'
";

?>
<?php
if ($_GET["verse1"]=="1") echo $one;
if ($_GET["verse2"]=="2") echo $two;
if ($_GET["verse3"]=="3") echo $three;
if ($_GET["verse4"]=="4") echo $four;
if ($_GET["verse5"]=="5") echo $five;
if ($_GET["verse6"]=="6") echo $six;
if ($_GET["verse7"]=="7") echo $seven;
if ($_GET["verse8"]=="8") echo $eight;
if ($_GET["verse9"]=="9") echo $nine;
if ($_GET["verse10"]=="10") echo $ten;
if ($_GET["verse11"]=="11") echo $eleven;
if ($_GET["verse12"]=="12") echo $twelve;
if ($_GET["verse13"]=="13") echo $thirteen;
if ($_GET["verse14"]=="14") echo $fourteen;
if ($_GET["verse15"]=="15") echo $fifteen;
if ($_GET["verse16"]=="16") echo $sixteen;
if ($_GET["verse17"]=="17") echo $seventeen;
if ($_GET["verse18"]=="18") echo $eighteen;
if ($_GET["verse19"]=="19") echo $nineteen;
if ($_GET["verse20"]=="20") echo $twenty;
if ($_GET["verse21"]=="21") echo $twentyone;
if ($_GET["verse22"]=="22") echo $twentytwo;


if(isset($_GET['1'])) {
echo $one;
}

?>
<hr>
<br />

</body>
</html>



Report •

#3
November 17, 2010 at 11:00:40
What about something like

<?php

if(isset($_POST["cmd"]) && $_POST["cmd"] === "Search") {   

   sscanf($_POST["test"], "%s %d : %d - %d", $book, $chap, $start, $end);

   if((!empty($book) && strtolower($book) === "genesis") &&
      (!empty($chap) && !empty($start))) {

      // validate book, chapter, and range(s), etc
 
      if(!empty($end)) {
         $out = display($book, $chap, $start, $end);
      } else {
         $out = display($book, $chap, $start, null);
      }

      echo $out;

   } else {
      oops();
   }       
}

function oops() {

   echo "unexpected or unrecognized format";
}

function display($b, $c, $s, $e) {

   // $verses[book][chapter][verse]

   $ret = "";

   $verses = array("genesis" => 
                         array(1 =>
                                 array(1 => "verse 1; chapter 1; book of genesis",
                                       2 => "verse 2; chapter 1; book of genesis"),
                               8 =>
                                 array(1 => "verse 1; chapter 8; book of genesis",
                                       2 => "verse 2; chapter 8; book of genesis",
                                       3 => "verse 3; chapter 8; book of genesis",
                                       4 => "verse 4; chapter 8; book of genesis",
                                       5 => "verse 5; chapter 8; book of genesis",
                                       6 => "verse 6; chapter 8; book of genesis")),
                      "revelation" =>
                         array(1 =>
                                 array(1 => "verse 1; chapter 1; book of revelation",
                                       2 => "verse 2; chapter 1; book of revelation"))
                 );

   if(is_null($e)) {
     $ret = $verses[$b][$c][$s];
   } else {
      for($i = $s; $i <= $e; $i++) {
         $ret .= $verses[$b][$c][$i] . "<br />";
      }
   }

   return($ret);
}

?>
<h4>search example: Genesis 8: 2-5</h4>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
Query <input name="test" type="text" size="60" maxlength="32" value="genesis 8: 2-5">
<input name="cmd" value="Search" type="submit">
</form>

Not really tested beyond what's there, but it *might* work. a regex expression would probably be better, but dreaming one up is too complex for me. :)


Report •

Related Solutions

#4
November 18, 2010 at 05:21:43
That worked near perfectly; however, I saw in the code that you also included verse 1 and 2 of revelation, but I can't seem to get those working. I would enter "revelation 1:1" and it would feed back the error message, "unexpected or unrecognized format"

Here is what you gave me unedited:
http://testsites.99k.org/Tests/vers...

I wasn't able to find out why revelation would not show up, do you think you could fix that? Thanks


Report •

#5
November 18, 2010 at 12:56:49
✔ Best Answer
Sure; I didn't test for "revelation" as being a valid book in the above snippit, but something like below may be easier to implement.

<?php

if(isset($_POST["cmd"]) && $_POST["cmd"] === "Search") { 

   sscanf($_POST["test"], "%s %d : %d - %d", $book, $chap, $start, $end);

   if(empty($book) || (preg_match('/(genesis|revelation)/i', $book) == 0)) {

      /* if $book is invalid or $book is not in the $verses array, then
         there is no need to process further. */  

      oops();

   } else if(!empty($chap) && !empty($start)) {

      /* $chap, $start, and $end (if entered) should be checked against
         the book to make sure the ranges are valid; verify step is omitted */
 
      if(!empty($end)) {
         $out = display(strtolower($book), $chap, $start, $end);
      } else {
         $out = display(strtolower($book), $chap, $start, null);
      }

      echo $out;

   } else {
      oops();
   }
}

function oops() {

   echo "unexpected or unrecognized format";
}

function display($b, $c, $s, $e) {

   // $verses[book][chapter][verse]

   $ret = "";

   $verses = array("genesis" => 
                         array(1 =>
                                 array(1 => "verse 1; chapter 1; book of genesis",
                                       2 => "verse 2; chapter 1; book of genesis"),
                               8 =>
                                 array(1 => "verse 1; chapter 8; book of genesis",
                                       2 => "verse 2; chapter 8; book of genesis",
                                       3 => "verse 3; chapter 8; book of genesis",
                                       4 => "verse 4; chapter 8; book of genesis",
                                       5 => "verse 5; chapter 8; book of genesis",
                                       6 => "verse 6; chapter 8; book of genesis")),
                      "revelation" =>
                         array(1 =>
                                 array(1 => "verse 1; chapter 1; book of revelation",
                                       2 => "verse 2; chapter 1; book of revelation"))
                 );

   if(is_null($e)) {
     $ret = $verses[$b][$c][$s];
   } else {
      for($i = $s; $i <= $e; $i++) {
         $ret .= $verses[$b][$c][$i] . "<br />";
      }
   }

   return($ret);
}

?>
<h4>search example: Genesis 8: 2-5</h4>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
Query <input name="test" type="text" size="60" maxlength="32" value="genesis 8: 2-5">
<input name="cmd" value="Search" type="submit">
</form>

You'll need to add a check to ensure that the chapter ($chap) and verse numbers ($start and $end) are valid for the chosen book. If you want to add other books, modify the preg_match and add the book in the $verses array.

hope that helps


Report •

Ask Question