Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

PHP Array Search

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

Alpha

Established Member
Impact
0
Ok I am asking for someone to please look this over:
PHP:
<?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:
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:
<?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;
	}
}
?>
(which does not use a form)
Works just fine. Anyone know what I am doing wrong?
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
If you add an if statement so it looks something like this:
PHP:
<?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++)
{
	echo "<BR>Searching for a match with $subjects[$i]";
	
	if($search == "$subjects[$i]")
	{
		echo "<BR>$search is one of my classes.";
		break;
	}
}
}
?>

I think that will get you what you're wanting.
 
0
Close
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.
 
0
Close
Try changine $POST_['search'] to $HTTP_POST_VARS['search'] and see if that makes a difference. :D
 
0
Close
................................ 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 :P 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 :D
 
0
Close
Originally posted by Alpha
EDIT: ......Dead.....I don't think I'm OK. I was supposed to be putting $_POST['search'] not $POST_['search']. Thank you VERY much :D

Yea that could cause problems. :D
 
0
Close
i know its been solved but isnt post

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


?
 
0
Close
Originally posted by Alpha

EDIT: ......Dead.....I don't think I'm OK. I was supposed to be putting $_POST['search'] not $POST_['search']. Thank you VERY much :D

:)
 
0
Close
ah right :)
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back