943,983 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1130
  • PHP RSS
Jul 23rd, 2007
0

storing a query for later use

Expand Post »
good morning everyone. i have pretty much correctly implemented a browsing function to look at table information 3 rows at a time. here's the problem: my front end builds the search query from user-entered information, so when i hit the "Next" button to see the next 3 rows that fit the query, that user information is no longer there, and the query is built with no information to go on. so when i hit "Next" it just says "No rows found matching your criteria." I know that I somehow have to save the original query or query information so the function can use it over and over. I am not sure how to do this...it seems complicated to me. here is my code....any help is greatly appreciated. THANKS!!!!!!!!!!!!!!!

also let me know if you wanna see th4e browse3() function code.

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. include "iprange.inc";
  4. include "error.inc";
  5. include "paths.inc";
  6. include "functions2.php";
  7.  
  8. if ( !empty($_POST["descr"]) || !empty($_POST["network"]) || !empty($_POST["size_dropdown"]) || !empty($_GET["offset"]) )
  9. {
  10. // initialize browse3() params
  11. $scriptName = "search_unavailable.php";
  12.  
  13. // set $offset to zero if not previously set
  14. if ( empty($offset) )
  15. $offset = 0;
  16.  
  17. $offset = $_GET['offset'];
  18.  
  19. // HTML <TABLE> column headers
  20. $header[0]["header"] = "Id";
  21. $header[1]["header"] = "Network";
  22. $header[2]["header"] = "Mask";
  23. $header[3]["header"] = "Size";
  24. $header[4]["header"] = "Type";
  25. $header[5]["header"] = "Router Info";
  26. $header[6]["header"] = "Description";
  27. $header[7]["header"] = "Member Id";
  28. $header[8]["header"] = "User";
  29. $header[9]["header"] = "Assigned";
  30.  
  31. // query attributes to display in <TABLE> columns
  32. $header[0]["attrib"] = "id";
  33. $header[1]["attrib"] = "network";
  34. $header[2]["attrib"] = "mask";
  35. $header[3]["attrib"] = "size";
  36. $header[4]["attrib"] = "type";
  37. $header[5]["attrib"] = "router_info";
  38. $header[6]["attrib"] = "description";
  39. $header[7]["attrib"] = "member_id";
  40. $header[8]["attrib"] = "user";
  41. $header[9]["attrib"] = "assigned";
  42.  
  43. // (1) Open the database connection
  44. $connection = mysql_connect($hostName,$username,$password);
  45.  
  46. // (2) Select the database
  47. mysql_select_db($databaseName, $connection); //
  48.  
  49.  
  50. // start beginning of query to build on
  51. $query = "SELECT * FROM subnets WHERE ";
  52.  
  53. // build a query for just a description search
  54. if ( !empty($_POST["descr"]) && empty($_POST["network"]) )
  55. {
  56. $query .= "description = '" . $_POST['descr'] . "'";
  57. }
  58.  
  59. // build a query for just a network search
  60. if ( !empty($_POST["network"]) )
  61. {
  62. // build a query for a search on description AND network
  63. if ( !empty($_POST["description"]) )
  64. {
  65. $query .= "description = '" . $_POST['descr'] .
  66. "' AND network = '" . $_POST['network'] . "'";
  67. }
  68. else
  69. {
  70. $query .= "network = '" . $_POST['network'] . "'";
  71. }
  72.  
  73. }
  74.  
  75. // build a query for just a size search
  76. if ( empty($_POST["descr"]) && empty($_POST["network"]) )
  77. $query .= "size = '" . $_POST['size_dropdown'] . "'";
  78.  
  79. $query .= " AND (assigned = 1 OR assigned = 2) ORDER BY network";
  80.  
  81. // use the browse3() function
  82. browse3( $scriptName, $connection, $offset, $query, $header );
  83.  
  84. /*
  85. // run the query on the connection
  86. if (!($result = @ mysql_query ($query, $connection)))
  87. showerror();
  88.  
  89. // display the results
  90. displayIPtable($result);
  91.  
  92. */
  93.  
  94. // choose to browse more or go to main menu
  95. echo '<form name="browse_options" method="post" action="search_unavailable.php"><p>' .
  96. '<input type="button" value="Browse More Networks"
  97. onclick="window.location.href=\'search_unavailable.php\'"><p>' .
  98. '<input type="button" value="Main Menu"
  99. onclick="window.location.href=\'interface.php\'" ><br><p>';
  100. echo '</form><p>';
  101.  
  102.  
  103. // close the connection
  104. if (!(mysql_close($connection)))
  105. showerror();
  106. }
  107. else
  108. {
  109. echo 'Search networks by description, network, or size. <br><p>';
  110.  
  111. //~~~~~~~~~~~~~~~ CREATE FORM TO SUBMIT DESIRED NETWORK DESCRIPTION OR NETWORK ~~~~~~~~~~~~~~~~~~~~~~~
  112. echo '<form name="search_unavailable" method="post" action="search_unavailable.php">
  113. Description: ' .
  114. '<input type="text" name="descr" value="" size="55"><br>' .
  115. 'Network: <input type="text" name="network" value="" size="35"><br><p>' .
  116. 'Size: <select name="size_dropdown"><br>' .
  117. '<option value="">Choose one</option>' .
  118. '<option value="2">2</option>' .
  119. '<option value="6">6</option>' .
  120. '<option value="14">14</option>' .
  121. '<option value="30">30</option>' .
  122. '<option value="62">62</option>' .
  123. '<option value="126">126</option>' .
  124. '<option value="254">254</option>' .
  125. '<option value="1022">1022</option>' .
  126. '<option value="65534">65534</option>' .
  127. '</select><p>' .
  128.  
  129. '<input type="submit" name="submit_all" value="Submit"><p>';
  130. echo '</form><p>';
  131. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. }
  133.  
  134. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sickly_man is offline Offline
42 posts
since Jun 2007
Jul 23rd, 2007
0

Re: storing a query for later use

You will need to forward those values along in your GET requests (the Next/Previous links) or you can store them in $_SESSION[] to have them persist. Session is probably the easiest bet.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 23rd, 2007
0

Re: storing a query for later use

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
You will need to forward those values along in your GET requests (the Next/Previous links) or you can store them in $_SESSION[] to have them persist. Session is probably the easiest bet.
ok. can i store the entire query in $_SESSION[]? how can i store those values in $_SESSION[]? im a big time newbie so i relaly appreciate the help.
Reputation Points: 10
Solved Threads: 0
Light Poster
sickly_man is offline Offline
42 posts
since Jun 2007
Jul 23rd, 2007
0

Re: storing a query for later use

Small sample on storing a value in session:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3. echo 'Sessions activated.<br />';
  4. $_SESSION['version'] = phpversion();
  5. echo 'Session data written.<br />';
  6. echo "Session data read: {$_SESSION['version']}.";
  7. ?>
Tutorial on sessions can be found here:
http://www.goodphptutorials.com/track/66

I don't think you will want to store the query itself in the session. That could be a lot of data for the server to serialize and deserialize. Just store enough info to requery as needed. You may want to up your result per page to 10 if the query is somewhat expensive to perform.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 23rd, 2007
0

Re: storing a query for later use

ok thanks a lot i've got it now.
Reputation Points: 10
Solved Threads: 0
Light Poster
sickly_man is offline Offline
42 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: view word/excel
Next Thread in PHP Forum Timeline: is it possible to store a query?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC