NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP Array Search

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

Advanced Search
5 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 10-25-2003, 02:27 PM THREAD STARTER               #1 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



PHP Array Search


Ok I am asking for someone to please look this over:
PHP Code:
<?php
$subjects 
= array(0=>"Calculus"1=>"Physics"2=>"Study Skills"3=>"Band"4=>"English"5=>"PE");
$search $POST_['search'];
//Search the array $subjects for the value $search.
$limit count($subjects);
for (
$i=0$i<$limit$i++)
{
    echo 
"<BR>Searching for a match with $subjects[$i]";
    
    if(
$search == "$subjects[$i]")
    {
        echo 
"<BR>$search is one of my classes.";
        break;
    }
}
?>
This code goes with the small HTML form:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Class Searcher</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="subjectsarray.php">
Search for:<INPUT TYPE="TEXT" NAME="search">
<INPUT TYPE="SUBMIT" VALUE="Search">
</FORM>
</BODY>
</HTML>
What I want it to print when I search for Study Skills is:
????: NamePros.com http://www.namepros.com/programming/15455-php-array-search.html
Searching for a match with Calculus
Searching for a match with Biology
Searching for a match with Study Skills
School Success is one of my classes.

But what it prints is:
Searching for a match with Calculus
Searching for a match with Biology
Searching for a match with Study Skills
Searching for a match with Band
Searching for a match with English
Searching for a match with PE

However, the similar code:
PHP Code:
<?php
$languages 
= array(0=>"Perl"1=>"PHP"2=>"Python");
$search "PHP";
// search the array $languages for the value $search
$limit count($languages);
for (
$i=0$i<$limit$i++)
{
    echo 
"<BR>Testing for match with $languages[$i]";
    if (
$search == $languages[$i])
    {
        echo 
"<BR>$search is an approved language.";
        break;
    }
????: NamePros.com http://www.namepros.com/showthread.php?t=15455
}
?>
(which does not use a form)
Works just fine. Anyone know what I am doing wrong?
__________________
--Alpha
Alpha is offline  
Old 10-25-2003, 09:40 PM   #2 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



If you add an if statement so it looks something like this:
PHP Code:
<?php 
$subjects 
= array(0=>"Calculus"1=>"Physics"2=>"Study Skills"3=>"Band"4=>"English"5=>"PE"); 
$search $POST_['search']; 
//Search the array $subjects for the value $search. 
$limit count($subjects); 
if (
$search) {
for (
$i=0$i<$limit$i++)
????: NamePros.com http://www.namepros.com/showthread.php?t=15455
{
    echo 
"<BR>Searching for a match with $subjects[$i]";
????: NamePros.com http://www.namepros.com/showthread.php?t=15455
    
    if(
$search == "$subjects[$i]")
    {
        echo 
"<BR>$search is one of my classes.";
        break;
    }
}
}
?>
I think that will get you what you're wanting.
deadserious is offline  
Old 10-25-2003, 10:30 PM THREAD STARTER               #3 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



Thanks but now it just brings up a blank page It's weird I don't get this at all lol. I usually try to make little programs like that as I am going thru the book so it helps me understand but boy this one is evil LOL.
__________________
--Alpha
Alpha is offline  
Old 10-25-2003, 10:55 PM   #4 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Try changine $POST_['search'] to $HTTP_POST_VARS['search'] and see if that makes a difference.
deadserious is offline  
Old 10-25-2003, 10:58 PM THREAD STARTER               #5 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



................................ IT WORKED!!!!!!! THANKS DEAD!!!!! Ok but that's still weird cuz in my other codes I dont have to put $HTTP_POST_VARS['search'] lol. Who knows PHP is evil THANKS AGAIN!!!!!!


EDIT: ......Dead.....I don't think I'm OK. I was supposed to be putting $_POST['search'] not $POST_['search']. Thank you VERY much
__________________
--Alpha
Alpha is offline  
Old 10-25-2003, 11:09 PM   #6 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Quote:
Originally posted by Alpha
????: NamePros.com http://www.namepros.com/showthread.php?t=15455
EDIT: ......Dead.....I don't think I'm OK. I was supposed to be putting $_POST['search'] not $POST_['search']. Thank you VERY much
Yea that could cause problems.
deadserious is offline  
Old 10-26-2003, 03:34 AM   #7 (permalink)
Senior Member
Join Date: May 2003
Posts: 2,187
adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough
 


Breast Cancer
i know its been solved but isnt post

$bob = $_POST['value']; and not $bob = $POST_['asd'];


?
adam_uk is offline  
Old 10-26-2003, 08:08 AM THREAD STARTER               #8 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



Quote:
Originally posted by Alpha
????: NamePros.com http://www.namepros.com/showthread.php?t=15455

EDIT: ......Dead.....I don't think I'm OK. I was supposed to be putting $_POST['search'] not $POST_['search']. Thank you VERY much
__________________
--Alpha
Alpha is offline  
Old 10-26-2003, 08:47 AM   #9 (permalink)
Senior Member
Join Date: May 2003
Posts: 2,187
adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough
 


Breast Cancer
ah right
adam_uk is offline  
Closed Thread


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


 
All times are GMT -7. The time now is 02:05 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger