Jump to content

read in file array


Caitlin-havener

Recommended Posts

The code block I quoted needs to be replaced in whole with the other code block. Is that how you did it? How does that code area look now?

Link to comment
Share on other sites

The code block I quoted needs to be replaced in whole with the other code block. Is that how you did it? How does that code area look now?
Your right I had just replaced the 'if' part, but it is still doing the same thing with a different error code now. When I push "NEXT PHRASE" the screen prints "An apple a day keeps the doctor away 38One mans garbage is another mans treasure 43The Early bird catches the worm 33Cut to the chase 18Stick a fork in it 20At the end of my rope 23Actions speak louder than words 33Time is money 15Alls fair in love and war 27Misery loves company 22Laughter is the best medicine 31There are no more phrases left.31There are no more phrases left...." And that last part repeats over and over and then there is an error message: " Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\wamp\www\project2\project2.php on line 58". Why is it reading the whole file like that!? I have for "NEXT PHRASE":
else if ($act=="NEXT PHRASE")	{		$_SESSION['index']+= 1;		unset($_SESSION['bad_guess_index']);		unset($_SESSION['guess_array']);		start();			}

And in the top under the if empty session I initialized the index to 0. It should increase by one each time 'next session' is pressed, then go and grab that line of text from clichefile.txt. Whole thing:

<?php // woffer02.php	session_start();	if (empty($_SESSION)) {    //Initialize session variables    $_SESSION['bad_guess_index'] = 0;    $_SESSION['guess_array'] = array();	$_SESSION['index']=0;	$_SESSION['turn']="GOLD"; //GOOD}	$testnumber=2;		# A standard diagnostic tool	function logprint ($what,$num)	{global $testnumber;		if ($num==$testnumber)			print "LP:$what <br />";	}		#makeheader:	function makeheader( )	{		print '		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">		<html xmlns="http://www.w3.org/1999/xhtml">		<head>		<link rel="stylesheet" type="text/css" href="style.css" />		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		<title></title>			<style type="text/css">			.mtable td 			{				width:50px;			}		</style>			</head>				<body>		<div id="wrap">		<div id="header"><h1>Caitlin\'s Jeopardy</h1></div>		<form method="post">		';	} # makeheader		#makefooter:	function makefooter ()	{		print "</div></form></body></html>";	}	#textloader:		function textloader($whatfile)		{				if (!file_exists($whatfile))		{			print "Sorry, can't find $whatfile.";			exit;		}		else		{			$textarray=file($whatfile);			return $textarray;			}		}# textloader	#display: Draws the Jeopardy table	function display($visible, $letters)	{		$letterlength=count($letters);		print "<table class='mtable' border=1><tr>";		for ($i=1; $i<$letterlength; $i++)		{			//logprint("i=$i,vi=".$visible[$i]." ltr=".$letters[$i],1);						//print "i=$i,visible=".$visible[$i]."<br />";			if ($letters[$i]==' ')				print "</tr></table><br/>				<table class='mtable'><tr>";			else if ($visible[$i])			 {				print "<td><center>".$letters[$i]."</center></td>";			 }			else				print "<td> </td>";		}		print "</tr></table>";	}		#drawinputscreen: Asks for input.	function drawinputscreen($guesses)	{		print "<input type='text' name='guessletter'>";		print "Guess a letter. You have $guesses guesses left.";		print "<input type='submit' name='action' value='GUESS'>";		print "<input type='submit' name='action' value='REVEAL'>";		print "<input type='submit' name='action' value='NEXT PHRASE'>";	}		######## MAIN PROGRAM ##########		makeheader();	start();	function start(){		$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$letters = array();		//$text=$array[$index];//function to display if no cliches are left		$text = array_key_exists($index, $array) ? $array[$index] : "There are no more phrases left.";		/*if ($text==''){			$text="There are no more phrases left.";		}*/		//$letters=strtoupper($text);		print "$text";		$txtarr=str_split($text);		$x=strlen($text);		print "$x";		for ($i=0; $i<strlen($text);$i++)		{			$letters[$i+1]=strtoupper($text[$i]);			//$letters[$i+1]=strtoupper($txtarr[$i]);			}			$letterlength=count($letters);			$visible=$_SESSION['visible'];	$guesses=$_SESSION['guesses'];		$act=isset($_POST['action']) ? $_POST['action'] : '';	if (!$act)	{		logprint("noact",1);				//if no more phrases print it in cells and set all to visible		if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;		}		else{			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=0;		}				$guesses=10;			} # initialization block			else if ($act=="GUESS")	{ // main repeated action. Get a guessletter, scan the array.							  		$guessletter=strtoupper($_POST['guessletter']); // a-> A, etc. uppecase		logprint("gp=$guessletter",1);				for ($i=1; $i<=$letterlength; $i++)		{			$ll=$letters[$i];			if ($guessletter==$letters[$i])			{				logprint("setting visible $i",1);				$visible[$i]=1;			}		}				$turn=$_SESSION['turn'];		//WHOS TURN??		if ($turn=="GOLD"){			//print"It is $turn team's turn.";			$_SESSION['turn']="BLACK";		}		else if($turn=="BLACK"){			//print "It is $turn team's turn.";			$_SESSION['turn']="GOLD";		} #End whos turn							$guesses=$guesses-1;				//IF LETTER IS NOWHERE TO BE FOUND. 			if (!preg_match("/($guessletter)/",$text)){				$guess_index=$_SESSION['bad_guess_index'];				$_SESSION['guess_array'][$guess_index] = $guessletter;				$_SESSION['bad_guess_index'] += 1;				print"$guess_index <br/>";	}	$guess_index=$_SESSION['bad_guess_index'];	print"<div id='bad_guesses'>";	print"$guess_index <br/>";	for($i=0; $i<=$guess_index; $i++){		$bad_guess_print=$_SESSION['guess_array'];		print"$bad_guess_print[$i]";	}	print"</div>";		} #repeated action block		else if ($act=="NEXT PHRASE")	{		$_SESSION['index']+= 1;		unset($_SESSION['bad_guess_index']);		unset($_SESSION['guess_array']);		start();			}		$turn_print=$_SESSION['turn'];	print"<div id='team'>It is $turn_print team's turn.</div>";					print "visible=$visible. Letters=$letters. Guesses=$guesses.";	display($visible,$letters);	drawinputscreen($guesses);		//RUN OUT OF GUESSES			if ($guesses==0){			print"<div id='lose'>You lose! Click NEXT PHRASE.</div>";		}		else if ($guesses==1){			print"<div id='last'>This is your last guess!!!</div>";		}			$_SESSION['visible']=$visible;	$_SESSION['guesses']=$guesses;	}?>

