Jump to content

Using a html form to sort SQL?


lenmat

Recommended Posts

I am a newbi, but I have created a database for reports, and would like the user to be able to search them based off of species.  here is a link with the drop down menu i would like to use, as well as a sample of reports using pagination (I have yet to enter all the reports into sql.)

http://www.thetrophystalkers.com/aaaaa.php

 

Here is the code

 Species: <SELECT NAME="species">
<OPTION VALUE="trout">Trout
<OPTION VALUE="salmon">Salmon
<OPTION VALUE="bass">Bass
<OPTION VALUE="muskies">Muskies
<OPTION VALUE="browntrout">Brown Trout
<OPTION VALUE="biggame">Big Game
<OPTION VALUE="saltwater">Saltwater
<OPTION VALUE="waterfowl">Waterfowl
<OPTION VALUE="pheasants">Pheasants
<OPTION VALUE="icefishing">Ice Fishing
</SELECT>
        
         <input type="submit" name="submit" value="Search!" />
      </form>
      
      <br><br><br>
     <?php
// Connects to your Database
mysql_connect("localhost", "xxxxxxxxx", "xxxxxxxx") or die(mysql_error());
mysql_select_db("xxxxxxxxxxxxx") or die(mysql_error());

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM Reports") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 5;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM Reports ORDER BY id DESC $max") or die(mysql_error());

//This is where you display your query results
while($info = mysql_fetch_array( $data_p ))
{
  echo $info['month'];
  echo $info['year'];
      echo "<b>";   echo "<br />";  
   echo $info['title'];    echo "</b>";echo "<br />"; 
   echo $info['prostaff'];
   echo "<br />";   echo "<br />";
echo "<br />";
   
   echo $info['report'];
  echo "<br />";echo "<br />"; echo "<br />"; echo "<br />"; 
  }
echo "<p>";
echo "<center>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
echo "</center>";
}
?>

 

is this possible and can anyone help me.  thanks

 

Link to comment
https://forums.phpfreaks.com/topic/162846-using-a-html-form-to-sort-sql/
Share on other sites

I am getting it figured out, but now I have this issue...

 

Parse error: syntax error, unexpected ';' in /home/lenmat/public_html/articlestest.php on line 35.  I know that there needs to be a ';' in line 35 and i checked all the  {} and they are good too.  any thoughts?

 

here is the code:

<html><body><form action ="POST" action="" name="myform">
Species: <SELECT NAME="species"> 
<OPTION VALUE="trout">Trout 
<OPTION VALUE="salmon">Salmon 
<OPTION VALUE="bass">Bass 
<OPTION VALUE="muskies">Muskies 
<OPTION VALUE="browntrout">Brown Trout 
<OPTION VALUE="biggame">Big Game 
<OPTION VALUE="saltwater">Saltwater 
<OPTION VALUE="waterfowl">Waterfowl 
<OPTION VALUE="pheasants">Pheasants 
<OPTION VALUE="icefishing">Ice Fishing 
</SELECT> 
         
         <input type="submit" name="submit" value="Search!" /> 
      </form> 
       
      <br><br><br> 
<?php

if (isset($_POST['submit'])) 
$species = $_POST['species'];
// Connects to your Database 
mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxxx") or die(mysql_error()); 
mysql_select_db("xxxxxxxxxxxxxxxxx") or die(mysql_error()); 

//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query('SELECT * FROM Reports WHERE `speciesfield`="'.$species.'")' or die(mysql_error());


//This is the number of results displayed per page 
$page_rows = 5; 
$rows = mysql_num_rows($data); 
//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

//This sets the range to display in our query 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 
//This is your query again, the same one... the only difference is we add $max into it 
$data_p = mysql_query("SELECT * FROM Reports ORDER BY id DESC $max") or die(mysql_error()); 

//This is where you display your query results 
while($info = mysql_fetch_array( $data_p )) 
{ 
  echo $info['month']; 
  echo $info['year']; 
      echo "<b>";   echo "<br />";   
   echo $info['title'];    echo "</b>";echo "<br />";  
   echo $info['prostaff']; 
   echo "<br />";   echo "<br />"; 
echo "<br />"; 
    
   echo $info['report']; 
  echo "<br />";echo "<br />"; echo "<br />"; echo "<br />";  
  } 
echo "<p>"; 
echo "<center>"; 

// This shows the user what page they are on, and the total number of pages 
echo " --Page $pagenum of $last-- <p>"; 

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. 
if ($pagenum == 1) 
{ 
} 
else 
{ 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; 
echo " "; 
$previous = $pagenum-1; 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; 
} 

//just a spacer 
echo " ---- "; 

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links 
if ($pagenum == $last) 
{ 
} 
else { 
$next = $pagenum+1; 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; 
echo " "; 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; 
echo "</center>"; 
} 
?>  
</body></html>

I appreciate all the help, here is where I'm at.

when I load the page, the main data base is not loading, and when i select a species, it goes to my page not found...

<html><body><form action ="POST" action="" name="myform">
Species: <SELECT NAME="species"> 
<OPTION VALUE="trout">Trout 
<OPTION VALUE="salmon">Salmon 
<OPTION VALUE="bass">Bass 
<OPTION VALUE="muskies">Muskies 
<OPTION VALUE="browntrout">Brown Trout 
<OPTION VALUE="biggame">Big Game 
<OPTION VALUE="saltwater">Saltwater 
<OPTION VALUE="waterfowl">Waterfowl 
<OPTION VALUE="pheasants">Pheasants 
<OPTION VALUE="icefishing">Ice Fishing 
</SELECT> 
         
         <input type="submit" name="submit" value="Search!" /> 
      </form> 
       
      <br><br><br> 
<?php

if (isset($_POST['submit'])){ 
$species = $_POST['species'];
// Connects to your Database 
mysql_connect("localhost", "xxxxxxxxx", "xxxxxxxxxx") or die(mysql_error()); 
mysql_select_db("xxxxxxxxxxxxxxxx") or die(mysql_error()); 

//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query('SELECT * FROM Reports WHERE `species`="'.$species.'"') or die(mysql_error());


//This is the number of results displayed per page 
$page_rows = 5; 
$rows = mysql_num_rows($data); 
//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

//This sets the range to display in our query 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 
//This is your query again, the same one... the only difference is we add $max into it 
$data_p = mysql_query("SELECT * FROM Reports ORDER BY id DESC $max") or die(mysql_error()); 

//This is where you display your query results 
while($info = mysql_fetch_array( $data_p )) 
{ 
  echo $info['month']; 
  echo $info['year']; 
      echo "<b>";   echo "<br />";   
   echo $info['title'];    echo "</b>";echo "<br />";  
   echo $info['prostaff']; 
   echo "<br />";   echo "<br />"; 
echo "<br />"; 
    
   echo $info['report']; 
  echo "<br />";echo "<br />"; echo "<br />"; echo "<br />";  
  } 
echo "<p>"; 
echo "<center>"; 

// This shows the user what page they are on, and the total number of pages 
echo " --Page $pagenum of $last-- <p>"; 

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. 
if ($pagenum == 1) 
{ 
} 
else 
{ 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; 
echo " "; 
$previous = $pagenum-1; 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; 
} 

//just a spacer 
echo " ---- "; 

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links 
if ($pagenum == $last) 
{ 
} 
else { 
$next = $pagenum+1; 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; 
echo " "; 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; 
echo "</center>"; 
} }
?>  
</body></html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.