Page 1 of 1

Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 8:59 am
by dominod
Hi

I want to loop the following code with data from a MySql database:

Code: Select all

elseif (wordsExist($search, array('$data1[$id]')))
{

	$redir = "$data2[$id]";
	}
I have the following code on top of the script:

Code: Select all

include("connect.php");


$query =mysql_query("SELECT * FROM `engines`");
$i=0;
while($row = mysql_fetch_array($query ))
{
$data1[$i]=$row['name'];
$data2[$i]=$row['url'];
$i++;
}
Can anyone explain how to do this?

Thanks in advance :)

Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 9:21 am
by Jade
Gah I can't even read your code.

Code: Select all

include("connect.php");

$query =mysql_query("SELECT * FROM `engines`);
$i=0;
while($row = mysql_fetch_array($query));
{
$data1[$i]=$row['name'];
$data2[$i]=$row['url'];
$i++;
}
elseif (wordsExist($search, array('$data1[$id]'))
{

        $redir = "$data2[$id]";
}
I believe that's what you meant to post. There is no elseif for while loops, however you can check to see if the query returned any results:

Code: Select all

include("connect.php");

$query =mysql_query("SELECT * FROM `engines`);
$i=0;
while($row = mysql_fetch_array($query));
{
$data1[$i]=$row['name'];
$data2[$i]=$row['url'];
$i++;
}

if (!mysql_num_rows($query)) //at this point there would be no $data1 because no results were returned
{
        $redir = ""; //there would be no data2 because the query returned no results
}
What I believe you're actually trying to do is this:

Code: Select all

include("connect.php");

$query =mysql_query("SELECT * FROM `engines`);

while($row = mysql_fetch_array($query));
{
   if (wordsExist($search, array($row['name']))
   {
         $redir = $row['url'];
         break; //stop executing the loop
    }
}

Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 9:41 am
by dominod
Hi

Sorry, I didnt realize that the forum messed up the code.

Anyways what I am trying to do is this:

I want the script to loop the following code for each row in the table:

Code: Select all

elseif (wordsExist($search, array('$data1[$id]'))
{

        $redir = "$data2[$id]";
}
Meaning that if I have two rows in the table which is $data1=google / $data2=google.com AND $data1=bing / $data2=bing.com it will loop the code twice so it will look like this:

Code: Select all


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

        $redir = "google.com";
}


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

        $redir = "bing.com";
}
Do you understand what I mean?

Thanks alot for your time 8)

Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 10:36 am
by Jade
You do realize that each time you loop and wordExists() is true then the $redir variable will be overwritten?

Code: Select all

include("connect.php");

$i = 0;
$query =mysql_query("SELECT * FROM `engines`);

while($row = mysql_fetch_array($query));
{
   $data1[$i]=$row['name'];
   $data2[$i]=$row['url'];

   if (wordsExist($search, array($data1[$id]))
         $redir = $data2[$id];
    
   $i++;
}


Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 10:49 am
by dominod
No I didnt .. Actually I am a PHP newbie :(

So how can I solve this?

And what did u mean by the code you added?

Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 10:53 am
by Jade
What is it exactly that you're trying to do? Are you trying to replace the words with URLs? Are you just trying to re-direct the page if it finds certain keywords in the text?

Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 10:57 am
by dominod
I am trying to re-direct the page if it finds certain keywords in the text :)

The keyword it need to find is in a mysql database.. The same is the redirect URL.

I tried the code you attached but it didnt work .. :?

Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 11:20 am
by Jade
So how do you want to handle cases where someone has both google and bing in the text? Do you want it to redirect to the first one it finds, the last one or have it just pick one at random?

Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 12:58 pm
by dominod
The first one :D

Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 1:50 pm
by Jade

Code: Select all

<?php
include("connect.php");

//execute the query
$query =mysql_query("SELECT * FROM `engines`);

//loop through all the row results from the engines table
while($row = mysql_fetch_array($query));
{

   if (wordsExist($search, array($row['name'])) //we found a word in the $search text that matches something in the database
   {
         $redir = $row['url']; //this is the URL we have for that keyword
         break; //stop executing the loop, we found one to redirect to
    }
}

if ($redir) //we found somewhere to redirect them to
{
    header("Location: $redir");
    exit;
}
?>

Re: Looping ELSEIF command ?

Posted: Fri Jul 02, 2010 2:37 pm
by dominod
Hmm .. I cant get it to work :(

I now use only the code you sendt me but nothing happens when I click enter on the form. It just takes me to a blank page .. :banghead:

Re: Looping ELSEIF command ?

Posted: Tue Jul 06, 2010 8:40 am
by Jade
Have you put in a case for if no words are found? The code I gave you will only redirect them if it finds a match... otherwise you'll get a blank page. You'll need to add code to handle situations when there are no matches found and the user isn't redirected.