And my text file reads:

An apple a day keeps the doctor awayOne mans garbage is another mans treasureThe Early bird catches the wormCut to the chaseStick a fork in itAt the end of my ropeActions speak louder than wordsTime is moneyAlls fair in love and warMisery loves companyLaughter is the best medicineTheres no such thing as a free lunch

just for reference. I've noticed somehow the program thinks there is an extra character at the end for some reason, but there are no blank spaces.

Link to comment
Share on other sites

You have an infinite recursion taking place... it needs to stop somewhere. Perhaps at the point where you realize the text is "There are no more phrases left.", like making:

		//if no more phrases print it in cells and set all to visible		if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;		}

into

		//if no more phrases print it in cells and set all to visible		if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;			return;		}

Link to comment
Share on other sites

You have an infinite recursion taking place... it needs to stop somewhere. Perhaps at the point where you realize the text is "There are no more phrases left.", like making:
if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;			return;		}

if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++){				$visible[$i]=1;			         return;}		}

Link to comment
Share on other sites

Errrr... maybe... like I said, I've kinda been unable to see though the whole app's workflow. A little too messy for my taste.

Link to comment
Share on other sites

Errrr... maybe... like I said, I've kinda been unable to see though the whole app's workflow. A little too messy for my taste.
Sorry.. If you have any suggestions on organization. I have a hard time reading it myself!! The different colors on dreamweaver is how im doing it. I just organize with the comments and indentation.
Link to comment
Share on other sites

Good...But for future reference's sake, here's what I believe is a little cleaner preliminary way of doing it:

