| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| New Member | Hi i made the following search function (with a little help from a previous post). At first when i used just 1 array , evry thing works well. But when i add a second and third array, the search feature would only work on the last array. What i mean by work, is that it would display the whole array[keys]. THe source codes: (the arrays are a bit large, but it works with only 1 array or the last one). Could any plzzzzz help so that the feature can search all of the arrays? <?php $subjects = array("A Death in Vienna","Daniel Silva","£18.16","23 February, 2004","Hardcover","Putnam Pub Group",'<img src="images\books\thumbs\a_death_in_vienna.jpg" alt="Click for more information">','<img src="images\books\full\a_death_in_vienna.jpg" alt="A Death in Vienna">',"Typical of the writing of author Daniel Silva, A DEATH IN VIENNA is a work of fact-based fiction based on his brilliant and thorough research."); $subjects = array("A Girl's Guide to Vampires","Katie Macalister","£16.99","November, 2003","Mass Market Paperback","Love Spell",'<img src="images\books\thumbs\a_girls_guide_to_vampires .jpg" alt="Click for more information">','<img src="images\books\full\a_girls_guide_to_vampires.j pg" alt="A Girls Guide to Vampires">',"If you're a fan of vampire romantic fiction, you'll enjoy this one. The basic premise is that our heroine, Joy, is accompanying her friend Roxy on a trip to the Czech Republic to try to visit a reclusive author of romantic vampire fiction and to explore whether or not vampires do exist."); $subjects= array("American Gods","Neil Gaiman","£17.99","30 April, 2002","Mass Market Paperback","HarperTorch",'<img src="images\books\thumbs\american_gods.jpg" alt="Click for more information">','<img src="images\books\full\american_gods.jpg" alt="American Gods">',"American Gods is Neil Gaiman's best and most ambitious novel yet, a scary, strange, and hallucinogenic road-trip story wrapped around a deep examination of the American spirit. Gaiman tackles everything from the onslaught of the information age to the meaning of death, but he doesn't sacrifice the razor-sharp plotting and narrative style he's been delivering since his Sandman days."); $subjects = array("Angels & Demons : A Novel","Atria Books","£7.98","01 July, 2003","Hardcover","Atria Books",'<img src="images\books\thumbs\angels_demons.jpg" alt="Click for more information">','<img src="images\books\full\angels_demons.jpg" alt="Angels & Demons : A Novel">',"A&D is a quick read that grabs you at the beginning and largely holds on for almost 600 pages. For all the pseudo-intellectuals posturing that the book is rife with mistakes - get over it, its fiction (and what are you doing reading best sellers anyway - isn't there some dusty text book in need of your attention?)"); $search = $_REQUEST['search']; $limit = count($subjects); if ($search) { for ($i=0; $i<$limit; $i++) { echo "<BR>Searching for a match with $subjects[$i]"; if($search == "$subjects[$i]") { echo "<BR>$search"; break; } } } ?> ************* Secondly*************** I also want the search feature to display more than one search result (if two or more arrays match the words). If is possible plzzz help. Thank you |
| |
| | #2 (permalink) |
| Senior Member | on the = where you assign the vairalbe to the array put a . so it becomes .= what that does saying include/merge any extistence of the variable $subjects you could rename the vairables subject1 subject2 subject3 then change if($search == "$subjects[$i]") to if($search == "$subjects$i[$i]") just to ways to get round it and then on the second bit use a while loop or sominthing like that maybe to look through the results |
| |
| | #3 (permalink) |
| New Member | Thanks for the reply Adam. Ok the .= didnt work. but if i use the: if($search == "$subjects$i[$i]") and changing the subjects to subjects1, 2 etc. What should i write for this line (sorry i'm new to php): $limit = count($subjects); (what do i replace the $subjects variable with, in order to recognise the arrays) I really appreciate your knowledge. |
| |
| | #5 (permalink) |
| Senior Member | Use array_push where you're adding more values to your array and then your code should work out how you want it to. Example: $subjects = array("A Death in Vienna","Daniel Silva","£18.16","23 February, 2004","Hardcover","Putnam Pub Group",'<img src="images\books\thumbs\a_death_in_vienna.jpg" alt="Click for more information">','<img src="images\books\full\a_death_in_vienna.jpg" alt="A Death in Vienna">',"Typical of the writing of author Daniel Silva, A DEATH IN VIENNA is a work of fact-based fiction based on his brilliant and thorough research."); array_push($subjects, "A Girl's Guide to Vampires","Katie Macalister","£16.99","November, 2003","Mass Market Paperback","Love Spell",'<img src="images\books\thumbs\a_girls_guide_to_vampires .jpg" alt="Click for more information">','<img src="images\books\full\a_girls_guide_to_vampires.j pg" alt="A Girls Guide to Vampires">',"If you're a fan of vampire romantic fiction, you'll enjoy this one. The basic premise is that our heroine, Joy, is accompanying her friend Roxy on a trip to the Czech Republic to try to visit a reclusive author of romantic vampire fiction and to explore whether or not vampires do exist."); You can also use in_array to search through arrays. Example: PHP Code: |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |