Jump to content

Increase value of Array by 1 via Form


ibz786

Recommended Posts

I have a text file with the following:

 

HA11QS, 200, house1.jpg, 4

HA22BR, 280, house2.jpg, 10

HA33AB, 390, house3.jpg, 3

HA44CD, 320, house4.jpg, 8

 

I have a form:

<?php

if (isset($_POST['price']))
{
$filename = "houses.txt";
$fileOpen = fopen($filename, "r");
$max = $_POST['price'];
$rowsArr  = file ($filename);
foreach ($rowsArr as $row)
	{
		$lineDetails = $row;
		$item_array = explode (",", $row);

		if (((int) $item_array[1]) <= $max)
		{
			?>
                <form action='visit2.php' method='post'>
                <?php
			echo("Post Code - " . $item_array[0]. "<br>");
			echo("Price - " . $item_array[1]. ",000 <br>");
			echo("Picture - " . $item_array[2]. "<br>");
			echo("Number of Visits - " . $item_array[3]. "<br>");
			?>
                
			<input type='checkbox' name='mybox'>
			<input type='submit' value='Visit Property'>
			</form>
			<?php
		}


	}
	fclose($fileOpen);
}
?>

 

and i also have "visit.php"

 

<?php

if (isset($_POST['mybox']))
{
$filename = "houses.txt";
$fileOpen = fopen($filename, "r+");

$filename2 = "houses2.txt";
$fileWrite = fopen($filename2, "w");



$visit = $_POST['mybox'];

$rowsArr  = file ($filename);
foreach ($rowsArr as $row)
	{
		$lineDetails = $row;
		$item_array = explode (",", $row);

		$fileText = $item_array[3]+1;


		if ($visit == true)
		{

			echo("Post Code - " . $item_array[0]. "<br>");
			echo("Price - " . $item_array[1]. ",000 <br>");
			echo("Picture - " . $item_array[2]. "<br>");
			echo("Number of Visits - " . $item_array[3] . "<br>");
			echo("<br>");
			fwrite($fileWrite, $fileText);

		}


	}
	fclose($fileOpen);
	fclose($fileWrite);

}


?>

 

What i need is , when the user in the form, clicks on the checked box, it sends the information to 'visits.php' and it writes to the text file, increasing the selected value by one

 

Please can anyone help, im really struggling with this

 

Thank You

 

 

 

 

Link to comment
Share on other sites

Im really confused, if the thing i need is either to write or append

I feel it maybe appened, but how do i ?

e.g.

After clicking on the first house, increase (and write to the file) the number of visits from 4 to 5?

 

If anyone knows where im going wrong please help in addition i know in the code provided i have:

"houses.txt" and then "houses2.txt", i was just experimenting since i didnt want to spoil the original file if anything were to go wrong

 

Link to comment
Share on other sites

