Jump to content

PHP newb needs some help from you!


phpBeginner1

Recommended Posts

I just started getting into PHP coding/programming. Today I need help with storing data from a form into different text files, and then displaying that data into a table. The basic outline of my code is this:

 

1) I start by setting $textfile = to the text file where data is saved. I then make an attempt to organize the data, separating each set of data by a “:” and then using an array to read this data and out put it using echo. I anticipate to replicate this node 5 additional times more, because every text file has different data. I want to use if commands to make sure that the correct data is read from the correct text files.

2) Below this I begin a javascript. This javascript basically allows me to hide/show a form that is developed using PHP.

3) Within the javascript is the form. The beginning of the form create text files depending upon what options were chosen in the form. A message is displayed to indicate that the information has been stored and also tells the end user where.

 

 

This is, in short, what my code says. Now for my conflict. The data won’t store. The system recognizes what is being selected within the form, and it creates the correct text file to receive data, however, the data does not save onto the text file. I believe that something at the top of my code with the array is not correct. Anyone have any suggestions? I am just starting with PHP, but I am already out of ideas.

 

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Device Manager</title>

<!-- Call JQuery -->
<script src="http://www.acmeous.com/tutorials/demo/acmeousCollapsibleLists/acmeousCollapsibleLists.js"
type="text/javascript"></script>


<!-- Setup Event Listener -->
<script type="text/javascript"><!--
if (window.addEventListener) {
window.addEventListener("load", function(){makeCollapsible(document.getElementById('addDevice'), 0);}, false);
} else if (window.attachEvent) {
window.attachEvent("onload", function(){makeCollapsible(document.getElementById('addDevice'), 1);});
} else {
window.onload = function(){makeCollapsible(document.getElementById('addDevice'), 1);};
} 
-->
</script>

</head>

<body>

<h2>DEVICE INVENTORY (Android): MOBILE TEAM</h2>
<?php		

	error_reporting(E_ALL); //E_ALL ^ E_NOTEICE
	ini_set('display_errors','1');

	$textfile = 'mtand.txt';
	$data = ''; //defining variable before use


	if (file_exists($textfile) && is_readable($textfile))		
	{			
		$data = file($textfile);		
	}

	for($i = 0; $i < sizeof($data) - 1; $i++)		
	{			

		//separate each element and store in temp array.		
		$tmp = explode(":", $data[$i]);					

		//assign each element to array. Line 51			
		$data[] = array('name' => $tmp[0], 'mdn' => $tmp[1], 'carrier' => $tmp[2], 'meid' => $tmp[3], 'version' => $tmp[4], 'date' => $tmp[5], 'notes' => $tmp[6], 'voice' => $tmp[7]);		
	}		
	echo "<table cellspacing='0' cellpadding='14'>";		
	if ($data!=""){			
		echo "<tr><th style='background:#6699FF'>Device Name</th><th style='background:#6699FF'>MDN</th><th style='background:#6699FF'>Carrier</th><th style='background:#6699FF'>MEID/IMEI</th><th style='background:#6699FF'>OS Version</th><th style='background:#6699FF'>Received Date</th><th style='background:#6699FF'>Notes</th><th style='background:#6699FF'>Voice</th></tr>";			
		foreach ($data as $key => $row)			
		{				

			$name[$key]= $row['name'];		
			$mdn[$key]= $row['mdn'];		
			$carrier[$key]= $row['carrier'];		
			$meid[$key]= $row['meid'];
			$version[$key]= $row['version'];
			$date[$key]= $row['date'];
			$notes[$key]= $row['notes'];
			$voice[$key]= $row['voice'];				

			echo "<tr><td>{$name[$key]}</td><td>{$mdn[$key]}</td><td>{$carrier[$key]}</td><td>{$meid[$key]}</td><td>{$version[$key]}</td><td>{$date[$key]}</td><td>{$notes[$key]}</td><td>{$voice[$key]}</td></tr>";			
		}

	}		
	echo "</table>";

?>


<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="750" height="500">
	<!-- MSTableType="layout" -->
	<tr>
		<td valign="top" height="320">
		<!-- MSCellType="ContentBody" -->
		<table cellpadding="0" cellspacing="0" width="750" height="320">
			<!-- MSCellFormattingTableID="1" -->
			<tr>
				<td height="310">
				<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
					<tr>
						<td valign="top" height="100%" width="100%">
						<!-- MSCellFormattingType="content" -->
						</td>
					</tr>
				</table>
				</td>
			</tr>

			<tr>
				<td height="10" width="750">
				<img alt="" width="1" height="10" src="MsSpacer.gif"></td>
			</tr>
		</table>
		</td>
	</tr>


	<tr>


		<td valign="top" height="180" width="750">
		<!-- MSCellType="ContentBody" -->

		<!-- Create id, hide and finalize expandable content -->
		<ul id="addDevice">
			<li>Add New Device<ul>

<?php		

