[SOLVED] Comparing a variable to an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
base64
Forum Newbie
Posts: 2
Joined: Mon Oct 18, 2004 4:25 pm

[SOLVED] Comparing a variable to an array

Post by base64 »

Hi All!,

I am building a script which will take a single string from a user, and compare it to a table in a mysql database, and if it matches 1 of the entries in the db, it will redirect the user to the corresponding page as indicated in the sql database. So far i have the following code, but i am slighty at loss of how to do a vital part of the code. Sorry if this question is too noobish for you guys, but maybe someone can at least point me in the right direction?

Code i have so far:

Code: Select all

<?php

$db = mysql_connect("localhost", "root", "mypass");
mysql_select_db("mydb",$db);

$result = mysql_query("SELECT * FROM TABLE1",$db);

//now i am adding the rows in the msql db to two arrays

while ($myrow = mysql_fetch_array($result)) {

$CODE[] = $myrow

Code: Select all

;

$REDIR[] = $myrow[REDIR];

}


?>
i already have a html form that will call this php script, but what im not sure about is how I will compare the user input ($input) to each of the items contained in $CODE[], and then redirect the user to the corresponding entry in $REDIR[] providing that the input matched an existing entry. For example if the user inputs a code that matches the string in $CODE[2], i want them to be redirected to the page specified in $REDIR[2], and so on. If I am going about this in the wrong way please let me know, thanks alot i am fairly new at this. :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

generally it's done like this:

Code: Select all

$look = isset($_POST['look']) ? $_POST['look'] : '';

// your mysql connection and query here

$CODE = array();
while($row = mysql_fetch_assoc($result))
  $CODE[$row['CODE']] = $row['REDIR'];

if(isset($CODE[$look]))
  $redir = $CODE[$look];
else
  $redir = $default;

header( 'Location: ' . $redir );
base64
Forum Newbie
Posts: 2
Joined: Mon Oct 18, 2004 4:25 pm

Post by base64 »

Thx alot, ill have a look at php.net to figure out what you just did, but im sure this will help me accomplish my objective. Thanks Again!

:P :P
Post Reply