<?php// woffer02.phpsession_start();if (isset($_POST['action']) && $_POST['action'] === 'RESET') {	$_SESSION = array();}if (empty($_SESSION)) {//Initialize session variables	$_SESSION['bad_guess_index'] = 0;	$_SESSION['guess_array'] = array();	$_SESSION['index'] = 0;	$_SESSION['turn'] = "GOLD"; //GOOD}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">	<head>		<link rel="stylesheet" type="text/css" href="style.css" />		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		<title></title>		<style type="text/css">			.mtable td			{				width:50px;				text-align:center;			}		</style>	</head>	<body>		<div id="wrap">			<h1>Caitlin's Jeopardy</h1>			<form action=""  method="post"><?php#textloader:function textloader($whatfile) {	if (!file_exists($whatfile)) {		print "Sorry, can't find $whatfile.";		exit;	}else {		$textarray = file($whatfile);		return $textarray;	}}# textloader#display: Draws the Jeopardy tablefunction display($visible, $letters) {	$letterlength = strlen($letters);	print "<table class='mtable' border=1><tr>";	for ($i = 0; $i < $letterlength; $i++) {//logprint("i=$i,vi=".$visible[$i]." ltr=".$letters[$i],1);//print "i=$i,visible=".$visible[$i]."<br />";		if ($letters[$i] == ' ')			print "</tr></table><br/>				<table class='mtable'><tr>";		else if ($visible[$i]) {			print "<td>" . $letters[$i] . "</td>";		}		else			print "<td> </td>";	}	print "</tr></table>";}######## MAIN PROGRAM ##########$array = textloader("clichefile.txt");$index = $_SESSION['index'];if (!isset($_POST['action'])) {	$_POST['action'] = '';}switch ($_POST['action']) {	case 'GUESS':		if (array_key_exists($index, $array)) {			$text = $array[$index];			$letters = strtoupper($text);			//Game on. $text is set... what now?		}else {?>				<div>There are no more phrases left.<input type="submit" name="action" value="RESET" /></div><?php		}		break;	case 'REVEAL':		//What's done when revealing anyway?		break;	case 'NEXT PHRASE':		$_SESSION['index']++;		//break; intentionally omitted.	case 'RESET':		//Nothing to do here for now. Just skipping.	default:		$guesses = 5;		//The game has just (re)started.		break;}//$text = array_key_exists($index, $array) ? $array[$index] : "There are no more phrases left.";//////$letters=strtoupper($text);//print "$text";//$txtarr = str_split($text);//$x = strlen($text);//for ($i = 0; $i < $x; $i++) {//	$letters[$i + 1] = strtoupper($txtarr[$i]);//}////$letterlength = count($letters);////$visible = $_SESSION['visible'];//$guesses = $_SESSION['guesses'];//////$act = isset($_POST['action']) ? $_POST['action'] : '';//if (!$act) {////	//if no more phrases print it in cells and set all to visible//	if ($text == "There are no more phrases left.") {//		for ($i = 1; $i <= $letterlength; $i++)//			$visible[$i] = 1;//		return;//	}else {//		for ($i = 1; $i <= $letterlength; $i++)//			$visible[$i] = 0;//	}////	$guesses = 5;//} # initialization block//else if ($act == "GUESS") { // main repeated action. Get a guessletter, scan the array.//	$guessletter = strtoupper($_POST['guessletter']); // a-> A, etc. uppecase////	for ($i = 1; $i <= $letterlength; $i++) {//		$ll = $letters[$i];//		if ($guessletter == $letters[$i]) {//			$visible[$i] = 1;//		}//	}////	$turn = $_SESSION['turn'];//	//WHOS TURN??//	if ($turn == "GOLD") {//		//print"It is $turn team's turn.";//		$_SESSION['turn'] = "BLACK";//	}else if ($turn == "BLACK") {//		//print "It is $turn team's turn.";//		$_SESSION['turn'] = "GOLD";//	} #End whos turn////IF LETTER IS NOWHERE TO BE FOUND.////	$guesses = $guesses - 1;////	if (!preg_match("/($guessletter)/", $text)) {//		$guess_index = $_SESSION['bad_guess_index'];//		$_SESSION['guess_array'][$guess_index] = $guessletter;//		$_SESSION['bad_guess_index'] += 1;//		print"$guess_index <br/>";//	}//	$guess_index = $_SESSION['bad_guess_index'];//	print"<div id='bad_guesses'>";//	print"$guess_index <br/>";//	for ($i = 0; $i <= $guess_index; $i++) {//		$bad_guess_print = $_SESSION['guess_array'];//		print"$bad_guess_print[$i]";//	}//	print"</div>";//} #repeated action block//else if ($act == "NEXT PHRASE") {//	$_SESSION['index']+= 1;//	unset($_SESSION['bad_guess_index']);//	unset($_SESSION['guess_array']);//	start();//}//$turn_print = $_SESSION['turn'];//print"It is $turn_print team's turn.";////print "visible=$visible. Letters=$letters. Guesses=$guesses.";//display($visible, $letters);//$_SESSION['visible'] = $visible;//$_SESSION['guesses'] = $guesses;?>				<input type="text" name="guessletter" />Guess a letter. You have <?php echo $guesses; ?> guesses left.";				<input type="submit" name="action" value="GUESS" />				<input type="submit" name="action" value="REVEAL" />				<input type="submit" name="action" value="NEXT PHRASE" />			</form>		</div>	</body></html>

I haven't rewritten it all because I haven't understood all of your logic, but the basic guiding principles are:1. When possible, stop PHP processing and output the HTML outside of PHP blocks. This lets editors do syntax highlighting on the HTML.2. Avoid unnecesary functions. You're only creating new scopes you have to deal with.3. Use switch instead of if...elseif...else sequences when operating on a single variable.4. Use strings as arrays instead of str_split()-ing them and/or doing fancy indexing stuff.

Link to comment
Share on other sites

Oh NO! I started cleaning it up and it looked like it was doing fine and now I realize that it is not setting the letter to visible when you hit "GUESS" now.Do you see issue???

