Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cabbit

macrumors 68020
Original poster
Jan 30, 2006
2,128
1
Scotland
I made a wee test thingie for making the dynamic WHERE statement, now all i need to work out is how to only have as many AND statements as needed and only output the data i need. Anyone know how to do this?


PHP:
<?php
	if (!isset($_GET['subpage'])) {
		echo '<form action="search.php?subpage=upload" method="post" />';
		echo 'Title: <input type="text" name="title">';
		echo '<br />';
		echo 'Body: <input type="text" name="body">';
		echo '<br />';
		echo 'Author: <input type="text" name="author">';
		echo '<br />';
		echo '<input type="checkbox" name="1" value="true">';
		echo '<input type="checkbox" name="2" value="true">';
		echo '<input type="checkbox" name="3" value="true">';
		echo '<input type="submit">';
		echo '</form>';
	}
	if (isset($_GET['subpage']) AND $_GET['subpage'] == "upload") {
		$title = $_POST['title'];
		$body = $_POST['body'];
		$author = $_POST['author'];
		$num1 = $_POST['1'];
		$num2 = $_POST['2'];
		$num3 = $_POST['3'];
		if (isset($_POST['title'])) {
			if ($_POST['title'] == "") {
				// nought //
			}
			else {
			$title_string = "
				story.`title` LIKE'%$title%'
				OR
				LOWER(story.`title`) LIKE '%$title%'
				OR
				UPPER(story.`title`) LIKE '%$title%'
			";
			}
		}
		if (isset($_POST['author'])) {
			if ($_POST['author'] == "") {
				// nought //
			}
			else {
			$author_string = "
				story.`author` LIKE'%$author%'
				OR
				LOWER(story.`author`) LIKE '%$author%'
				OR
				UPPER(story.`author`) LIKE '%$author%'
			";
			}
		}
		if (isset($_POST['body'])) {
			if ($_POST['body'] == "") {
				// nought //
			}
			else {
			$body_string = "
				story.`body` LIKE'%$body%'
				OR
				LOWER(story.`body`) LIKE '%$body%'
				OR
				UPPER(story.`body`) LIKE '%$body%'
			";
			}
		}
		if (isset($_POST['1'])) {
			if ($_POST['1'] == "") {
				// nought //
			}
			else {
			$num1_string = "
				key.`1` = '$num1'
			";
			}
		}
		if (isset($_POST['2'])) {
			if ($_POST['2'] == "") {
				// nought //
			}
			else {
			$num2_string = "
				key.`2` = '$num2'
			";
			}
		}
		if (isset($_POST['3'])) {
			if ($_POST['3'] == "") {
				// nought //
			}
			else {
			$num3_string = "
				key.`3` = '$num3'
			";
			}
		}
		echo 'WHERE '.$title_string.' AND '.$author_string.' AND '.$body_string.' AND '.$num1_string.' AND '.$num2_string.' AND '.$num3_string.''; 
		echo '<br />';
		echo '<a href="search.php">back</a>';
	}
?>
 
trying this but the result is empty. anyone know why?

PHP:
<?php
	if (!isset($_GET['subpage'])) {
		echo '<form action="search.php?subpage=upload" method="post" />';
		echo 'Title: <input type="text" name="title">';
		echo '<br />';
		echo 'Body: <input type="text" name="body">';
		echo '<br />';
		echo 'Author: <input type="text" name="author">';
		echo '<br />';
		echo '<input type="checkbox" name="1" value="true">';
		echo '<input type="checkbox" name="2" value="true">';
		echo '<input type="checkbox" name="3" value="true">';
		echo '<input type="submit">';
		echo '</form>';
	}
	if (isset($_GET['subpage']) AND $_GET['subpage'] == "upload") {
		echo '<a href="search.php">back</a>';
		$title = $_POST['title'];
		$body = $_POST['body'];
		$author = $_POST['author'];
		$num1 = $_POST['1'];
		$num2 = $_POST['2'];
		$num3 = $_POST['3'];
		if (isset($_POST['title'])) {
			if ($_POST['title'] == "") {
				// nought //
			}
			else {
			$title_string = "
				story.`title` LIKE'%$title%'
				OR
				LOWER(story.`title`) LIKE '%$title%'
				OR
				UPPER(story.`title`) LIKE '%$title%'
			";
			}
		}
		if (isset($_POST['author'])) {
			if ($_POST['author'] == "") {
				// nought //
			}
			else {
			$author_string = "
				story.`author` LIKE'%$author%'
				OR
				LOWER(story.`author`) LIKE '%$author%'
				OR
				UPPER(story.`author`) LIKE '%$author%'
			";
			}
		}
		if (isset($_POST['body'])) {
			if ($_POST['body'] == "") {
				// nought //
			}
			else {
			$body_string = "
				story.`body` LIKE'%$body%'
				OR
				LOWER(story.`body`) LIKE '%$body%'
				OR
				UPPER(story.`body`) LIKE '%$body%'
			";
			}
		}
		if (isset($_POST['1'])) {
			if ($_POST['1'] == "") {
				// nought //
			}
			else {
			$num1_string = "
				key.`1` = '$num1'
			";
			}
		}
		if (isset($_POST['2'])) {
			if ($_POST['2'] == "") {
				// nought //
			}
			else {
			$num2_string = "
				key.`2` = '$num2'
			";
			}
		}
		if (isset($_POST['3'])) {
			if ($_POST['3'] == "") {
				// nought //
			}
			else {
			$num3_string = "
				key.`3` = '$num3'
			";
			}
		}
		$strArray[] = $title_string; 
		$strArray[] = $author_string; 
		$strArray[] = $body_string; 
		$strArray[] = $num1_string; 
		$strArray[] = $num2_string; 
		$strArray[] = $num3_string; 
		$queryStr = 'WHERE '; 
		$andStr = ' AND '; 
		foreach($strArray as $str) 
		{ 
   			if(!isempty($str)) 
   			{ 
     			$queryStr = $queryStr . $str . $andStr; 
   			} 
		}	 
		// Remove last $andStr added to str 
		$queryStr = substr($queryStr, 0, strlen($queryStr)-strlen($andStr)); 
		echo $queryStr;
	}
?>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.