Page 1 of 1

Why doesnt this code work? :(

Posted: Sun Jul 04, 2010 4:15 pm
by dominod
I dont get this code to work ... Anybody knows what I am doing wrong?

Code: Select all

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";
	}
when I use a actual word instead of a string in the following code it works .. (changed $keyword[$id] to 'keyword')

Code: Select all

if (wordsExist($search, array('keyword')))
{

	$redir = "http://wedidit.com"; 
	}
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 :)

Re: Why doesnt this code work? :(

Posted: Sun Jul 04, 2010 5:04 pm
by requinix
Yay for being unable to grasp fundamental concepts about code and variables!

Code: Select all

wordsExist($search, $keyword)

Re: Why doesnt this code work? :(

Posted: Sun Jul 04, 2010 5:48 pm
by dominod
I just started learning PHP 3 days ago so I´m kinda a newbie ..

Anyways that makes sense but it still wont work :banghead:

Re: Why doesnt this code work? :(

Posted: Sun Jul 04, 2010 6:02 pm
by requinix
Have you checked if $redir is being set appropriately? Have you turn on display_errors and made sure error_reporting is up all the way?

Code: Select all

<?php

ini_set("display_errors", true);
error_reporting(-1);

// rest of script

Re: Why doesnt this code work? :(

Posted: Mon Jul 05, 2010 3:29 am
by dominod
Well the redir works when I am not using a string inside the while loop.

I added the error reporting code now and the errors I got was this:

Code: Select all

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

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

Re: Why doesnt this code work? :(

Posted: Mon Jul 05, 2010 4:07 am
by dominod
Now I added $i = "0"; in the following code:

Code: Select all

$extract = mysql_query("SELECT * FROM engines");
$numrows = mysql_num_rows($extract);
$i = "0";
while ($row = mysql_fetch_array($extract))
And that got rid of :

Code: Select all

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
I also added a new function called wordExist where I deleted the following:

Code: Select all

    foreach($words as &$word) {
That got rid of :

Code: Select all

Warning: Invalid argument supplied for foreach() in /home/haukaas/public_html/new/search.php on line 25
So what I am struggeling with right now is this:

Code: Select all

Notice: Uninitialized string offset: 9 in /home/haukaas/public_html/new/search.php on line 48

Warning: Cannot modify header information - headers already sent by (output started at /home/haukaas/public_html/new/search.php:48) in /home/haukaas/public_html/new/search.php on line 86

Re: Why doesnt this code work? :(

Posted: Mon Jul 05, 2010 8:31 am
by esibobo
Warning: Cannot modify header information - headers already sent...
you get this error if you use the php redirect function after lines of code( php or html) which echo something to the screen.so structure your code such that your redirect statement comes before any line which echos something to the screen.

Re: Why doesnt this code work? :(

Posted: Mon Jul 05, 2010 3:47 pm
by dominod
I now put the header function at the top of the script.. Above "include("connect.php");" and everything ...

All it leaves me now with is a blank page .. No redirect :banghead:

Re: Why doesnt this code work? :(

Posted: Mon Jul 05, 2010 4:00 pm
by Ziq
Above of all put this 2 lines

Code: Select all

ini_set("display_errors", true);
error_reporting(-1);
and look at the errors. If it still

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/haukaas/public_html/new/search.php:48) in /home/haukaas/public_html/new/search.php on line 86
then look at the line (output started at /home/haukaas/public_html/new/search.php:48) you echo'ed something there or just have an empty string above your PHP code

Re: Why doesnt this code work? :(

Posted: Mon Jul 05, 2010 5:19 pm
by dominod
Thanks for helping me out Ziq :)

I did as you suggested and now I get this:

Code: Select all

 Notice: Undefined variable: redir in /home/haukaas/public_html/new/search.php on line 6

Warning: Cannot modify header information - headers already sent by (output started at /home/haukaas/public_html/new/search.php:6) in /home/haukaas/public_html/new/search.php on line 6
Line 6 and above is:

Code: Select all

<?php

ini_set("display_errors", true);
error_reporting(-1);

header("Location: $redir");
So obviously(?) the $redir string must be empty/undefined.. I kinda knew this, but I cant figure out how to solve it . . .

This code works perfect:

Code: Select all

elseif (wordsExist($search, array('keyword')))
{

	$redir = "http://domain.com"; //this should be a URL!
	}
While this dont :/

Code: Select all

if (wordExist($search, $keyword))
{

	$redir = "http://domain.com"; //this should be a URL!
	break;
	}
:crazy: