Why doesnt this code work? :(

Discussion in 'PHP' started by misterdh, Jul 4, 2010.

  1. #1
    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 :)
     
    misterdh, Jul 4, 2010 IP
  2. Triggs

    Triggs Active Member

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #2
    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:
     
    Triggs, Jul 4, 2010 IP
  3. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks but it still wont work :(

    Am I doing something wrong with pulling data from the database? It just gives me a blank page . . :(
     
    misterdh, Jul 4, 2010 IP
  4. Nerv

    Nerv Active Member

    Messages:
    568
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    70
    #4
    to see if you are connecting good to your database you need to post here your connect.php file without passes of course ;)
     
    Nerv, Jul 4, 2010 IP
  5. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    wont work means....., we can do something...,
    if you tell what type of error message you are getting
     
    strgraphics, Jul 4, 2010 IP
  6. helvetica

    helvetica Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    use echo or print.it works on me. inside your while echo"$i++";
     
    helvetica, Jul 4, 2010 IP
  7. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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:
     
    misterdh, Jul 5, 2010 IP
  8. helvetica

    helvetica Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    initialize the $i.
     
    helvetica, Jul 5, 2010 IP
  9. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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:
     
    misterdh, Jul 5, 2010 IP
  10. helvetica

    helvetica Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    header() should be on top of all scripts.
     
    helvetica, Jul 5, 2010 IP
  11. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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 :confused: But still learning :cool:
     
    misterdh, Jul 5, 2010 IP
  12. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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 . ..
     
    misterdh, Jul 5, 2010 IP
  13. largn81

    largn81 Peon

    Messages:
    237
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    "strpos" use it instead of what your using "stripos"
     
    largn81, Jul 5, 2010 IP
  14. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Done :) But still the same error msg . .
     
    misterdh, Jul 5, 2010 IP
  15. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #15
    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:
     
    danx10, Jul 5, 2010 IP
  16. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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!
     
    misterdh, Jul 5, 2010 IP
  17. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #17
    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:
     
    danx10, Jul 5, 2010 IP
  18. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Still nothing . . .

    $search is the word typed in a text field in a previous page ..
     
    misterdh, Jul 5, 2010 IP
  19. computeraholic

    computeraholic Member

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #19
    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 :)
     
    computeraholic, Jul 7, 2010 IP
  20. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #20
    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:
     
    killerj, Jul 8, 2010 IP