Link to home
Start Free TrialLog in
Avatar of h3rm1t9536
h3rm1t9536

asked on

handle form page is not storing the session varibles

Hi There!

I have two pages search.php and searchresults.php - searchresults.php handles the form sent on search.php

I have a few functions on the searchresults.php  - I have a search box that highlights text that matches the word searched for. The second function was created to break up the data being displayed into pages (ie previous next ...)

the problem I am having is that when the form is sent searchresults.php displays correctly at first but as soon as a word is searched for in the search box the page refreshes and looses the initial vaules sent from the form. This happens as well when I select ta page number.

I tried to incorporate session variables for all form variables on the searchresults page but this did not solve the problem.

It would be great if some one could help!

Thanks so much,
Chelsea
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

If you want to use the session, you will need session_start() at the top of every script.

If you want to get some foundation in how PHP works, consider getting this book and working through the examples.  It will put you way ahead, I promise.
http://www.sitepoint.com/books/phpmysql4/

If you need to understand how PHP and HTML forms interact, have a look at this
http://us2.php.net/manual/en/tutorial.forms.php

HTH, and if you want to post some code and test data so we can see what might be going on, that would be great. ~Ray
Ray's right, possibly you forgot to place session_start() at the very beginning of the script.
Yep, the missing session_start() or what is worse, the session_start() inside a conditional control structure that depends on external input... Well, that trips a lot of us up.  In a perfect world the $_SESSION array would be immutable before session_start() was called, and an attempt to access it would throw a fatal error.  However PHP does not live in a perfect world.  You can blithely put stuff into $_SESSION and PHP will silently throw it all away if you did not execute the session_start().  PHP will also throw it away at the moment when you execute session_start().

Also, session_start() will try to set a cookie.  Cookies are headers, and all headers must come before and be complete before there is any browser output (even invisible whitespace).  So for all these reasons it's a good idea to call session_start() at the top of the script, right after error_reporting(E_ALL).  Or use output buffering if you cannot get the logic right.

Here is a little script that shows PHP does not live in a perfect world!
<?php // RAY_session_error.php
error_reporting(E_ALL);


// DEMONSTRATE HOW NOT TO USE SESSIONS


// THIS WILL CAPTURE THE OUTPUT BUFFERS
ob_start();

// SET SOMETHING IN THE UNSTARTED SESSION ARRAY - NO ERROR!
$_SESSION["Ray"] = "Paseur";

// SHOW THE CONTENTS OF THE UNSTARTED SESSION ARRAY
echo "<br/>THE SESSION BEFORE SESSION_START() ";
var_dump($_SESSION);

// NOW START THE SESSION
session_start();

// SHOW THE CONTENTS OF THE STARTED SESSION ARRAY
echo "<br/>THE SESSION AFTER SESSION_START() ";
var_dump($_SESSION);

Open in new window

Avatar of h3rm1t9536
h3rm1t9536

ASKER

Thanks so much for all of your help Ray and Roads Roads! Greatly appreciated!

I had included session_start() previously though...

Here's my code for search.php:
 