Indeed, its part of my coursework :(

I know for a fact that all that i am doing would be so much easier via an SQL Database, and i have more of an idea on how to do it on there since arrays as such wouldnt be needed etc ...

However our lecturer wants to read it from a text file which is really troublesome :(

Link to comment
Share on other sites

try this

$item_array[3] += 1;

instead of

$fileText = $item_array[3]+1;

 

Hi i tried that:

echo("Picture - " . $item_array[2]. "<br>");
echo("Number of Visits - " . $item_array[3] += 1 . "<br>");

 

unfortunately it didnt work :S

 

I may have not put it in the right area, but still not sure :/

 

Edit:

Also i have realised that when i write "houses2.txt" Instead of updating the number of visits, the whole file becomes blank :S

Link to comment
Share on other sites

you changed the wrong code.

 

replace $fileText = $item_array[3]+1;

with $item_array[3] += 1;

 

My apologies,

I tried changing that however, the problem remains the same, it doesnt seem to replace 4 with 5 (for the first property)

I just seems to overwrite the whole txt file and leaves it blank

 

Not sure what to do here really :/

Link to comment
Share on other sites

that's because you write to the file with

 

fwrite($fileWrite, $fileText);

 

however, $fileText is an empty variable. I think what you need to do is changing all the "echo" above this line to $fileText .=

//so this
echo("Post Code - " . $item_array[0]. "<br>");
//becomes this
$fileText .= "Post Code - " . $item_array[0]. "<br>";

 

additionally, I think you check twice whether the checkbox is checked or not, once with

 

if (isset($_POST['mybox']))

 

and once with

 

if ($visit == true)

Link to comment
Share on other sites

Ok ive made the code to work slightly:

It has indeed incremented it, by one however i have a few errors, i have uploaded a picture of the error

 

My current code is:

<?php

if (isset($_POST['mybox']))
{
$filename = "houses2.txt";
$fileOpen = fopen($filename, "a");

$visit = $_POST['mybox'];

$rowsArr  = file ($filename);
foreach ($rowsArr as $row)
	{
		$lineDetails = $row;
		$item_array = explode (",", $row);
		$newVisit = $item_array[3]+=1;
		$fileText = "Number of Visits - " . $newVisit . "<br>";



			echo("Post Code - " . $item_array[0]. "<br>");
			echo("Price - " . $item_array[1]. ",000 <br>");
			echo("Picture - " . $item_array[2]. "<br>");
			echo("Number of Visits - " . $item_array[3]. "<br>");
			echo("<br>");
			fwrite($fileWrite, $fileText);




	}
	fclose($fileOpen);

}


?>

 

Not sure where im going wrong :S

 

[attachment deleted by admin]

Link to comment
Share on other sites

Yeah sorry i realised that straight after i posted that my aplogies, been a long day

 

Ok now what happens is that ive got the code to increment the house visits by one, however, it doesnt overwrite e.g. 4 (for house one) to 5, i just creates a whole new line :s

Link to comment
Share on other sites

<?php
if (isset($_POST['mybox'])) {
$filename = "houses2.txt";
$file = fopen($filename, "a");	
$rowsArr  = file($filename);

foreach ($rowsArr as $row) {
	$item_array = explode (",", $row);
	$item_array[3] += 1; //no need to save this to a new variable
	$fileText = "Post Code - " . $item_array[0]. "<br>";
	$fileText .= "Price - " . $item_array[1]. ",000 <br>";
	$fileText .= "Picture - " . $item_array[2]. "<br>";
	$fileText .= "Number of Visits - " . $item_array[3]. "<br>";
	$fileText .= "<br>";
	fwrite($file, $fileText);
}
fclose($file);	
}
?>

 

hope it works!

Link to comment
Share on other sites

I just realized that I completely misunderstood what you wanted to do. I think this should work, but it's untested

<?php
//form.php
if (isset($_POST['price'])) {
$filename = "houses.txt";
$fileOpen = fopen($filename, "r");
$max = $_POST['price'];
$rowsArr = file($filename);
$i = 0;
foreach ($rowsArr as $row) {
	$item_array = explode(", ", $row);
	if (((int) $item_array[1]) <= $max) {
		?>
		<form action='action.php' method='post'>
		<?php
		echo("Post Code - " . $item_array[0]. "<br>");
		echo("Price - " . $item_array[1]. ",000 <br>");
		echo("Picture - " . $item_array[2]. "<br>");
		echo("Number of Visits - " . $item_array[3]. "<br>");
		?>
		<input type="hidden" name="linenumber" value="<?php echo $i; ?>" />
		<input type='checkbox' name='mybox' />
		<input type='submit' value='Visit Property' />
		</form>
		<?php
	}
	$i++;
}
fclose($fileOpen);
}

//action.php
if (isset($_POST['mybox'])) {
$filename = "houses.txt";
$file = fopen($filename, "a");	
$rowsArr = file($filename);

$row = $rowsArr[$_POST["linenumber"]]; //get the row we want to change
$item_array = explode(", ", $row); //split it into an array so we can change
$item_array[3] += 1; //do the change
$row = implode(", ", $item_array); //re-assemble the row
$rowsArr[$_POST["linenumber"]] = $row; //replace the old row with the re-assembled one
$fileText = implode("\n", $rowsArray); //re-assemble the file by joining all rows

echo("Post Code - " . $item_array[0]. "<br>");
echo("Price - " . $item_array[1]. ",000 <br>");
echo("Picture - " . $item_array[2]. "<br>");
echo("Number of Visits - " . $item_array[3]. "<br>");
echo("<br>");

fwrite($file, $fileText);
fclose($file);
}

?>

 

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.