Jump to content

Search


Drippingblood123

Recommended Posts

So I'm trying to make a program that makes it so that you input whether it's a hardback or paperback book and it outputs if it's found or not found and how many books are in stock based on the 2 txt files i have. I can get it to display the Not Found part, but the problem is is that it does it for every book. My code:

 

 

<form method="post" action="">
    <p>Name: <input type="text" name="book" value="<? echo $_POST['book'];?>" ></p>
    <p>Hardback or Paperback (enter h or p): <input type="text" name="hp" value=<? echo $_POST['hp'];?> ></p>
<p><input type="submit" name="submit" value="Reorder" /></p>
<?

if( isset($_POST['submit'])){
$book = $_POST['book'];
$hp = $_POST['hp'];
$paperback = array();
$hardback = array();

$file = fopen("paperback.txt", "r") or exit("Unable to open file!");

while(!feof($file))

  {

  $f = fgets($file);

  array_push($paperback, $f);

  }

fclose($file);

$file2 = fopen("hardback.txt", "r") or exit("Unable to open file!");

while(!feof($file2))

  {

  $g = fgets($file2);

  array_push($hardback, $g);

  }

fclose($file2);
//print_r($paperback);
$implode = implode('",',$paperback);
$explode = explode('",',$implode);
$numbers = array();
$paper_titles = array();
//print_r($explode);
for($i = 1; $i <= 9; $i = $i + 2){
array_push($numbers,$explode[$i]);
}
//print_r($numbers);
for($i = 0; $i <= 8; $i = $i + 2){
array_push($paper_titles,$explode[$i]);
}

//print_r($paper_titles);
sort($paper_titles);
$first = 0;
$last = 4;
$flag = false;
if($hp == "p"){
	while($first <= $last && $flag == false){
		$middle = intval(($first + $last) / 2);
			if($book == $paper_titles[$middle]){
				$flag = true;
			}
			elseif($paper_titles[$middle] > $book){
				$last = $middle - 1;
			}
			elseif($paper_titles[$middle] < $book){
				$first = $middle + 1;
			}

	}

	if($flag == true){
		echo "Found, and there are " . $numbers[$middle] . " copies in stock.";
	}
	elseif($flag == false){
		echo "Not found";
	}


}
$implode_2 = implode('",',$hardback);
$explode_2 = explode('",',$implode_2);
$numbers_2 = array();
$hard_titles = array();
//print_r($explode);
for($i = 1; $i <= 9; $i = $i + 2){
array_push($numbers_2,$explode_2[$i]);
}
//print_r($numbers);
for($i = 0; $i <= 8; $i = $i + 2){
array_push($hard_titles,$explode_2[$i]);
}

//print_r($hard_titles);
sort($hard_titles);
$first_2 = 0;
$last_2 = 3;
$flag_2 = false;
if($hp == "h"){
	while($first_2 <= $last_2 && $flag_2 == false){
		$middle_2 = intval(($first_2 + $last_2) / 2);
			if($book == $hard_titles[$middle_2]){
				$flag_2 = true;
			}
			elseif($hard_titles[$middle_2] > $book){
				$last_2 = $middle_2 - 1;
			}
			elseif($hard_titles[$middle_2] < $book){
				$first_2 = $middle_2 + 1;
			}

	}

	if($flag_2 == true){
		echo "Found, and there are " . $numbers_2[2] . " copies in stock.";
	}
	elseif($flag_2 == false){
		echo "Not found";
	}


}
}
?>

Link to comment
Share on other sites

What's the format that data is stored in the two files? Can you post an example?

 

I'd also suggest you use a select input for hardback/paperback instead of having them enter h or p, it's much more user-friendly.

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.