<?php $_pagetitle="Search"; include('inc/header.inc.php'); 
	
	//Function to create drop down lists in forms
	include('inc/dropdown.inc.php');

	//Function to place all tables into php arrays	
	include('php_functions/getAllValues.php');
	
	//Calling the getAllValues() for each table
	
	//$account = array();
	//$account = getAllValues('AccountID', 'AccountValue', 'account');

	$country = array();
	$country = getAllValues('CountryID', 'CountryValue', 'country');

	$cover_format_size = array();
	$cover_format_size = getAllValues('CoverFormatSizeID', 'CoverFormatSizeValue', 'cover_format_size');

	$cover_stock_finish = array();
	$cover_stock_finish = getAllValues('CoverStockFinishID', 'CoverStockFinishValue', 'cover_stock_finish');

	$distribution_channel = array();
	$distribution_channel = getAllValues('DistributionChannelID', 'DistributionChannelValue', 'distribution_channel');

	//$distribution_channel_details = array();
	//$distribution_channel_details = getAllValues('DistributionChannelDetailsID', 'DistributionChannelDetailsValue', 'distribution_channel_details');

	$industry_sector = array();
	$industry_sector = getAllValues('IndustrySectorID', 'IndustrySectorValue', 'industry_sector');

	$industry_sub_sector = array();
	$industry_sub_sector = getAllValues('IndustrySubSectorID', 'IndustrySubSectorValue', 'industry_sub_sector');

	$insert_size = array();
	$insert_size = getAllValues('InsertSizeID', 'InsertSizeValue', 'insert_size');

	$insert_stock = array();
	$insert_stock = getAllValues('InsertStockID', 'InsertStockValue', 'insert_stock');
	
	$industry_sector = array();
	$industry_sector = getAllValues('IndustrySectorID', 'IndustrySectorValue', 'industry_sector');
	
	$industry_sub_sector = array();
	$industry_sub_sector = getAllValues('IndustrySubSectorID', 'IndustrySubSectorValue', 'industry_sub_sector');

	//$local_order_number = array();
	//$local_order_number = getAllValues('LocalOrderNumberID', 'LocalOrderNumberValue', 'local_order_number');

	//$orders = array();
	//$orders = getAllValues('OrderID', 'OrderName', 'orders');

	$strategic_application = array();
	$strategic_application = getAllValues('StrategicApplicationID', 'StrategicApplicationValue', 'strategic_application');
	?>
	<!--END OF PLACEING ALL TABLES INTO ARRAYS -->

	<!--CONVERT PHP ARRAY TO JAVASCRIPT ARRAY FOR AUTO COMPLETE -->

	<script type="text/javascript">
		
		//var account = new Array();
		var country = new Array();
		var cover_format_size = new Array();
		var cover_stock_finish = new Array();
		var insert_size = new Array();
		var insert_stock = new Array();
		var industry_sector = new Array();
		var industry_sub_sector = new Array();
		//var local_order_number = new Array();
		//var orders = new Array();
		
		<?php
			//foreach ($account as $id=>$value)
			//echo "account[$id] = '$value';\n";
			
			foreach ($country as $id=>$value)
			echo "country[$id] = '$value';\n";
			
			foreach ($cover_format_size as $id=>$value)
			echo "cover_format_size[$id] = '$value';\n";
			
			foreach ($cover_stock_finish as $id=>$value)
			echo "cover_stock_finish[$id] = '$value';\n";
			
			foreach ($insert_size as $id=>$value)
			echo "insert_size[$id] = '$value';\n";
			
			foreach ($insert_stock as $id=>$value)
			echo "insert_stock[$id] = '$value';\n";
			
			foreach ($industry_sector as $id=>$value)
			echo "industry_sector[$id] = '$value';\n";
			
			foreach ($industry_sub_sector as $id=>$value)
			echo "industry_sub_sector[$id] = '$value';\n";
			
			//foreach ($local_order_number as $id=>$value)
			//echo "local_order_number[$id] = '$value';\n";
			
			//foreach ($orders as $id=>$value)
			//echo "orders[$id] = '$value';\n";
		?>
	</script>
	<!-- END OF CONVERTING PHP ARRAY TO JAVASCRIPT ARRAY FOR AUTO COMPLETE -->
	
	<!-- DISPLAYING FORM -->

	<table>
		<form action="searchresults.php" method="post" autocomplete="off">
			<tr>
				<td width="310">
					<h2>Contact Sales</h2><br/>
					Please contact us today if you have<br/>a general enquiry or if you would like<br/>more information relating to a spesific<br/>order found on the ISD Portal.<br/><br/>
					We have a dedicated sales team<br/>who will be more than happy to<br/>assist in any enquiries you have.<br/><br/><br/>
					<br/><br/>                  
				</td>
				
				<td width="310">
					<h3>
						<!--<div class="bluetext">Account</div><input type='text' style='color:#1E2C3B' name='AccountValue' id='account' value='' size='40' placeholder='Enter Account Name'/><br/><br/>-->
						Country of Sale<input type='text' style='color:#1E2C3B' name='CountryValue' id='country' value='' size='40' placeholder='Enter Country'/><br/><br/>				
						<!--<div class="bluetext">Local Order Number</div><input type='text' name='LocalOrderNumberValue' id='local_order_number' value='' size='40' placeholder='Enter Local Order Number'/><br/><br/>-->
						<!--<div class="bluetext">Orders</div><input type='text' name='OrdersName' id='orders' value='' size='40' placeholder='Enter Order Name'/><br/><br/>-->
						Order Date Range
							<input type="text" style='color:#1E2C3B' id="datepicker1" size="18" name="OrdersDateFrom" placeholder='Select Date From'/>   
							<input type="text" style='color:#1E2C3B' id="datepicker2" size="18" name="OrdersDateTo" placeholder='Select Date To'/><br><br>
						Industry Sector<input type='text' style='color:#1E2C3B' name='IndustrySectorValue' id='industry_sector' value='' size='40' placeholder='Enter Industry Sector'/><br/><br/>
						Industry Sub-Sector<input type='text' style='color:#1E2C3B' name='IndustrySubSectorValue' id='industry_sub_sector' value='' size='40' placeholder='Enter Industry Sub-Sector'/><br/><br/>
						Insert Stock<input type='text' style='color:#1E2C3B' name='InsertStockValue' id='insert_stock' value='' size='40' placeholder='Enter Insert Stock'/><br/><br/>
					</h3>
				</td>
				<td width="230">
					<h3>Insert Size<input type='text' style='color:#1E2C3B' name='InsertSizeValue' id='insert_size' value='' size='40' placeholder='Enter Insert Size'/><br/><br/>
						Cover Format Size<input type='text' style='color:#1E2C3B' name='CoverFormatSizeValue' id='cover_format_size' value='' size='40' placeholder='Enter Cover Format and Size'/><br/><br/>
						Cover Stock Finish<input type='text' style='color:#1E2C3B' name='CoverStockFinishValue' id='cover_stock_finish' value='' size='40' placeholder='Enter Cover Stock and Finish'/><br/><br/>
						Distribution Channel<?php createDropdown($distribution_channel, 'DistributionChannelValue'); ?><br/><br/>
						Strategic Application</h3><?php createDropdown($strategic_application, 'StrategicApplicationValue'); ?><br/><br/>
					</h3>
					<p><br/><input type="submit" name="submit" style="float: right;" value="       Search      " size="30"/></p><br/>
					<input type="hidden" name="submitted" value="TRUE" />
				</td>
			</tr>
		</form>
	</table>
	<!-- END OF DISPLAYING FORM -->
	
	<!--AUTO COMPLETE-->
	<script >
		//var obj1 = new actb(document.getElementById('account'), account);
		var obj2 = new actb(document.getElementById('country'), country);
		var obj3 = new actb(document.getElementById('cover_format_size'), cover_format_size);
		var obj4 = new actb(document.getElementById('cover_stock_finish'), cover_stock_finish);
		var obj5 = new actb(document.getElementById('insert_size'), insert_size);
		var obj6 = new actb(document.getElementById('insert_stock'), insert_stock);
		var obj7 = new actb(document.getElementById('industry_sector'), industry_sector);
		var obj8 = new actb(document.getElementById('industry_sub_sector'), industry_sub_sector);
		//var obj9 = new actb(document.getElementById('local_order_number'), local_order_number);
		//var obj10 = new actb(document.getElementById('orders'), orders);
	</script>
	<!--END OF AUTO COMPLETE-->

