Why doesnt this code work? :(

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
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Why doesnt this code work? :(

Post 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 :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Why doesnt this code work? :(

Post by requinix »

Yay for being unable to grasp fundamental concepts about code and variables!

Code: Select all

wordsExist($search, $keyword)
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Why doesnt this code work? :(

Post 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:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Why doesnt this code work? :(

Post 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
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Why doesnt this code work? :(

Post 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
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Why doesnt this code work? :(

Post 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
esibobo
Forum Newbie
Posts: 7
Joined: Mon May 31, 2010 7:05 am

Re: Why doesnt this code work? :(

Post 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.
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Why doesnt this code work? :(

Post 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:
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Why doesnt this code work? :(

Post 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
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Why doesnt this code work? :(

Post 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:
Post Reply