Drippingblood123 Posted December 4, 2009 Share Posted December 4, 2009 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"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/ Share on other sites More sharing options...
Drippingblood123 Posted December 4, 2009 Author Share Posted December 4, 2009 . Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/#findComment-971583 Share on other sites More sharing options...
Drippingblood123 Posted December 4, 2009 Author Share Posted December 4, 2009 . Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/#findComment-971598 Share on other sites More sharing options...
Drippingblood123 Posted December 4, 2009 Author Share Posted December 4, 2009 . Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/#findComment-971617 Share on other sites More sharing options...
Drippingblood123 Posted December 5, 2009 Author Share Posted December 5, 2009 Does anyone know? Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/#findComment-971661 Share on other sites More sharing options...
Drippingblood123 Posted December 5, 2009 Author Share Posted December 5, 2009 Anyone? :/ :/ Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/#findComment-971881 Share on other sites More sharing options...
Drippingblood123 Posted December 5, 2009 Author Share Posted December 5, 2009 I really need help. Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/#findComment-971897 Share on other sites More sharing options...
Drippingblood123 Posted December 5, 2009 Author Share Posted December 5, 2009 AlexWD do you know what to do? You've given some pretty good help before. Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/#findComment-971914 Share on other sites More sharing options...
MadTechie Posted December 5, 2009 Share Posted December 5, 2009 Please Read RULE #17! Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/#findComment-971963 Share on other sites More sharing options...
Alex Posted December 5, 2009 Share Posted December 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184028-search/#findComment-971973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.