<?php include('inc/footer.inc'); ?>

Open in new window


Here's my code for searchresults.php (the page that handles the form):
 
<?php
	ob_start();
	session_start();

	//get values from form
	//$AccountValue = $_POST['AccountValue'];
	$_SESSION['AccountValue'] = $_POST['AccountValue'];
	//$CountryValue = $_POST['CountryValue'];
	$_SESSION['CountryValue'] = $_POST['CountryValue'];
	//$LocalOrderNumberValue = $_POST['LocalOrderNumberValue'];
	$_SESSION['LocalOrderNumberValue'] = $_POST['LocalOrderNumberValue'];
	//$OrdersName = $_POST['OrdersName'];
	$_SESSION['OrdersName'] = $_POST['OrdersName'];
	//$OrdersDateFrom = $_POST['OrdersDateFrom'];
	$_SESSION['OrdersDateFrom'] = $_POST['OrdersDateFrom'];
	//$OrdersDateTo = $_POST['OrdersDateTo'];
	$_SESSION['OrdersDateTo'] = $_POST['OrdersDateTo'];
	//$IndustrySectorValue = $_POST['IndustrySectorValue'];
	$_SESSION['IndustrySectorValue'] = $_POST['IndustrySectorValue'];
	//$IndustrySubSectorValue = $_POST['IndustrySubSectorValue'];
	$_SESSION['IndustrySubSectorValue'] = $_POST['IndustrySubSectorValue'];
	//$InsertStockValue = $_POST['InsertStockValue'];
	$_SESSION['InsertStockValue'] = $_POST['InsertStockValue'];
	//$InsertSizeValue = $_POST['InsertSizeValue'];
	$_SESSION['InsertSizeValue'] = $_POST['InsertSizeValue'];
	//$CoverFormatSizeValue = $_POST['CoverFormatSizeValue'];
	$_SESSION['CoverFormatSizeValue'] = $_POST['CoverFormatSizeValue'];
	//$CoverStockFinishValue = $_POST['CoverStockFinishValue'];
	$_SESSION['CoverStockFinishValue'] = $_POST['CoverStockFinishValue'];
	//$DistributionChannelValue = $_POST['DistributionChannelValue'];
	$_SESSION['DistributionChannelValue'] = $_POST['DistributionChannelValue'];
	//$StrategicApplicationValue = $_POST['StrategicApplicationValue'];
	$_SESSION['StrategicApplicationValue'] = $_POST['StrategicApplicationValue'];
	
	//Display th results from the search.php 
	$_pagetitle="Search Results"; 
	include('inc/headersr.inc.php');
	
	$conditions = FALSE;
	$conditions = "orders.ContentID = content.ContentID
				AND orders.CountryID = country.CountryID
				AND orders.CoverFormatSizeID = cover_format_size.CoverFormatSizeID
				AND orders.CoverStockFinishID = cover_stock_finish.CoverStockFinishID
				AND orders.DistributionChannelID = distribution_channel.DistributionChannelID
				AND orders.DistributionChannelDetailsID = distribution_channel_details.DistributionChannelDetailsID
				AND orders.IndustrySectorID = industry_sector.IndustrySectorID
				AND orders.IndustrySubSectorID = industry_sub_sector.IndustrySubSectorID
				AND orders.InsertSizeID = insert_size.InsertSizeID
				AND orders.InsertStockID = insert_stock.InsertStockID
				AND orders.LocalOrderNumberID = local_order_number.LocalOrderNumberID
				AND orders.StrategicApplicationID = strategic_application.StrategicApplicationID ";
	
	//find out with varibles are set
	if($_SESSION['AccountValue'])
	{
		$AccountValue = $_SESSION['AccountValue'];
		$conditions .= "AND AccountValue = '$AccountValue' ";
	}
	if($_SESSION['CountryValue'])
	{
		$CountryValue = $_SESSION['CountryValue'];
		$conditions .= "AND CountryValue = '$CountryValue' ";
	}
	if($_SESSION['LocalOrderNumberValue'])
	{
		$LocalOrderNumberValue = $_SESSION['LocalOrderNumberValue'];
		$conditions .= "AND LocalOrderNumberValue = '$LocalOrderNumberValue' ";
	}
	if($_SESSION['OrdersName'])
	{
		$OrdersName = $_SESSION['OrdersName'];
		$conditions .= "AND OrdersName = '$OrdersName' ";
	}
	if($_SESSION['OrdersDateFrom'])
	{
		$OrdersDateFrom = $_SESSION['OrdersName'];
		$conditions .= "AND OrdersDate >= '$OrdersDateFrom' ";
	}
	if($_SESSION['OrdersDateTo'])
	{
		$OrdersDateTo = $_SESSION['OrdersDateTo'];
		$conditions .= "AND OrdersDate <= '$OrdersDateTo' ";
	}
	if($_SESSION['IndustrySectorValue'])
	{
		$IndustrySectorValue = $_SESSION['IndustrySectorValue'];
		$conditions .= "AND IndustrySectorValue = '$IndustrySectorValue' ";
	}
	if($_SESSION['IndustrySubSectorValue'])
	{
		$IndustrySubSectorValue = $_SESSION['IndustrySubSectorValue'];
		$conditions .= "AND IndustrySubSectorValue = '$IndustrySubSectorValue' ";
	}
	if($_SESSION['InsertStockValue'])
	{
		$InsertStockValue = $_SESSION['InsertStockValue'];
		$conditions .= "AND InsertStockValue = '$InsertStockValue' ";
	}
	if($_SESSION['InsertSizeValue'])
	{
		$InsertSizeValue = $_SESSION['InsertSizeValue'];
		$conditions .= "AND InsertSizeValue = '$InsertSizeValue' ";
	}
	if($_SESSION['CoverFormatSizeValue'])
	{
		$CoverFormatSizeValue = $_SESSION['CoverFormatSizeValue'];
		$conditions .= "AND CoverFormatSizeValue = '$CoverFormatSizeValue' ";
	}
	if($_SESSION['CoverStockFinishValue'])
	{
		$CoverStockFinishValue = $_SESSION['CoverStockFinishValue'];
		$conditions .= "AND CoverStockFinishValue = '$CoverStockFinishValue' ";
	}
	if($_SESSION['DistributionChannelValue'])
	{
		$DistributionChannelValue = $_SESSION['DistributionChannelValue'];
		$conditions .= "AND DistributionChannelValue = '$DistributionChannelValue' ";
	}
	if($_SESSION['StrategicApplicationValue'])
	{
		$StrategicApplicationValue = $_SESSION['StrategicApplicationValue'];
		$conditions .= "AND StrategicApplicationValue = '$StrategicApplicationValue' ";
	}
	echo "country";
	echo $_SESSION['CountryValue'];
	echo "country varible no session below";
	echo $CountryValue;
	require ('C:xampp/mysqli_connect.php');
	
	// Count the number of records
	$q = "SELECT COUNT(OrderID) 
	FROM 
		content,
		country,
		cover_format_size,
		cover_stock_finish,
		distribution_channel,
		distribution_channel_details,
		industry_sector,
		industry_sub_sector,
		insert_size,
		insert_stock,
		local_order_number,
		orders,
		strategic_application
	WHERE 
		$conditions";
		
		$r = @mysqli_query ($dbc, $q);
		$num_rows = mysqli_fetch_row ($r);
		$_SESSION['num_rows'] = $num_rows[0];
	//No rows were returned then display error message and redirect user to search.php
	if($_SESSION['num_rows'] == 0)
	{
		mysqli_free_result ($r);
		mysqli_close($dbc);
		
		echo "<script language='javascript'>";
		echo "alert('No rows were returned')";
		echo "</script>";
		
		//redirect to search.php and show error message
		header("Location: http://localhost/isd/search.php/");
		exit;
	}
	//else get the values returned and display them
	else
	{
		include('inc/paginator.class.php');
		
		//Create a new paginator object and initializes the default values behind the scenes.
		$pages = new Paginator;  
		//Uses the query to get the total number of records and assigns it to our paginator's items_total property. 
		//$num_rows is an array containing the result of our count query (you could also use PHP's mysql_num_rows function to retrieve a similar count if you like).
		$pages->items_total = $_SESSION['num_rows']; 
		//Tells the paginator the number of page links to display. This number should be odd & greater than 3 so that the display is symmetrical.
		$pages->mid_range = 5;  
		//Tells the paginator to get to work and paginate
		$pages->paginate();  
		
		// Determine the sorting order:
		$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'OrdersDate';
		include('inc/sort.php');
		
		// Make the query:
		$sql = "
			SELECT 
				OrdersDate, 
				LocalOrderNumberValue, 
				OrdersName, 
				OrdersQuantity, 
				InsertSizeValue, 
				InsertStockValue, 
				CoverStockFinishValue, 
				CoverFormatSizeValue, 
				ContentValue, 
				StrategicApplicationValue, 
				DistributionChannelValue, 
				DistributionChannelDetailsValue,  
				CountryValue, 
				IndustrySectorValue, 
				IndustrySubSectorValue
			FROM 
				content,
				country,
				cover_format_size,
				cover_stock_finish,
				distribution_channel,
				distribution_channel_details,
				industry_sector,
				industry_sub_sector,
				insert_size,
				insert_stock,
				local_order_number,
				orders,
				strategic_application
			WHERE 
				$conditions
			ORDER BY 
				$order_by 
			$pages->limit";
			
			$_SESSION['sql'] = $sql;
				//$start, $display		
		$resultToDisplay = @mysqli_query ($dbc, $_SESSION['sql']); // Run the query.

	// ****RESULTS DISPLAYED IN TABLE*****
	
	echo '<table id="displayresults">';
	  
			// Table header 
				echo '<thead>  
					<tr>  
						<th scope="col" id="...">Date</th>
						<th scope="col" id="...">Local Order Number</th>
						<th scope="col" id="...">Orders Name</th>
						<th scope="col" id="...">Quantity</th>
						<th scope="col" id="...">Insert Size</th>
						<th scope="col" id="...">Insert Stock</th>
						<th scope="col" id="...">Cover Format & Size</th>
						<th scope="col" id="...">Cover Stock & Finish</th>
						<th scope="col" id="...">Content</th>
						<th scope="col" id="...">Strategic Application</th>
						<th scope="col" id="...">Distribution Channel</th>
						<th scope="col" id="...">Distribution Channel Details</th>
						';//<th scope="col" id="...">Account</th>
						echo '<th scope="col" id="...">Country</th>
						<th scope="col" id="...">Industry Sector</th>
						<th scope="col" id="...">Industry Sub Sector</th>
					</tr>  
				</thead>';  
		  
			//Table footer   
				echo '<tfoot>  
					<tr>  
						  <td colspan="14">...</td>  
					</tr>  
				</tfoot>';
		  
			//Table body
				echo '<tbody>'; 

			// Fetch and print all the records....
			$bg = 'odd'; 
			while ($row = mysqli_fetch_array($resultToDisplay, MYSQLI_ASSOC)) 
			{
				$bg = ($bg=='odd' ? 'even' : 'odd');
				//you can take out table. for all the values below...
				echo '
					<tr class="' . $bg . '">
						<td class="date">' . $row['OrdersDate'] . '</td>';
						//while we're at we will store the data in comma separated values (csv) format in the csv_output variable for later use.
						$csv_output .= $row['OrdersDate'] . ", ";
				echo '	<td class="local_order_number">' . $row['LocalOrderNumberValue'] . '</td>';
						//repeat for all remaining fields or columns we have headings for...
						$csv_output .= $row['LocalOrderNumberValue'] . ", ";
				echo '	<td class="opportunity">' . $row['OrdersName'] . '</td>';
						$csv_output .= $row['OrdersName'] . ", ";
				echo '	<td class="quantity">' . $row['OrdersQuantity'] . '</td>';
						$csv_output .= $row['OrdersQuantity'] . ", ";
				echo '	<td class="insert_size">' . $row['InsertSizeValue'] . '</td>';
						$csv_output .= $row['InsertSizeValue'] . ", ";
				echo '	<td class="insert_stock">' . $row['InsertStockValue'] . '</td>';
						$csv_output .= $row['InsertStockValue'] . ", ";
				echo '	<td class="cover_format_size">' . $row['CoverFormatSizeValue'] . '</td>';
						$csv_output .= $row['CoverFormatSizeValue'] . ", ";
				echo '	<td class="cover_stock_finish">' . $row['CoverStockFinishValue'] . '</td>';
						$csv_output .= $row['CoverStockFinishValue'] . ", ";
				echo '	<td class="strategic_application">' . $row['StrategicApplicationValue'] . '</td>';
						$csv_output .= $row['StrategicApplicationValue'] . ", ";
				echo '	<td class="distribution_channel">' . $row['DistributionChannelValue'] . '</td>';
						$csv_output .= $row['DistributionChannelValue'] . ", ";
				echo '	<td class="distribution_channel_details">' . $row['DistributionChannelDetailsValue'] . '</td>';
						$csv_output .= $row['DistributionChannelDetailsValue'] . ", ";
						//<td class="account">' . $row['AccountValue'] . '</td>
						//$csv_output .= $row['AccountValue'] . ", ";
				echo '	<td class="country">' . $row['CountryValue'] . '</td>';
						$csv_output .= $row['CountryValue'] . ", ";
				echo '	<td class="industry_sector">' . $row['IndustrySectorValue'] . '</td>';
						$csv_output .= $row['IndustrySectorValue'] . ", ";
				echo '	<td class="industry_sub_sector">' . $row['IndustrySubSectorValue'] . '</td>';
						//ensure the last column entry starts a new line
						$csv_output .= $row['IndustrySubSectorValue'] . "\n";
				echo '	</tr>';
			} // End of WHILE loop.
			echo '</tbody></table>';
			
			//Now that we've created such a nice heading for our html table, lets create a heading for our csv table
			$csv_hdr = "Date, Local Order Number, Order Name, Quantity, Insert Size, Insert Stock, Cover Format & Size, Cover Stock & Finish, Content, Strategic Application, Distribution Channel, Distribution Channel Details, Country, Industry Sector, Industry Subsector";
			
			/*
			Here is the important part. we've got the 2 variables (csv_hdr & csv_output) to create our csv file, but we can't do it in this file.
			Why? Because the header for this file has already been sent and will show up in our csv file if we generate it on this page. We don't
			want any html header in our csv file, so we've got to post our 2 variables to another php page (export.php) on which we generate our csv
			file.

			Here's the code for a form & button that'll post our 2 variables as hidden _POST to export.php.
			*/
		?>
			<br />
			<center>
			<form name="export" action="export.php" method="post">
				<input type="submit" value="Export table to CSV">
				<input type="hidden" value="<? echo $csv_hdr; ?>" name="csv_hdr">
				<input type="hidden" value="<? echo $csv_output; ?>" name="csv_output">
			</form>
			</center>
			
		<?php	
			//Displays the page numbers
			echo $pages->display_pages(); 
			
			//Allow users the option of changing the number of items per page or jumping to a specific page
			echo $pages->display_items_per_page() . "<br>" . $pages->display_jump_menu();
	
		mysqli_free_result ($resultToDisplay);
		mysqli_free_result ($r);
		mysqli_close($dbc);
	}

