How to make one variable out of this?

Best,

I'm bussy making a website, but one thing holds me up.. I have to make a real long search query, and i have made this PHP code:

if($_GET['genre']) { 
echo 'SELECT * FROM movies WHERE `genre1` = '; 
foreach($_GET['genre'] as $genre) 
{   
$genres = array( "Actie", "Animatie", "Avontuur", "Documentaire", "Drama", "Erotiek", "Familie", "Fantasy", "Film", "Horror", "Komedie", "Misdaad", "Muziek", "Mystery", "Oorlog", "Roadmovie", "Romantiek", "Sciencefiction", "Thriller", "Western" );
if (!in_array($genre, $genres)) 
{
header('location: ?error=1'); 
} 
echo " '".$genre."' OR `genre2` = '".$genre."'"; if(end($_GET['genre']) !== $genre)
{ 
echo ' OR `genre1` = '; 
} 
} 
echo " AND `year` > '".$_GET['year1']."' AND `year` < '".$_GET['year2']."';"; 
} 
else
{ 
echo "SELECT * FROM movies WHERE `year` > '".$_GET['year1']."' AND `year` < '".$_GET['year2']."';"; 
}

On a URL like this:

127.0.0.1/querygenerator.php?genre%5B3%5D=Avontuur&genre%5B4%5D=Documentaire&genre%5B6%5D=Erotiek&year1=1900&year2=2014

And it outputs something like this:

SELECT * FROM movies WHERE `genre1` = 'Avontuur' OR `genre2` = 'Avontuur' OR `genre1` = 'Documentaire' OR `genre2` = 'Documentaire' OR `genre1` = 'Erotiek' OR `genre2` = 'Erotiek' AND `year` > '1900' AND `year` < '2014';

So, my question is, how can i make one PHP variable out this whole, so I can run the query? I can do this with file_get_contents but that's not so safe, I guess..

Thanks! - Karim

Answers


Just concatenate to a string rater than echoing:

if($_GET['genre']) { 
  $sql = 'SELECT * FROM movies WHERE `genre1` = '; 
  foreach($_GET['genre'] as $genre) {
    $genres = array( "Actie", "Animatie", "Avontuur", "Documentaire", "Drama", "Erotiek", "Familie", "Fantasy", "Film", "Horror", "Komedie", "Misdaad", "Muziek", "Mystery", "Oorlog", "Roadmovie", "Romantiek", "Sciencefiction", "Thriller", "Western" );
    if (!in_array($genre, $genres)) {
      header('location: ?error=1');
      die(); 
    } 
    $sql = $sql . " '".$genre."' OR `genre2` = '".$genre."'"; 
    if(end($_GET['genre']) !== $genre) { 
      $sql = $sql . ' OR `genre1` = '; 
    } 
  } 
  $sql = $sql . " AND `year` > '".$_GET['year1']."' AND `year` < '".$_GET['year2']."';"; 
} else { 
  $sql = "SELECT * FROM movies WHERE `year` > '".$_GET['year1']."' AND `year` < '".$_GET['year2']."';"; 
}

echo $sql;

Need Your Help

can't instantiate singleton with `factory-method`

java xml spring

I had a piece of code working that integrated Hibernate 3.3.2.GA and Spring 2.5.x and these versions were old so I decided to upgrade. I first upgraded Spring to the latest available version

Getting EBCDIC value of a character in C

c character ebcdic

I need to get the EBCDIC value of a character in C. I don't know how. Do I have to get the ASCII value first then get the EBCDIC value from there? Thanks anyone

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.