Looping ELSEIF command ?

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

Looping ELSEIF command ?

Post 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 :)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Looping ELSEIF command ?

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

Re: Looping ELSEIF command ?

Post 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)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Looping ELSEIF command ?

Post 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++;
}

dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Looping ELSEIF command ?

Post 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?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Looping ELSEIF command ?

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

Re: Looping ELSEIF command ?

Post 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 .. :?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Looping ELSEIF command ?

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

Re: Looping ELSEIF command ?

Post by dominod »

The first one :D
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Looping ELSEIF command ?

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

Re: Looping ELSEIF command ?

Post 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:
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Looping ELSEIF command ?

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