?>
</div>
<?php include('inc/footer.inc'); ?>

Open in new window


The result to the end user is basically suppose to be a set number of returned values as the search allows them to refine the result - but unfortunately when either the search box or page number options are used then the searchresults.php returns all possible results regardless of what the user entered in search.php

Thanks so much again for helping me out Ray and Roads Roads!
any ideas why its not returning the refined result added from the form input?
Your searchresults.php contain the lines:

                  ORDER BY
                        $order_by
                  $pages->limit

can you output your sql query after that so you can see if the query limits the result ?
seems like there is order by limit instead of limit $pages->limit
This is line 149 from the second code snippet at #36814924

$r = @mysqli_query ($dbc, $q);

There are three things you must do.  First you must remove the @ from the function name.  The @ suppresses error messages.  You want to see ALL of the error messages at this point in the application life cycle.

Next, you want to raise the level of error reporting to error_reporting(E_ALL); so you can see if the script accidentally relied on an undefined variable.

Third, you want to test the $r variable to see if the mysqli_query() function worked at all.  Please see the return values on the man page here.
http://php.net/manual/en/mysqli.query.php

Example #1 "procedural style" shows how to test the return value and visualize the error message if the query failed.

Armed with a little debugging information it should be easy to know whether the query worked.  A query failure is what I would look for first.
Hi Roads Roads - I printed the sql query and it was correct it printed....
 
