[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 04-24-2004, 04:07 PM   #1 (permalink)
New Member
 
Join Date: Apr 2004
Posts: 2
2.00 NP$ (Donate)

Junior is an unknown quantity at this point


Sad Arrays search help !! Plzzzz

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","&pound18.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","&pound16.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","&pound17.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","&pound7.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
Junior is offline  
Old 04-24-2004, 04:16 PM   #2 (permalink)
Senior Member
 
Join Date: May 2003
Posts: 2,211
6,170.25 NP$ (Donate)

adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough

Breast Cancer
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
adam_uk is offline  
Old 04-25-2004, 11:25 AM   #3 (permalink)
New Member
 
Join Date: Apr 2004
Posts: 2
2.00 NP$ (Donate)

Junior is an unknown quantity at this point


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.
Junior is offline  
Old 04-26-2004, 11:24 AM   #4 (permalink)
Senior Member
 
Join Date: May 2003
Posts: 2,211
6,170.25 NP$ (Donate)

adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough

Breast Cancer
erm

i would change it to this

$limit = 5;

reason i selelcted is because you are saying while $limit is less than (you have 4 variables add one so it will look at the last one
adam_uk is offline  
Old 04-27-2004, 02:02 AM   #5 (permalink)
Senior Member
 
Join Date: Aug 2002
Posts: 1,300
2.85 NP$ (Donate)

deadserious has a spectacular aura aboutdeadserious has a spectacular aura about


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","&pound18.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","&pound16.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:
if ($search)
{
    if (
in_array($search, $subjects))
    {
        echo
"Found $search";
    }
    else
    {
        echo
"$search not found";
    }
}
But if you use array_push like above, then your code should work the way you expect it to. And if you want it to show the results if there was more than one match, then just remove your break; statement.
deadserious is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 02:43 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85