New to PHP

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
pazafuera
Forum Newbie
Posts: 3
Joined: Tue Apr 08, 2008 6:11 pm

New to PHP

Post by pazafuera »

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:

Code: Select all

"SELECT * FROM `jos_jobfeeds` WHERE $query;"
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.

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%'>&nbsp;</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 
?>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: New to PHP

Post by flying_circus »

A couple options come to mind for passing your variable to the next page. You can use cookies, sessions, hidden form fields, or the querystring.
pazafuera
Forum Newbie
Posts: 3
Joined: Tue Apr 08, 2008 6:11 pm

Re: New to PHP

Post by pazafuera »

Thanks so much for the quick reply.

I've tried making a session variable, but perhaps my syntax isn't right.

Code: Select all

$_SESSION['query']=$query;
That creates the session...I've tested that. But when I plug the session variable into the SQL statement, I get no results...when there should be...here's my SQL statement:

Code: Select all

$t = mysql_query("SELECT * FROM `jos_jobfeeds` WHERE '".$_SESSION[query]."';");
I arrived at this by trial and error...its the only way I don't get an error, but I don't get any results either.

Any suggestions?
earcaraxe
Forum Newbie
Posts: 5
Joined: Mon Apr 07, 2008 8:33 pm

Re: New to PHP

Post by earcaraxe »

You need to do a

Code: Select all

session_start();
at the top of every php page that is using sessions.
pazafuera
Forum Newbie
Posts: 3
Joined: Tue Apr 08, 2008 6:11 pm

Re: New to PHP

Post by pazafuera »

Now that explains why I'm having no success. There's a concept that doesn't exist in ASP.

Thank you so much for your help.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: New to PHP

Post by flying_circus »

pazafuera wrote:Now that explains why I'm having no success. There's a concept that doesn't exist in ASP.

Thank you so much for your help.

You can configure the sessions to autostart in your php.ini config file, but I think it it generally left up to the dev to enable it :wink:

You should be able to check if it is enabled by doing something like:

Code: Select all

<?php
 
  if( ini_get('session.auto_start') ) {
    # Sessions are auto started for every page
    // ...
  }
 
?>
Post Reply