making a query a global variable

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
sickly_man
Forum Newbie
Posts: 2
Joined: Mon Jul 23, 2007 10:59 am

making a query a global variable

Post by sickly_man »

is it possible to make a query (i.e. $query = SELECT * FROM subnets .....) a global variable so it can be used over and over? my code builds a query based on whether or not textboxes are filled and i need to use the query the user creates over and over. i need to do this because i need to display the results of the query by rows of 3. my problem is i can only get it to display the first page (first 3 rows) of info, the next button doesnt work because there are no textboxes to check for the second time around, and it is telling me the query is empty. can anyone help me? thanks. my code is below.

Code: Select all

<?php

include	"iprange.inc";
include	"error.inc";
include	"paths.inc";
include	"functions2.php";


if ( !empty($_GET["offset"]) )
{
	// initialize browse3() params
	$scriptName = "search_unavailable.php";

	$offset = $_GET['offset'];

	// HTML <TABLE> column headers
	$header[0]["header"] = "Id";
	$header[1]["header"] = "Network";
	$header[2]["header"] = "Mask";
	$header[3]["header"] = "Size";
	$header[4]["header"] = "Type";
	$header[5]["header"] = "Router Info";
	$header[6]["header"] = "Description";
	$header[7]["header"] = "Member Id";
	$header[8]["header"] = "User";
	$header[9]["header"] = "Assigned";

	// query attributes to display in <TABLE> columns
	$header[0]["attrib"] = "id";
	$header[1]["attrib"] = "network";
	$header[2]["attrib"] = "mask";
	$header[3]["attrib"] = "size";
	$header[4]["attrib"] = "type";
	$header[5]["attrib"] = "router_info";
	$header[6]["attrib"] = "description";
	$header[7]["attrib"] = "member_id";
	$header[8]["attrib"] = "user";
	$header[9]["attrib"] = "assigned";

	// (1) Open the database connection
	$connection = mysql_connect($hostName,$username,$password);

	// (2) Select the database
	mysql_select_db($databaseName, $connection); 

	// use the browse3() function
	browse3( $scriptName, $connection, $offset, $query, $header );

	// choose to browse more or go to main menu
	echo	'<form name="browse_options" method="post" action="search_unavailable.php"><p>' .
		'<input type="button" value="Browse More Networks" 
			onclick="window.location.href=\'search_unavailable.php\'"><p>' .
		'<input type="button" value="Main Menu" 
			onclick="window.location.href=\'interface.php\'" ><br><p>';
	echo	'</form><p>';

	// close the connection
	if (!(mysql_close($connection)))
		showerror();
}
elseif ( !empty($_POST["descr"]) || !empty($_POST["network"]) || !empty($_POST["size_dropdown"]) )
{
	// initialize browse3() params
	$scriptName = "search_unavailable.php";

	// set $offset to zero if not previously set
	if ( empty($offset) )
		$offset = 0;

//	$offset = $_GET['offset'];

	// HTML <TABLE> column headers
	$header[0]["header"] = "Id";
	$header[1]["header"] = "Network";
	$header[2]["header"] = "Mask";
	$header[3]["header"] = "Size";
	$header[4]["header"] = "Type";
	$header[5]["header"] = "Router Info";
	$header[6]["header"] = "Description";
	$header[7]["header"] = "Member Id";
	$header[8]["header"] = "User";
	$header[9]["header"] = "Assigned";

	// query attributes to display in <TABLE> columns
	$header[0]["attrib"] = "id";
	$header[1]["attrib"] = "network";
	$header[2]["attrib"] = "mask";
	$header[3]["attrib"] = "size";
	$header[4]["attrib"] = "type";
	$header[5]["attrib"] = "router_info";
	$header[6]["attrib"] = "description";
	$header[7]["attrib"] = "member_id";
	$header[8]["attrib"] = "user";
	$header[9]["attrib"] = "assigned";

	// (1) Open the database connection
	$connection = mysql_connect($hostName,$username,$password);

	// (2) Select the database
	mysql_select_db($databaseName, $connection); 
	

	// start beginning of query to build on
	$query = "SELECT * FROM subnets WHERE ";

	// build a query for just a description search
	if ( !empty($_POST["descr"]) && empty($_POST["network"]) )
	{
		$query .= "description = '" . $_POST['descr'] . "'";
		//$description = $_POST['descr'];
		//global_session('description');
	}

	// build a query for just a network search
	if ( !empty($_POST["network"]) )
	{
		// build a query for a search on description AND network
		if ( !empty($_POST["description"]) )
		{
			$query .= "description = '" . $_POST['descr'] .
				"' AND network = '" . $_POST['network'] . "'";
			//$description = $_POST['descr'];
			//$network = $_POST['network'];
			//global_session('description');
			//global_session('network');
		}
		else
		{
			$query .= "network = '" . $_POST['network'] . "'";
			//$network = $_POST['network'];
			//global_session('network');
		}
		
	}

	// build a query for just a size search
	if ( empty($_POST["descr"]) && empty($_POST["network"]) )
	{
		$query .= "size = '" . $_POST['size_dropdown'] . "'";
		//$size = $_POST['size_dropdown'];
		//global_session('size');
	}

	$query .= " AND (assigned = 1 OR assigned = 2) ORDER BY network";

	// store the query for later use
	//global_session('query');
	global $query;
	
	// use the browse3() function
	browse3( $scriptName, $connection, $offset, $query, $header );

/*
	// run the query on the connection
	if (!($result = @ mysql_query ($query, $connection)))
		showerror();

	// display the results
	displayIPtable($result);

*/

	// choose to browse more or go to main menu
	echo	'<form name="browse_options" method="post" action="search_unavailable.php"><p>' .
		'<input type="button" value="Browse More Networks" 
			onclick="window.location.href=\'search_unavailable.php\'"><p>' .
		'<input type="button" value="Main Menu" 
			onclick="window.location.href=\'interface.php\'" ><br><p>';
	echo	'</form><p>';

	// close the connection
	if (!(mysql_close($connection)))
		showerror();
}
else
{
	echo	'Search networks by description, network, or size. <br><p>';

	//~~~~~~~~~~~~~~~  CREATE FORM TO SUBMIT DESIRED NETWORK DESCRIPTION OR NETWORK  ~~~~~~~~~~~~~~~~~~~~~~~
	echo	'<form name="search_unavailable" method="post" action="search_unavailable.php">
			Description: ' .
		'<input type="text" name="descr" value="" size="55"><br>' .
		'Network: <input type="text" name="network" value="" size="35"><br><p>' .
		'Size: <select name="size_dropdown"><br>' .
					'<option value="">Choose one</option>' .
					'<option value="2">2</option>' .
					'<option value="6">6</option>' .
					'<option value="14">14</option>' .
					'<option value="30">30</option>' .
					'<option value="62">62</option>' .
					'<option value="126">126</option>' .
					'<option value="254">254</option>' .
					'<option value="1022">1022</option>' .
					'<option value="65534">65534</option>' .
				'</select><p>' .

		'<input type="submit" name="submit_all" value="Submit"><p>';
	echo	'</form><p>';
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}

?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

That is because you are trying to use the query when it's never even been created. It's impossible for anything that exists in one part of an if-else series to exist in another part of it because only ONE condition of the if-else series may be met, ignoring all of the rest. You need to rethink your program flow.
sickly_man
Forum Newbie
Posts: 2
Joined: Mon Jul 23, 2007 10:59 am

Post by sickly_man »

i already figured this out, but thanks anyways.
Post Reply