New to PHP
Posted: Tue Apr 08, 2008 6:18 pm
Hello all, I'm new to PHP (coming from ASP) and need a little direction.
I have a page with pagination that pulls records from a database. My SQL query is built on the fly depending on how the user used the form on the search page.
The SQL is:
Which works fine. But when I go to the "Next" page, I lose the $query value because it is built by the POST data from the original form, and the SQL statement no longer work.
I cannot seem to wrap my head around what to do logically. Any ideas? Thanks in advance.
I have a page with pagination that pulls records from a database. My SQL query is built on the fly depending on how the user used the form on the search page.
The SQL is:
Code: Select all
"SELECT * FROM `jos_jobfeeds` WHERE $query;"I cannot seem to wrap my head around what to do logically. Any ideas? Thanks in advance.
Code: Select all
<?php
$queryAry = array();
if ($_POST[Amateur]<>"")
{
$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[Amateur])."'";
}
if ($_POST[Corporate]<>"")
{
$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[Corporate])."'";
}
if ($_POST[Facilities]<>"")
{
$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[Facilities])."'";
}
if ($_POST[Health]<>"")
{
$queryAry[] ="jobsegment = '".mysql_real_escape_string($_POST[Health])."'";
}
if ($_POST[Professional]<>"")
{
$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[Professional])."'";
}
if ($_POST[Sporting]<>"")
{
$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[Sporting])."'";
}
if ($_POST[Location]="Region")
{
if ($_POST[West]<>"")
{
$queryAry[] = "jobregion = '".mysql_real_escape_string($_POST[West])."'";
}
if ($_POST[Central]<>"")
{
$queryAry[] = "jobregion = '".mysql_real_escape_string($_POST[Central])."'";
}
if ($_POST[Northeast]<>"")
{
$queryAry[] = "jobregion = '".mysql_real_escape_string($_POST[Northeast])."'";
}
if ($_POST[Southeast]<>"")
{
$queryAry[] = "jobregion = '".mysql_real_escape_string($_POST[Southeast])."'";
}
if ($_POST[Canada]<>"")
{
$queryAry[] = "jobregion = '".mysql_real_escape_string($_POST[Canada])."'";
}
}
if ($_POST[Location]="State")
{
if ($_POST[State]<>"")
{
$queryAry[] = "jobstateabbrv = '".mysql_real_escape_string($_POST[State])."'";
}
}
$query = implode(' OR ', $queryAry);
$t = mysql_query("SELECT * FROM `jos_jobfeeds` WHERE $query;");
if(!$t) die(mysql_error());
$a = mysql_fetch_object($t);
$total_items = mysql_num_rows($t);
$limit = $_GET['limit'];
$type = $_GET['type'];
$page = $_GET['page'];
//set default if: $limit is empty, non numerical, less than 10, greater than 50
if((!$limit) || (is_numeric($limit) == false) || ($limit < 10) || ($limit > 50)) {
$limit = 10; //default
}
//set default if: $page is empty, non numerical, less than zero, greater than total available
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) {
$page = 1; //default
}
//calcuate total pages
$total_pages = ceil($total_items / $limit);
$set_limit = $page * $limit - ($limit);
//query: **EDIT TO YOUR TABLE NAME, ECT.
$q = mysql_query("SELECT * FROM `jos_jobfeeds` WHERE $query LIMIT $set_limit, $limit");
if(!$q) die(mysql_error());
$err = mysql_num_rows($q);
if($err == 0) die("No matches met your criteria.");
//Results per page: **EDIT LINK PATH**
echo("
<a href=http://www.mysite.com/index.php?option=com_content&task=view&id=375&Itemid=271&cat=$cat&limit=10&page=1&Refresh=No>10</a> |
<a href=http://www.mysite.com/index.php?option=com_content&task=view&id=375&Itemid=271&cat=$cat&limit=25&page=1&Refresh=No>25</a> |
<a href=http://www.mysite.com/index.php?option=com_content&task=view&id=375&Itemid=271&cat=$cat&limit=50&page=1>50</a>");
//show data matching query:
echo ("<table border='0' width='100%' bordercolor='#000000'>");
echo ("<tr>");
echo ("<td valign='top'>");
echo ("<table border='0' width='100%' cellspacing='0' cellpadding='4' style='border-left-width: 0px; border-right-width: 0px'>");
echo ("<tr>");
echo ("<td><h3>Results of Your Search</h3></td>");
echo ("</tr>");
echo ("<tr>");
echo ("<td style='border-style: solid; border-width: 1px' bordercolor='#CBDDF1' bgcolor='#CBDDF1'>");
echo ("<table border='0' width='100%'>");
echo ("<tr>");
echo ("<td width='10%'><font color='#000000'><b>DATE</b></font></td>");
echo ("<td width='35%'><font color='#000000'><b>JOB TITLE</b></font></td>");
echo ("<td width='35%'><font color='#000000'><b>EMPLOYER</b></font></td>");
echo ("<td width='15%'><font color='#000000'><b>LOCATION</b></font></td>");
echo ("</tr>");
echo ("</table>");
echo ("</td>");
echo ("</tr>");
while($code = mysql_fetch_object($q)) {
echo ("<tr>");
echo ("<td style='border-style: solid; border-width: 1px' bordercolor='#CBDDF1'>");
echo ("<table border='0' width='100%' height='31' cellpadding='0'>");
echo ("<tr>");
echo ("<td bgcolor='#EDF3FA' height='20' style='border-bottom-style: solid; border-bottom-width: 1px'>");
echo ("<table border='0' cellspacing='0' cellpadding='2' width='100%' height='35'>");
echo ("<tr>");
echo ("<td width='10%' height='20'>$code->lastupdatedate</td>");
echo ("<td width='35%' height='20' valign='top'><a href='http://www.mysite.com/index.php?option=com_content&task=view&id=372&Itemid=269&jobid=$code->jobid'><b>$code->jobtitle</b></a></td>");
echo ("<td width='25%' height='20' valign='top'>$code->employer</td>");
echo ("<td width='25%' height='20' valign='top'>$code->jobcity, $code->jobstate</td>");
echo ("</tr>");
echo ("<tr>");
echo ("<td width='10%'> </td>");
echo ("<td width='85%' colspan='3' valign='top'>$code->excerpt</td>");
echo ("</tr>");
echo ("</table>");
echo ("</td>");
echo ("</tr>");
echo ("</table>");
echo ("</td>");
echo ("</tr>");
}
echo ("</table>");
echo ("</td>");
echo ("</tr>");
echo ("</table>");
$cat = urlencode($cat); //makes browser friendly
//prev. page: **EDIT LINK PATH**
$prev_page = $page - 1;
if($prev_page >= 1) {
echo("<b><<</b> <a href=http://www.mysite.com/index.php?option=com_content&task=view&id=375&Itemid=271&cat=$cat&limit=$limit&page=$prev_page><b>Prev.</b></a>");
}
//Display middle pages: **EDIT LINK PATH**
for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo("<b> $a</b> | "); //no link
} else {
echo(" <a href=http://www.mysite.com/index.php?option=com_content&task=view&id=375&Itemid=271&cat=$cat&limit=$limit&page=$a> $a </a> | ");
}
}
//next page: **EDIT THIS LINK PATH**
$next_page = $page + 1;
if($next_page <= $total_pages) {
echo("<a href=http://www.mysite.com/index.php?option=com_content&task=view&id=375&Itemid=271&cat=$cat&limit=$limit&page=$next_page><b>Next</b></a> > >");
}
//all done
?>