Page 1 of 1

[SOLVED] Comparing a variable to an array

Posted: Mon Oct 18, 2004 4:53 pm
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

Posted: Mon Oct 18, 2004 5:40 pm
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 );

Posted: Mon Oct 18, 2004 9:45 pm
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