I dont get this code to work ... Anybody knows what I am doing wrong? include("connect.php"); $query =mysql_query("SELECT * FROM engines"); $numrows = mysql_num_rows($query); while ($row = mysql_fetch_array($query) { $name[$i]=$row['name']; $url[$i]=$row['url']; $searchurl[$i]=$row['searchurl']; $keyword[$i]=$row['keyword']; $i++; } function wordsExist(&$string, $words) { foreach($words as &$word) { if(stripos($string, $word) !== false) { return true; } } return false; } if (wordsExist($search, array($keyword[$id]))) { $redir = "http://wedidit.com"; } PHP: when I use a actual word instead of a string in the following code it works .. (changed $keyword[$id] to 'keyword') if (wordsExist($search, array('keyword'))) { $redir = "http://wedidit.com"; } PHP: What I am trying to do is to make it trigger a certian redirect when it finds a certain word from the database. Thanks in advance
Try this; include("connect.php"); $query =mysql_query("SELECT * FROM engines"); $numrows = mysql_num_rows($query); while ($row = mysql_fetch_array($query){ $name[$i]=$row['name']; $url[$i]=$row['url']; $searchurl[$i]=$row['searchurl']; $keyword[$i]=$row['keyword']; $i++; } function wordsExist(&$string, $words) { foreach($words as &$word) { if(strpos($string, $word)) { return true; } } return false; } if (wordsExist($search, $keyword)){ $redir = "http://wedidit.com"; } PHP:
Thanks but it still wont work Am I doing something wrong with pulling data from the database? It just gives me a blank page . .
to see if you are connecting good to your database you need to post here your connect.php file without passes of course
Here is the error I am getting: Notice: Undefined variable: i in /home/haukaas/public_html/new/search.php on line 14 Notice: Undefined variable: i in /home/haukaas/public_html/new/search.php on line 15 Notice: Undefined variable: i in /home/haukaas/public_html/new/search.php on line 16 Notice: Undefined variable: i in /home/haukaas/public_html/new/search.php on line 17 Notice: Undefined variable: i in /home/haukaas/public_html/new/search.php on line 18 12345678 Notice: Uninitialized string offset: 9 in /home/haukaas/public_html/new/search.php on line 38 Warning: Invalid argument supplied for foreach() in /home/haukaas/public_html/new/search.php on line 25 Warning: Cannot modify header information - headers already sent by (output started at /home/haukaas/public_html/new/search.php:14) in /home/haukaas/public_html/new/search.php on line 76 PHP:
Thanks that got rid of some of the notices .. This is whats left: Notice: Uninitialized string offset: 9 in /home/haukaas/public_html/new/search.php on line 40 Warning: Invalid argument supplied for foreach() in /home/haukaas/public_html/new/search.php on line 25 Warning: Cannot modify header information - headers already sent by (output started at /home/haukaas/public_html/new/search.php:40) in /home/haukaas/public_html/new/search.php on line 78 PHP: Line 40: if (wordsExist($search, $keyword[$i])) PHP: Line 25: foreach($words as &$word) { PHP: Line 78: header("Location: $redir") ; PHP:
I added a new function called wordExist where I deleted the following: foreach($words as &$word) { PHP: That got rid of : Warning: Invalid argument supplied for foreach() in /home/haukaas/public_html/new/search.php on line 25 PHP: I also did as you said and places the header at the very top of the script. Then it just took me to a blank page .. So then I placed the header under ini_set("display_errors", true); error_reporting(-1); PHP: and it gave me the following: Notice: Undefined variable: redir in /home/haukaas/public_html/new/search.php on line 10 Warning: Cannot modify header information - headers already sent by (output started at /home/haukaas/public_html/new/search.php:10) in /home/haukaas/public_html/new/search.php on line 10 PHP: Confused But still learning
Ok like u understand redir is undefined .. But I dont get it since I am not using any strings or anything in the $redir string .. I am using a normal URL as $redir . ..
try this: <?php error_reporting(E_ALL); require_once 'connect.php'; $query = mysql_query("SELECT * FROM engines"); $numrows = mysql_num_rows($query); $i = 0; while ($row = mysql_fetch_array($query)) { $name[$i] = $row['name']; $url[$i] = $row['url']; $searchurl[$i] = $row['searchurl']; $keyword[$i] = $row['keyword']; $i++; } function wordsExist($string, $words) { foreach ($words as $word) { if (stripos($string, $word)) { return true; } } return false; } if (wordsExist($search, $keyword)) { $redir = "http://wedidit.com"; } ?> PHP:
Thanks, I tried that code and all the error messages is now gone The problem is that it wont redirect to wedidit.com when it finds a keyword in the database via the string $keyword... Confusing!
Ok try this, and reply if you get any errors: <?php error_reporting(E_ALL); require_once 'connect.php'; $query = mysql_query("SELECT * FROM engines"); $numrows = mysql_num_rows($query); $i = 0; while ($row = mysql_fetch_array($query)) { $name[$i] = $row['name']; $url[$i] = $row['url']; $searchurl[$i] = $row['searchurl']; $keyword[$i] = $row['keyword']; $i++; } function wordsExist($string, $words) { foreach ($words as $word) { if (stripos($string, $word)) { return true; } } return false; } //added some debugging code... if (!is_array($keyword)) trigger_error('$keyword is not an array!', E_USER_ERROR); //added this as i did'nt see it defined... $search = "wedidit"; if (wordsExist($search, $keyword)) { $redir = "http://wedidit.com"; header("Location: {$redir}"); } ?> PHP:
try to bypass the header, add add beginning of your code : <? ob_start(); ?> and the end of your code: <? ob_flush(); ?> Let me know if you've tried it.. Good luck
You can try this : <?php error_reporting(E_ALL); require_once 'connect.php'; $query = mysql_query("SELECT * FROM engines"); $numrows = mysql_num_rows($query); $i = 0; while ($row = mysql_fetch_array($query)) { $name[$i] = $row['name']; $url[$i] = $row['url']; $searchurl[$i] = $row['searchurl']; $keyword[$i] = $row['keyword']; $i++; } function wordsExist($string, $words) { foreach ($words as $word) { if (!stripos($string, $word)) { return false; } } return true; } // remember to store keyword in $search variable!! if (wordsExist($search, $keyword)) { $redir = "http://wedidit.com"; header("Location: {$redir}"); } ?> PHP: