Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PHP problem...


shadowboy1505's Avatar
Member
0 0

I'm trying to make a random verse generator for my church's webpage, but I can't seem to get it to work and I can't figure out what's wrong. Please can someone help? Thanks in advance.

$randverse = mt_rand(1,7)
switch($randverse);
	{
	case 1:
		echo "For God so loved the world, that He gave His only begotten Son, that whoever believes in Him shall not perish, but have eternal life." . "<br />";
		echo "-John 3:16";
		break;
	case 2:
		echo "as it is written, THERE IS NONE RIGHTEOUS, NOT EVEN ONE" . "<br />";
		echo "-Romans 3:10";
		break;
	case 3:
		echo "5 For there is one God, and one mediator also between God and men, the man Christ Jesus, 6 who gave Himself as a ransom for all, the testimony given at the proper time." . "<br />";
		echo "-1 Timothy 2:5-6";
		break;
	case 4:
		echo "Jesus said to him, "I am the way, and the truth, and the life; no one comes to the Father but through Me." . "<br />";
		echo "-John 14:6";
		break;
	case 5:
		echo "So faith comes from hearing, and hearing by the word of Christ." . "<br />";
		echo "-Romans 10:17";
		break;
	case 6:
		echo "for all have sinned and fall short of the glory of God," . "<br />";
		echo "-Romans 3:23";
		break;
	case 7:
		echo "For the wages of sin is death, but the free gift of God is eternal life in Christ Jesus our Lord." . "<br />";
		echo "-Romans 6:23";
		break;
	default:
		echo $randverse;			//for finding errors...if the script will actually work
		break;
	}
?>```

shadowboy1505's Avatar
Member
0 0

Oh and any ideas on how to make this better (or shorter) would be appreciated also. Thanks


fashizzlepop's Avatar
Member
0 0

Put a semi colon after $randverse = mt_rand(1,7)


ghost's Avatar
0 0

markupswitch($randverse);

wrong syntax for a switch/case


ghost's Avatar
0 0

<?php $verses = array( 'a', 'b', 'c', 'd' ); echo $verses[ rand(1, count($verses))-1 ]; ?>


shadowboy1505's Avatar
Member
0 0

How is that incorrect syntax? It's the same syntax I used in another file and the other one works fine. I even removed the semicolon to be sure and it still didn't work… Actually, for clarification, the script does work. It just doesn't output what I want. I want it to output only one verse, but it outputs this…

&quot;; echo &quot;-Romans 3:10&quot;; break; case 3: echo &quot;5 For there is one God, and one mediator also between God and men, the man Christ Jesus, 6 who gave Himself as a ransom for all, the testimony given at the proper time.&quot; . &quot;
&quot;; echo &quot;-1 Timothy 2:5-6&quot;; break; case 4: echo &quot;Jesus said to him, &quot;I am the way, and the truth, and the life; no one comes to the Father but through Me.&quot; . &quot;
&quot;; echo &quot;-John 14:6&quot;; break; case 5: echo &quot;So faith comes from hearing, and hearing by the word of Christ.&quot; . &quot;
&quot;; echo &quot;-Romans 10:17&quot;; break; case 6: echo &quot;for all have sinned and fall short of the glory of God,&quot; . &quot;
&quot;; echo &quot;-Romans 3:23&quot;; break; case 7: echo &quot;For the wages of sin is death, but the free gift of God is eternal life in Christ Jesus our Lord.&quot; . &quot;
&quot;; echo &quot;-Romans 6:23&quot;; break; default: echo $randverse; //for finding errors...if the script will actually work break; } ?&gt;```

ghost's Avatar
0 0

There are broken quotes. eg

echo &quot;Jesus said to him, &quot;I am the way, and the truth, and the life; no one comes to the Father but through Me.&quot; . &quot;&lt;br /&gt;&quot;;

That is, there are 5 quotation marks in that line of code. Everything comes in pairs!! :)


shadowboy1505's Avatar
Member
0 0

Never mind…I just ran a script I know works and it doesn't work so I guess my server's screwed up :( crap


Mr_Cheese's Avatar
0 1

shadowboy1505 wrote: Never mind…I just ran a script I know works and it doesn't work so I guess my server's screwed up :( crap

no its not your server. its your poor code.

As Swatmumba said, its extra quotes that break out of your echo;s. **echo "Jesus said to him, "I am **

**echo "Jesus said to him, \"I am **


ghost's Avatar
0 0

First of, your code sucked, second: forgot an ; after random string function. third: its switch () {} not switch(); {}.

To shorten your code you can do

     .&quot;blablabla&quot;;```

instead of 

```markupecho &quot;blablabla&quot;.&quot;&lt;br /&gt;&quot;;
echo &quot;blablabla&quot;;```

Working code:
```markup
&lt;?php
$randverse = mt_rand(1,7);
switch ($randverse){

case 1:
echo &quot;For God so loved the world, that He gave His only begotten Son, that whoever believes in Him shall not perish, but have eternal life.&lt;br /&gt;&quot;
	.&quot;-John 3:16&quot;;
break;

case 2:
echo &quot;as it is written, THERE IS NONE RIGHTEOUS, NOT EVEN ONE&lt;br /&gt;&quot;
	.&quot;-Romans 3:10&quot;;
break;

case 3:
echo &quot;5 For there is one God, and one mediator also between God and men, the man Christ Jesus, 6 who gave Himself as a ransom for all, the testimony given at the proper time.&lt;br /&gt;&quot;
	.&quot;-1 Timothy 2:5-6&quot;;
break;

case 4:
echo &quot;Jesus said to him, &#92;&quot;I am the way, and the truth, and the life; no one comes to the Father but through Me.&#92;&quot;&lt;br /&gt;&quot;
	.&quot;-John 14:6&quot;;
break;

case 5:
echo &quot;So faith comes from hearing, and hearing by the word of Christ.&quot;
	.&quot;-Romans 10:17&quot;;
break;

case 6:
echo &quot;for all have sinned and fall short of the glory of God,&lt;br /&gt;&quot;
	.&quot;-Romans 3:23&quot;;
break;

case 7:
echo &quot;For the wages of sin is death, but the free gift of God is eternal life in Christ Jesus our Lord.&lt;br /&gt;&quot;
	.&quot;-Romans 6:23&quot;;
break;

default:
echo $randverse; //for finding errors...if the script will actually work
break;

}
?&gt;

B)


shadowboy1505's Avatar
Member
0 0

Thank you but like I said my server really is screwed up so I have no way of testing anything right now.

p.s. I just started learning PHP 2 days ago and with school and work I haven't really had much time to learn, so my coding skills aren't too good right now.


ghost's Avatar
0 0

to make it easier, i would say store in a DB


fashizzlepop's Avatar
Member
0 0

Easier??? Not for something this small…


ghost's Avatar
0 0

Seeing how this thread is still alive, I'll try to help. The code can really be shortened.

$element_num = array_rand($quotes_arr, 1);
echo $quotes_arr[$element_num];```