SELECT OrdersDate, LocalOrderNumberValue, OrdersName, OrdersQuantity, InsertSizeValue, InsertStockValue, CoverStockFinishValue, CoverFormatSizeValue, ContentValue, StrategicApplicationValue, DistributionChannelValue, DistributionChannelDetailsValue, CountryValue, IndustrySectorValue, IndustrySubSectorValue FROM content, country, cover_format_size, cover_stock_finish, distribution_channel, distribution_channel_details, industry_sector, industry_sub_sector, insert_size, insert_stock, local_order_number, orders, strategic_application WHERE orders.ContentID = content.ContentID AND orders.CountryID = country.CountryID AND orders.CoverFormatSizeID = cover_format_size.CoverFormatSizeID AND orders.CoverStockFinishID = cover_stock_finish.CoverStockFinishID AND orders.DistributionChannelID = distribution_channel.DistributionChannelID AND orders.DistributionChannelDetailsID = distribution_channel_details.DistributionChannelDetailsID AND orders.IndustrySectorID = industry_sector.IndustrySectorID AND orders.IndustrySubSectorID = industry_sub_sector.IndustrySubSectorID AND orders.InsertSizeID = insert_size.InsertSizeID AND orders.InsertStockID = insert_stock.InsertStockID AND orders.LocalOrderNumberID = local_order_number.LocalOrderNumberID AND orders.StrategicApplicationID = strategic_application.StrategicApplicationID AND CountryValue = 'Barbados' LIMIT 0,25

Open in new window


however when I select page 2 the query changes to:
 
SELECT OrdersDate, LocalOrderNumberValue, OrdersName, OrdersQuantity, InsertSizeValue, InsertStockValue, CoverStockFinishValue, CoverFormatSizeValue, ContentValue, StrategicApplicationValue, DistributionChannelValue, DistributionChannelDetailsValue, CountryValue, IndustrySectorValue, IndustrySubSectorValue FROM content, country, cover_format_size, cover_stock_finish, distribution_channel, distribution_channel_details, industry_sector, industry_sub_sector, insert_size, insert_stock, local_order_number, orders, strategic_application WHERE orders.ContentID = content.ContentID AND orders.CountryID = country.CountryID AND orders.CoverFormatSizeID = cover_format_size.CoverFormatSizeID AND orders.CoverStockFinishID = cover_stock_finish.CoverStockFinishID AND orders.DistributionChannelID = distribution_channel.DistributionChannelID AND orders.DistributionChannelDetailsID = distribution_channel_details.DistributionChannelDetailsID AND orders.IndustrySectorID = industry_sector.IndustrySectorID AND orders.IndustrySubSectorID = industry_sub_sector.IndustrySubSectorID AND orders.InsertSizeID = insert_size.InsertSizeID AND orders.InsertStockID = insert_stock.InsertStockID AND orders.LocalOrderNumberID = local_order_number.LocalOrderNumberID AND orders.StrategicApplicationID = strategic_application.StrategicApplicationID LIMIT 50,25