<?php // woffer02.php	session_start();	if (empty($_SESSION)) {    //Initialize session variables    $_SESSION['bad_guess_index'] = 0;    $_SESSION['guess_array'] = array();	$_SESSION['index']=0;	$_SESSION['turn']="GOLD"; //GOOD}?>		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">		<html xmlns="http://www.w3.org/1999/xhtml">		<head>		<link rel="stylesheet" type="text/css" href="style.css" />		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		<title></title>			<style type="text/css">			.mtable td 			{				width:50px;			}		</style>			</head>				<body>		<div id="wrap">		<div id="header"><h1>Caitlin\'s Jeopardy</h1></div>		<form method="post">	<?php		#textloader:		function textloader($whatfile)		{				if (!file_exists($whatfile))		{			print "Sorry, can't find $whatfile.";			exit;		}		else		{			$textarray=file($whatfile);			return $textarray;			}		}# textloader	#display: Draws the Jeopardy table	function display($visible, $letters)	{		$letterlength=count($letters);		print "<table class='mtable' border=1><tr>";		for ($i=1; $i<$letterlength; $i++)		{						//print "i=$i,visible=".$visible[$i]."<br />";			if ($letters[$i]==' ')				print "</tr></table><br/>				<table class='mtable'><tr>";			else if ($visible[$i])			 {				print "<td><center>".$letters[$i]."</center></td>";			 }			else				print "<td> </td>";		}		print "</tr></table>";	}		#drawinputscreen: Asks for input.	function drawinputscreen($guesses)	{		print "<input type='text' name='guessletter'>";		print "Guess a letter. You have $guesses guesses left.";		print "<input type='submit' name='action' value='GUESS'>";		print "<input type='submit' name='action' value='REVEAL'>";		print "<input type='submit' name='action' value='NEXT PHRASE'>";		print "<input type='submit' name='action' value='END SESSION'>";	}		######## MAIN PROGRAM ##########		//initialize variables	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$letters = array();		$act=isset($_POST['action']) ? $_POST['action'] : '';		$text = array_key_exists($index, $array) ? $array[$index] : "There are no more phrases left.";	$txtarr=($text);			//Print whos turn	$turn_print=$_SESSION['turn'];	print"<div id='team'>It is $turn_print team's turn.</div>";	print "$text";			//Assign each letter to letters array and uppercase it		for ($i=0; $i<strlen($text);$i++)		{			$letters[$i+1]=strtoupper($text[$i]);			}			$letterlength=count($letters);		$visible=isset($_SESSION['visible']) ? $_SESSION['visible']: '';		$guesses=isset($_SESSION['guesses']) ? $_SESSION['guesses']: '';			switch ($act)	{	case (!$act):			for ($i=1; $i<=$letterlength; $i++)			$visible[$i]=0;					$guesses=16;		break;			case $act=="GUESS":				$guessletter=strtoupper($_POST['guessletter']); // a-> A, etc. uppecase					for ($i=1; $i<=$letterlength; $i++)			{				$ll=$letters[$i];				if ($guessletter==$letters[$i])				{					$visible[$i]=1;				}			}											//WHOS TURN??			$turn=$_SESSION['turn'];			if ($turn=="GOLD"){				$_SESSION['turn']="BLACK";			}			else if($turn=="BLACK"){				$_SESSION['turn']="GOLD";			} #End whos turn												$guesses=$guesses-1;						//IF LETTER IS NOWHERE TO BE FOUND. 				$guess_index=isset($_SESSION['bad_guess_index']) ? $_SESSION['bad_guess_index']: '';		$bad_guess_print=isset($_SESSION['guess_array']) ? $_SESSION['guess_array']: '';					if (!preg_match("/($guessletter)/",$text)){				print "<p style='float:right'>That letter was no where to be found.</p>";				$_SESSION['guess_array'][$guess_index] = $guessletter;				$_SESSION['bad_guess_index'] += 1;				print"$guess_index <br/>";			}					print"<div id='bad_guesses'>";		//print"$guess_index <br/>";				for($i=0; $i<=$guess_index; $i++){				print"<p style='float:right; color:red;'>$bad_guess_print[$i],  </p>";			}		print"</div>";		break;			case $act=="NEXT PHRASE":			$_SESSION['index']+= 1;		unset($_SESSION['bad_guess_index']);		unset($_SESSION['guess_array']);		unset($_SESSION['visible']);		unset($_SESSION['letters']);		break;				case $act=="REVEAL":			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;			return;			break;				case $act=="END SESSION":			session_destroy();		break;	}					//if no more phrases print it in cells and set all to visible		if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;			return;		}					else{			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=0;		}					//RUN OUT OF GUESSES			if ($guesses==0){			print"<div id='lose'>You lose! Click NEXT PHRASE.</div>";		}		else if ($guesses==1){			print"<div id='last'>This is your last guess!!!</div>";		}			print "visible=$visible. Letters=$letters. Guesses=$guesses.";	display($visible,$letters);	drawinputscreen($guesses);		$_SESSION['visible']=$visible;	$_SESSION['guesses']=$guesses;		?></div></form></body></html>

Link to comment
Share on other sites

You're not using switch correctly.The idea is to specify a single variable at the swtich, and see if it's equal to a value in "case" statements below. So, by having:

switch ($act){case (!$act):

You're essentially checking "if the value of $act is equal to the negative of $act"... yeah... I can't think of one case where this would be true.In addition, you might to want to change

case $act=="GUESS":

case $act=="NEXT PHRASE":

case $act=="REVEAL":

case $act=="END SESSION":

to

case "GUESS":

case "NEXT PHRASE":

case "REVEAL":

case "END SESSION":

Link to comment
Share on other sites

Well, since $act has a default value set earlier to '', you can just test for that:

case '':

or you could move it at the bottom under

default:

but in that later case, "default:" will be triggered even when the value is set, but is set to an unknown value.

Link to comment
Share on other sites

Well, since $act has a default value set earlier to '', you can just test for that:
<?php // woffer02.php	session_start();	if (empty($_SESSION)) {    //Initialize session variables    $_SESSION['bad_guess_index'] = 0;    $_SESSION['guess_array'] = array();	$_SESSION['index']=0;	$_SESSION['turn']="GOLD"; //GOOD}?>		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">		<html xmlns="http://www.w3.org/1999/xhtml">		<head>		<link rel="stylesheet" type="text/css" href="style.css" />		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		<title></title>			<style type="text/css">			.mtable td 			{				width:50px;			}		</style>			</head>				<body>		<div id="wrap">		<div id="header"><h1>Caitlin\'s Jeopardy</h1></div>		<form method="post">	<?php		#textloader:		function textloader($whatfile)		{				if (!file_exists($whatfile))		{			print "Sorry, can't find $whatfile.";			exit;		}		else		{			$textarray=file($whatfile);			return $textarray;			}		}# textloader	#display: Draws the Jeopardy table	function display($visible, $letters)	{		$letterlength=count($letters);		print "<table class='mtable' border=1><tr>";		for ($i=1; $i<$letterlength; $i++)		{						//print "i=$i,visible=".$visible[$i]."<br />";			if ($letters[$i]==' ')				print "</tr></table><br/>				<table class='mtable'><tr>";			else if ($visible[$i])			 {				print "<td><center>".$letters[$i]."</center></td>";			 }			else				print "<td> </td>";		}		print "</tr></table>";	}		#drawinputscreen: Asks for input.	function drawinputscreen($guesses)	{		print "<input type='text' name='guessletter'>";		print "Guess a letter. You have $guesses guesses left.";		print "<input type='submit' name='action' value='GUESS'>";		print "<input type='submit' name='action' value='REVEAL'>";		print "<input type='submit' name='action' value='NEXT PHRASE'>";		print "<input type='submit' name='action' value='END SESSION'>";	}		######## MAIN PROGRAM ##########		//initialize variables	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$letters = array();		$act=isset($_POST['action']) ? $_POST['action'] : '';		$text = array_key_exists($index, $array) ? $array[$index] : "There are no more phrases left.";	$txtarr=($text);			//Print whos turn	$turn_print=$_SESSION['turn'];	print"<div id='team'>It is $turn_print team's turn.</div>";	print "$text";			//Assign each letter to letters array and uppercase it		for ($i=0; $i<strlen($text);$i++)		{			$letters[$i+1]=strtoupper($text[$i]);			}			$letterlength=count($letters);		$visible=isset($_SESSION['visible']) ? $_SESSION['visible']: '';		$guesses=isset($_SESSION['guesses']) ? $_SESSION['guesses']: '';					switch ($act)	{					case "GUESS":				$guessletter=strtoupper($_POST['guessletter']); // a-> A, etc. uppecase					for ($i=1; $i<=$letterlength; $i++)			{				$ll=$letters[$i];				if ($guessletter==$letters[$i])				{					$visible[$i]=1;				}			}											//WHOS TURN??			$turn=$_SESSION['turn'];			if ($turn=="GOLD"){				$_SESSION['turn']="BLACK";			}			else if($turn=="BLACK"){				$_SESSION['turn']="GOLD";			} #End whos turn												$guesses=$guesses-1;						//IF LETTER IS NOWHERE TO BE FOUND. 				$guess_index=isset($_SESSION['bad_guess_index']) ? $_SESSION['bad_guess_index']: '';		$bad_guess_print=isset($_SESSION['guess_array']) ? $_SESSION['guess_array']: '';					if (!preg_match("/($guessletter)/",$text)){				print "<p style='float:right'>That letter was no where to be found.</p>";				$_SESSION['guess_array'][$guess_index] = $guessletter;				$_SESSION['bad_guess_index'] += 1;				print"$guess_index <br/>";			}					print"<div id='bad_guesses'>";		//print"$guess_index <br/>";				for($i=0; $i<=$guess_index; $i++){				print"<p style='float:right; color:red;'>$bad_guess_print[$i] </p>";			}		print"</div>";		break;			case "NEXT PHRASE":			$_SESSION['index']+= 1;		unset($_SESSION['bad_guess_index']);		unset($_SESSION['guess_array']);		unset($_SESSION['visible']);		unset($_SESSION['letters']);		break;				case "REVEAL":			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;			return;			break;				case "END SESSION":			session_destroy();		break;		default:			for ($i=1; $i<=$letterlength; $i++)			$visible[$i]=0;					$guesses=16;	}					//if no more phrases print it in cells and set all to visible		if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;			return;		}					else{			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=0;		}					//RUN OUT OF GUESSES			if ($guesses==0){			print"<div id='lose'>You lose! Click NEXT PHRASE.</div>";		}		else if ($guesses==1){			print"<div id='last'>This is your last guess!!!</div>";		}			print "visible=$visible. Letters=$letters. Guesses=$guesses.";	display($visible,$letters);	drawinputscreen($guesses);		$_SESSION['visible']=$visible;	$_SESSION['guesses']=$guesses;		?></div></form></body></html>

Link to comment
Share on other sites

I'm not sure if that's the reason, but... I think

		$visible=isset($_SESSION['visible']) ? $_SESSION['visible']: '';		$guesses=isset($_SESSION['guesses']) ? $_SESSION['guesses']: '';

		$bad_guess_print=isset($_SESSION['guess_array']) ? $_SESSION['guess_array']: '';

should actually be

		$visible=isset($_SESSION['visible']) ? $_SESSION['visible']: array();		$guesses=isset($_SESSION['guesses']) ? $_SESSION['guesses']: array();

		$bad_guess_print=isset($_SESSION['guess_array']) ? $_SESSION['guess_array']: array();

because those things are supposed to be arrays that start off as empty arrays when they're not present in the session.

Link to comment
Share on other sites

I'm not sure if that's the reason, but... I think
<?php // woffer02.php	session_start();	if (empty($_SESSION)) {    //Initialize session variables    $_SESSION['bad_guess_index'] = 0;    $_SESSION['guess_array'] = array();	$_SESSION['index']=0;	$_SESSION['turn']="GOLD"; //GOOD}?>		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">		<html xmlns="http://www.w3.org/1999/xhtml">		<head>		<link rel="stylesheet" type="text/css" href="style.css" />		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		<title></title>			<style type="text/css">			.mtable td 			{				width:50px;			}		</style>			</head>				<body>		<div id="wrap">		<div id="header"><h1>Caitlin\'s Jeopardy</h1></div>		<form method="post">	<?php		#textloader:		function textloader($whatfile)		{				if (!file_exists($whatfile))		{			print "Sorry, can't find $whatfile.";			exit;		}		else		{			$textarray=file($whatfile);			return $textarray;			}		}# textloader	#display: Draws the Jeopardy table	function display($visible, $letters)	{		$letterlength=count($letters);		print "<table class='mtable' border=1><tr>";		for ($i=1; $i<$letterlength; $i++)		{						//print "i=$i,visible=".$visible[$i]."<br />";			if ($letters[$i]==' ')				print "</tr></table><br/>				<table class='mtable'><tr>";			else if ($visible[$i])			 {				print "<td><center>".$letters[$i]."</center></td>";			 }			else				print "<td> </td>";		}		print "</tr></table>";	}		#drawinputscreen: Asks for input.	function drawinputscreen($guesses)	{		print "<input type='text' name='guessletter'>";		print "Guess a letter. You have $guesses guesses left.";		print "<input type='submit' name='action' value='GUESS'>";		print "<input type='submit' name='action' value='REVEAL'>";		print "<input type='submit' name='action' value='NEXT PHRASE'>";		print "<input type='submit' name='action' value='END SESSION'>";	}		######## MAIN PROGRAM ##########		//initialize variables	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$letters = array();		$act=isset($_POST['action']) ? $_POST['action'] : '';		$text = array_key_exists($index, $array) ? $array[$index] : "There are no more phrases left.";	$txtarr=($text);			//Print whos turn	$turn_print=$_SESSION['turn'];	print"<div id='team'>It is $turn_print team's turn.</div>";	print "$text";			//Assign each letter to letters array and uppercase it		for ($i=0; $i<strlen($text);$i++)		{			$letters[$i+1]=strtoupper($text[$i]);			}		print "$letters";		$letterlength=count($letters);		$visible=isset($_SESSION['visible']) ? $_SESSION['visible']: array();		$guesses=isset($_SESSION['guesses']) ? $_SESSION['guesses']: '';		/*	if(!$act){		for ($i=1; $i<=$letterlength; $i++)			$visible[$i]=0;					$guesses=16;	}	*/	switch ($act)	{			case '':			for ($i=1; $i<=$letterlength; $i++)			$visible[$i]=0;					$guesses=16;		break;							case "GUESS":				$guessletter=strtoupper($_POST['guessletter']); // a-> A, etc. uppercase					for ($i=1; $i<=$letterlength; $i++)			{				$ll=$letters[$i];				if ($guessletter==$letters[$i])				{					$visible[$i]=1;				}			}											//WHOS TURN??			$turn=$_SESSION['turn'];			if ($turn=="GOLD"){				$_SESSION['turn']="BLACK";			}			else if($turn=="BLACK"){				$_SESSION['turn']="GOLD";			} #End whos turn												$guesses=$guesses-1;						//IF LETTER IS NOWHERE TO BE FOUND. 				$guess_index=isset($_SESSION['bad_guess_index']) ? $_SESSION['bad_guess_index']: '';		$bad_guess_print=isset($_SESSION['guess_array']) ? $_SESSION['guess_array']: array();					if (!preg_match("/($guessletter)/",$text)){				print "<p style='float:right'>That letter was no where to be found.</p>";				$_SESSION['guess_array'][$guess_index] = $guessletter;				$_SESSION['bad_guess_index'] += 1;				print"$guess_index <br/>";			}					print"<div id='bad_guesses'>";		//print"$guess_index <br/>";				for($i=0; $i<=$guess_index; $i++){				print"<p style='float:right; color:red;'>$bad_guess_print[$i] </p>";			}		print"</div>";		break;			case "NEXT PHRASE":			$_SESSION['index']+= 1;		unset($_SESSION['bad_guess_index']);		unset($_SESSION['guess_array']);		unset($_SESSION['visible']);		unset($_SESSION['letters']);		unset($_SESSION['guesses']);		break;				case "REVEAL":			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;			return;			break;				case "END SESSION":			session_destroy();		break;	}					//if no more phrases print it in cells and set all to visible		if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;			return;		}					else{			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=0;		}					//RUN OUT OF GUESSES			if ($guesses==0){			print"<div id='lose'>You lose! Click NEXT PHRASE.</div>";		}		else if ($guesses==1){			print"<div id='last'>This is your last guess!!!</div>";		}			print "visible=$visible. Letters=$letters. Guesses=$guesses.";	display($visible,$letters);	drawinputscreen($guesses);		$_SESSION['visible']=$visible;	$_SESSION['guesses']=$guesses;		?></div></form></body></html>

Link to comment
Share on other sites

var_dump($session) is showing that the visible array is not changing from all 0's like it should. The cell where the letter is contained should be changing to a value of 1 but it is not. See the issue?

for ($i=1; $i<=$letterlength; $i++)			{				$ll=$letters[$i];				if ($guessletter==$letters[$i])				{					$visible[$i]=1;				}			}			var_dump($_SESSION);

Link to comment
Share on other sites

Can you see why this bit of code is acting all funky:

//IF LETTER IS NOWHERE TO BE FOUND. 				$guess_index=isset($_SESSION['bad_guess_index']) ? $_SESSION['bad_guess_index']: '';							if (!preg_match("/($guessletter)/",$text)){				print "<p style='float:right'>That letter was no where to be found.</p>";				$_SESSION['guess_array'][$guess_index] = $guessletter;				$_SESSION['bad_guess_index'] += 1;				$guess_index=$_SESSION['bad_guess_index'];				print"$guess_index <br/>";			}		$bad_guess_print=isset($_SESSION['guess_array']) ? $_SESSION['guess_array']: array();					$guess_index=$_SESSION['bad_guess_index'];				print"<div id='bad_guesses'>";		print"$guess_index <br/>";			//foreach($guess_index as $value){			for($i=0; $i<$guess_index; $i++){				print"<p style='float:right; color:red;'>$bad_guess_print[$i] </p>";			}		print"</div>";

The "/($guessletter)/" clearly isn't working. What it is supposed to do: When inputting a letter- if it is not in the phrase then it prints it out.

Link to comment
Share on other sites

I have to turn this in for school. I had it working for the most part on my local Apache server, but now it is not working right on our school server. The guesses print down by the buttons was working, now it is not. When you guess a correct letter and it sets it to visible in the table, the correct letters dissappear when you guess another correct letter! And the whole guess_array is not printing out! Can you see any of the issues! It is at http://sulley.dm.ucf.edu/~ca081919/DIG3134/project2.php Code below:

<?php // woffer02.php	error_reporting(0);	session_start();	if (empty($_SESSION)) {    //Initialize session variables    $_SESSION['bad_guess_index'] = 0;    $_SESSION['guess_array'] = array();	$_SESSION['index']=0;	$_SESSION['turn']="GOLD"; //GOOD}?>		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">		<html xmlns="http://www.w3.org/1999/xhtml">		<head>		<link rel="stylesheet" type="text/css" href="style_project_two.css" />		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		<title></title>			<style type="text/css">			.mtable td 			{				width:50px;			}		</style>			</head>				<body>		<div id="wrap">		<div id="header"><h1>Caitlin's Jeopardy</h1></div>        <div id="right">		<form method="post">	<?php		#textloader:		function textloader($whatfile)		{				if (!file_exists($whatfile))		{			print "Sorry, can't find $whatfile.";			exit;		}		else		{			$textarray=file($whatfile);			return $textarray;			}		}# textloader	#display: Draws the Jeopardy table	function display($visible, $letters)	{		$letterlength=count($letters);		$letterlength-=1;		print "<table class='mtable' border=1><tr>";		for ($i=1; $i<$letterlength; $i++)		{						//print "i=$i,visible=".$visible[$i]."<br />";			if ($letters[$i]==' ')				print "</tr></table><br/>				<table class='mtable'><tr>";			else if ($visible[$i])			 {				print "<td><center>".$letters[$i]."</center></td>";			 }			else				print "<td> </td>";		}		print "</tr></table>";	}		#drawinputscreen: Asks for input.	function drawinputscreen($guesses)	{		print "<input type='text' name='guessletter'>";		print "Guess a letter. You have $guesses guesses left.";		print "<input type='submit' name='action' value='GUESS'>";		print "<input type='submit' name='action' value='REVEAL'>";		print "<input type='submit' name='action' value='NEXT PHRASE'>";		//print "<input type='submit' name='action' value='END SESSION'> (then refresh)";	}		######## MAIN PROGRAM ##########	//print"<img src='images/vanna.jpg' alt=''/>";		//initialize variables	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$letters = array();	$act=isset($_POST['action']) ? $_POST['action'] : '';		$text = array_key_exists($index, $array) ? $array[$index] : "There are no more phrases left.";	$txtarr=($text);			//Print whos turn	$turn_print=$_SESSION['turn'];	print"<div id='team'>It is $turn_print team's turn.</div>";	//print "$text";			//Assign each letter to letters array and uppercase it		for ($i=0; $i<strlen($text);$i++)		{			$letters[$i+1]=strtoupper($text[$i]);			}				$letterlength=count($letters);		$visible=isset($_SESSION['visible']) ? $_SESSION['visible']: array();		$guesses=isset($_SESSION['guesses']) ? $_SESSION['guesses']: '';					/*	if(!$act){		for ($i=1; $i<=$letterlength; $i++)			$visible[$i]=0;					$guesses=16;	}	*/	switch ($act)	{			case '':			for ($i=1; $i<=$letterlength; $i++)			$visible[$i]=0;						//If there are no more phrases left		if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;		}					else{			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=0;		}			$guesses=16;		break;							case "GUESS":				$guessletter=strtoupper($_POST['guessletter']); // a-> A, etc. uppercase					for ($i=1; $i<=$letterlength; $i++)			{				$ll=$letters[$i];				if ($guessletter==$letters[$i])				{					$visible[$i]=1;				}			}			//var_dump($_SESSION);								//WHOS TURN??			$turn=$_SESSION['turn'];			if ($turn=="GOLD"){				$_SESSION['turn']="BLACK";			}			else if($turn=="BLACK"){				$_SESSION['turn']="GOLD";			} #End whos turn												$guesses=$guesses-1;						//IF LETTER IS NOWHERE TO BE FOUND. 				$guess_index=isset($_SESSION['bad_guess_index']) ? $_SESSION['bad_guess_index']: '';							if (!(preg_match("/($guessletter)/",$text))){				print "<p class='right_side'>That letter was no where to be found.</p>";				$_SESSION['guess_array'][$guess_index] = $guessletter;				$_SESSION['bad_guess_index'] += 1;				$guess_index=isset($_SESSION['bad_guess_index']) ? $_SESSION['bad_guess_index']: '';				//print"$guess_index <br/>";			}		$bad_guess_print=isset($_SESSION['guess_array']) ? $_SESSION['guess_array']: array();					$guess_index=$_SESSION['bad_guess_index'];				print"<div id='bad_guesses'><p>Bad Guesses</p> <br />";		//print"$guess_index <br/>";			//foreach($guess_index as $value){			for($i=0; $i<$guess_index; $i++){				print"<p class='right_side' style='color:red'>$bad_guess_print[$i] </p>";			}		print"</div>";			break;			case "NEXT PHRASE":			$_SESSION['index']+= 1;		unset($_SESSION['bad_guess_index']);		unset($_SESSION['guess_array']);		unset($_SESSION['visible']);		unset($_SESSION['letters']);		unset($_SESSION['guesses']);		$guesses=16;		for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=0;		break;				case "REVEAL":			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;			break;				case "END SESSION":			session_destroy();		break;	}					//RUN OUT OF GUESSES			if ($guesses==0){			print"<div id='lose'>You lose! Click NEXT PHRASE.</div>";		}		else if ($guesses==1){			print"<div id='last'>This is your last guess!!!</div>";		}			print"</div>";	print"<div id='left'>";	display($visible,$letters);		drawinputscreen($guesses);	print"</div>";			$_SESSION['visible']=$visible;	$_SESSION['guesses']=$guesses;		?><tt>Note: Guesses counter was working fine on local Apache server and the whole guess_array was printing as well. The correctly guessed letters were accumulating on the screen as they were guessed- as you see they are going away when another correct letter is guessed.They all are not working after being uploaded. Please let me know if you see the issues. Thanks. </tt></div></form></body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...