if (isset($_POST['submit']))	 
{				

$platform= $_POST['platform']; //defining variable before use
$owner= $_POST['owner']; //defining variable before use

if ($platform=="Android") {
	if ($owner=="Mobile Team") {
//prepare 'mtand.txt' file for data	
$fh = fopen("mtand.txt","a+") or die("File Close"); }
}

if ($platform=="iOS") {
	if ($owner=="Mobile Team") {
//prepare 'mtios.txt' file for data	
$fh = fopen("mtios.txt","a+") or die("File Close"); }
}

if ($platform=="MISC") {
	if ($owner=="Mobile Team") {
//prepare 'mtmisc.txt' file for data	
$fh = fopen("mtmisc.txt","a+") or die("File Close"); }
}

if ($platform=="Android") {
	if ($owner=="Biz Dev") {
//prepare 'bdand.txt' file for data	
$fh = fopen("bdand.txt","a+") or die("File Close"); }
}

if ($platform=="iOS") {
	if ($owner=="Biz Dev") {
//prepare 'bdios.txt' file for data	
$fh = fopen("bdios.txt","a+") or die("File Close"); }
}

if ($platform=="MISC") {
	if ($owner=="Biz Dev") {
//prepare 'bdmisc.txt' file for data	
$fh = fopen("bdmisc.txt","a+") or die("File Close"); }
}


$name= $_POST['name'];		
$mdn= $_POST['mdn'];		
$carrier= $_POST['carrier'];		
$meid= $_POST['meid'];
$version= $_POST['version'];
$date= $_POST['date'];
$notes= $_POST['notes'];
$voice= $_POST['voice'];

/* $platform= $_POST['platform'] */
/* $owner= $_POST['owner'] */

//setup implementation rules for notes
$notes=stripslashes($notes);

//implement validation
if (empty($name)) {
print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
elseif (empty($mdn)) {
print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
elseif (empty($carrier)) {
print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
elseif (empty($meid)) {
print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
elseif (empty($version)) {
print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
elseif (empty($date)) {
print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }

else {


//assemble the data into a line variable		
$data= $name . ":" . $mdn . ":" . $carrier . ":" . $meid . ":" . $version . ":" . $date . ":" . $notes . ":" . $voice . ":" . "\r\n";		

//Figure out what choice was made so that data can be written to correct text file
if ($platform=="Android") {	
	 if ($owner=="Mobile Team") { 		
		/* $output = "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!" */
		print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
				}			
			}

//Figure out what choice was made so that data can be written to correct text file
if ($platform=="iOS") {	
	 if ($owner=="Mobile Team") { 
	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
				}			
			}

//Figure out what choice was made so that data can be written to correct text file
if ($platform=="MISC") {	
	 if ($owner=="Mobile Team") { 
	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
				}			
			}

//Figure out what choice was made so that data can be written to correct text file
if ($platform=="Android") {	
	 if ($owner=="Biz Dev") { 
	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
				}			
			}

//Figure out what choice was made so that data can be written to correct text file
if ($platform=="iOS") {	
	 if ($owner=="Biz Dev") { 
	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
				}			
			}

//Figure out what choice was made so that data can be written to correct text file
if ($platform=="MISC") {	
	 if ($owner=="Biz Dev") { 
	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
				}			
			}

//close the file		
fclose($fh);	     

}
} 	 
?>


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />	
<style type="text/css">		
	body{			
		font: 10px Verdana, Arial, Helvetica, sans-serif;		
	}	
</style>


<?php 

$output = ''; //defining variable before use. Line 255

if($output!=""){ echo $output; } ?>
<form action = "<? echo $_SERVER['PHP_SELF']?>" method = "post">	
<div style="border:2px solid #cccccc;margin:8px;padding:8px;width:600px;">	
<table width="80%">	
<tr><td><b>Enter Device Name: </b></td><td><input name="name" type="text" style="width:200px;" /></td></tr>
<tr><td><b>Enter MDN: </b></td><td><input name="mdn" type="text" style="width:200px;" /></td></tr>
<tr><td><b>Enter Carrier: </b></td><td><input name="carrier" type="text" style="width:200px;" /></td></tr>
<tr><td><b>Enter MEID/IMEI: </b></td><td><input name="meid" type="text" style="width:200px;" /></td></tr>
<tr><td><b>Enter OS Version: </b></td><td><input name="version" type="text" style="width:200px;" /></td></tr>
<tr><td><b>Enter Received Date: </b></td><td><input name="date" type="text" style="width:200px;" /></td></tr>
<tr><td><b>Enter Notes: </b></td><td><input name="notes" type="text" style="width:200px;" /></td></tr>	
<tr><td><b>Is Voice Enabled (Yes/No):</b></td><td>		
<select name="voice" style="width:200px;"> 	
<option>Yes</option>	
<option>No</option>		
</select></td></tr>

<tr><td><b>Select Device Platform (Android/iOS/MISC):</b></td><td>		
<select name="platform" style="width:200px;"> 	
<option>Android</option>	
<option>iOS</option>
<option>MISC</option>		
</select></td></tr>

<tr><td><b>Select Device Owner (Mobile Team/Biz Dev):</b></td><td>		
<select name="owner" style="width:200px;"> 	
<option>Mobile Team</option>	
<option>Biz Dev</option>		
</select></td></tr>	
</table>	
<input type="submit" name="submit" value="Update" />	
</div>
</form>	

</tr>
</table>
        </ul>
    </li>
</ul>
</div>

   

</body>

</html>

Link to comment
Share on other sites

Maybe you are doing too much at once. write a small php program which just reads a text file using file_get_contents and then play around with that for a while.

Then you can progress from there once you know about reading in file contents  etc.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.