Open in new window


which is incorrect as it is leaving off the form input details where the user selected ... CountryValue = 'Barbados'
but the limit part is correct in the fact that it is displaying 25 results per a page and starts at 25 and ends at 50.

Why is it leaving out the form input?

Hi Ray Paseur

I removed the @ sign from both lines and the error_reporting was always set to (E_ALL)
I tested both the result varibles and both are correct initially however once I select a different page number or a search for a word in the search box the $r returns a query that does not include what the user selected in search.php - the above code shows that it leaves out AND CountryValue = 'Barbados' when the user selects the result to only include Barbados as the country...

Why is the session not storing the value - have I placed the if statements in the incorrect order? Have I left out an if statement? should I include a while loop?

I must have missed out something or done something incorrectly and have no idea what the solution is... saddest thing is I need this working as soon as possible -

any ideas would be greatly appreciated!
p.s. I tried to do a little test on the query by making it static and including the AND CountryValue = 'Barbados' and the searchresults.php returned the correct data even after a different page number was selected and even after a word was search for in the search box. So the problem is identified - basically the search.php sets the additional conditions to be added to the $sql then the searchresults.php opens up correctly and displays the correct result. but then as soon as a function is used the searchresults.php code is run again basically - when it runs again it looks up lines 7 - 33 and stores new values (basically writing over the original values set by the user) therefore all variable that sore the session as overwritten and all of those variables store "" (basically nothing - an empty string)

Some how I need the script to change in a way that will either not overwrite the original session variables or I need to include a while loop where it runs till the user wants to be redirected to another web page

Do you have any suggestions of what would be the best practice solution? Have you ever seen a problem like this before? what was that persons solution to avoid this over writing?

You might want to throw some var_dump() statements into the code to see what variables are getting sent in $_POST and see where they are getting used in $_SESSION or other parts of the script.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial