Solved

Session Variables revisited...

Posted on 2003-12-01
3
306 Views
Last Modified: 2008-07-03
I have a DWMX generated page that enables dynamic sorting of the column names: ie:

<a href="<?Php echo ($tfm_orderbyURL); ?>AppName">Application Name</a>
<a href="<?Php echo ($tfm_orderbyURL); ?>Class_Code">Class Code</a>
etc...

That all works fine...I also have the ability to select the number of records per page to be displayed:
Records per page to be displayed: <a href="<?php echo printf($currentPage); ?>?maxRows_Recordset1=25">25</a>
| <a href="<?php echo printf($currentPage); ?>?maxRows_Recordset1=50">50</a>
etc...

That all works fine..

My challenge is: if the user wants to resort the # of records or the field name / columns it negates the previous selection. Make sense?

What i am trying to figure out is how to "cache" a user's choice...ie: Sort by Application Name...then show 50 records per screen while still sorting by Application Name.

I found this earlier snippet of code (modified for this scenario) I'm wondering how / if it would work:

<?
 if ( strlen(trim($_SESSION['sort'])) <= 0){
  $_SESSION['appname']=$HTTP_POST_VARS['appname'];
  $_SESSION['maxRows_Recordset1']=$HTTP_POST_VARS['maxRows_Recordset1'];
}
?>
-
-
-
<a href="file.php">Click This</a><? echo $_SESSION['sort']; ?>

Please forgive my feeble attempt at the aforementioned example. I'm trying to make sense of this :)

Any suggestions...Thanks

-Neils
0
Comment
Question by:nriddock
3 Comments
 
LVL 13

Assisted Solution

by:lozloz
lozloz earned 200 total points
ID: 9851481
hi,

that alone won't work - all it does is to stick the choices into a session which is carried across pages (although you will need session_start(); right at the top of your script which i don't know if you have or not). so now you're storying the data you need to include it in the sorting logic. also if you're using $_SESSION, the $HTTP_POST_VARS variable should really become $_POST for both those instances. i think it'd be easier to help further if you could provide the code for the entire page

cheers,

loz
0
 

Author Comment

by:nriddock
ID: 9851804
Here goes:

<?php
//initialize the session
session_start();

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  session_unregister('MM_Username');
  session_unregister('MM_UserGroup');
      
  $logoutGoTo = "http://intranet";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php require_once('Connections/local_coverage.php'); ?>
<?php
$currentPage = $HTTP_SERVER_VARS["PHP_SELF"];
$self=$_SERVER['PHP_SELF'];
$tfm_orderby =(!isset($HTTP_GET_VARS["tfm_orderby"]))?"AppName":$HTTP_GET_VARS["tfm_orderby"];
$tfm_order =(!isset($HTTP_GET_VARS["tfm_order"]))?"ASC":$HTTP_GET_VARS["tfm_order"];
$sql_orderby = " " .$tfm_orderby." ".$tfm_order;
?>
<?php
$currentPage = $HTTP_SERVER_VARS["PHP_SELF"];

$SSAdv_colors1 = array("#CCCCCC","#CCCCCC");
$SSAdv_k1 = 0;
$SSAdv_m1 = 0;
$SSAdv_change_every1 = 1;

if (isset($_GET['maxRows_Recordset1']))  $maxRows_Recordset1 = $_GET['maxRows_Recordset1'];
else $maxRows_Recordset1 = 50;

$pageNum_Recordset1 = 0;
if (isset($HTTP_GET_VARS['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $HTTP_GET_VARS['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

$sqlorderby_Recordset1 = "AppName";
if (isset($sql_orderby)) {
  $sqlorderby_Recordset1 = (get_magic_quotes_gpc()) ? $sql_orderby : addslashes($sql_orderby);
}
mysql_select_db($database_local_coverage, $local_coverage);
$query_Recordset1 = sprintf("SELECT * FROM projects ORDER BY %s", $sqlorderby_Recordset1);

$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $local_coverage) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($HTTP_GET_VARS['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $HTTP_GET_VARS['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($HTTP_SERVER_VARS['QUERY_STRING'])) {
  $params = explode("&", $HTTP_SERVER_VARS['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . implode("&", $newParams);
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);

$queryString_Recordset1 = "";
if (!empty($HTTP_SERVER_VARS['QUERY_STRING'])) {
  $params = explode("&", $HTTP_SERVER_VARS['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . implode("&", $newParams);
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?>
<?php
//sort column headers for Recordset1
$tfm_saveParams = explode(",","");
$tfm_keepParams = "";
if($tfm_order == "ASC") {
      $tfm_order = "DESC";
}else{
      $tfm_order = "ASC";
};
while (list($key,$val) = each($tfm_saveParams)) {
      if(isset($HTTP_GET_VARS[$val]))$tfm_keepParams .= ($val)."=".urlencode($HTTP_GET_VARS[$val])."&";      
      if(isset($HTTP_POST_VARS[$val]))$tfm_keepParams .= ($val)."=".urlencode($HTTP_POST_VARS[$val])."&";
}
$tfm_orderbyURL = $HTTP_SERVER_VARS["PHP_SELF"]."?".$tfm_keepParams."tfm_order=".$tfm_order."&tfm_orderby=";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link href="coverage_styles.css" rel="stylesheet" type="text/css">
</head>


<body link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">
<table width="614" border="0" align="left">
  <tr>
    <td width="1800" colspan="3"><div align="center"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Records
          per page to be displayed: <a href="<?php echo printf($currentPage); ?>?maxRows_Recordset1=25"><font color="#FF0000">25</font></a> | <a href="<?php echo printf($currentPage); ?>?maxRows_Recordset1=50"><font color="#FF0000">50</font></a><font color="#FF0000"> <font color="#000000">|</font> <a href="<?php echo printf($currentPage); ?>?maxRows_Recordset1=100"><font color="#FF0000">100</font></a><font color="#FF0000"><font color="#000000"> |</font> <a href="<?php echo printf($currentPage); ?>?maxRows_Recordset1=5000"><font color="#FF0000">All</font></a></font></font></font></div>
    </td>
  </tr>
</table>
<br>
<br>
<br>
<table width="614" border="0">
  <tr>
    <td><table width="614" border="0" align="left" cellpadding="0" cellspacing="2" class="contentAreaBodyText">
      <tr bgcolor="#000000">
        <td align="center"><strong><font color="#FFFFCC" size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="<?Php echo ($tfm_orderbyURL); ?>AppName">Application
                Name</a></font></strong></td>
        <td width="67" align="center"><strong><font color="#FFFFCC" size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="<?Php echo ($tfm_orderbyURL); ?>Class_Code">IRM/BCP<br>
      Class</a></font></strong></td>
        <td width="84" align="center"><strong><font color="#FFFFCC" size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="<?Php echo ($tfm_orderbyURL); ?>VerMgmt">Version
                Mgmt</a></font></strong></td>
        <td width="87" align="center"><strong><font color="#FFFFCC" size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="<?Php echo ($tfm_orderbyURL); ?>ChgMgmt">Change
                Mgmt</a></font></strong></td>
        <td width="78" align="center"><strong><font color="#FFFFCC" size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="<?Php echo ($tfm_orderbyURL); ?>SysTest">System
                Test</a></font></strong></td>
        <td width="84" align="center"><strong><font color="#FFFFCC" size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="<?Php echo ($tfm_orderbyURL); ?>ERMProcComp">Release      Mgmt</a></font></strong></td>
      </tr>
      <?php do { ?>
      <tr bordercolor="1" bgcolor="<?php
if($SSAdv_m1%$SSAdv_change_every1==0 && $SSAdv_m1>0){
$SSAdv_k1++;
}
print $SSAdv_colors1[$SSAdv_k1%count($SSAdv_colors1)];
$SSAdv_m1++;
?>" class="contentAreaBodyText">
        <td width="205" align="center"><div align="left"><a href="coverage_detail.php?recordID=<?php echo $row_Recordset1['CM_ID']; ?>"><font color="#000000"><?php echo $row_Recordset1['AppName']; ?></font></a></div>
        </td>
        <td align="center"><div align="center"><?php echo $row_Recordset1['IRMBCPClass']; ?></div>
        </td>
        <td width="84" align="center" bgcolor=<?php echo $row_Recordset1['VerMgmt']; ?>><span class=contentAreaBodyText<?php echo $row_Recordset1['VerMgmt']; ?>><?php echo $row_Recordset1['VerMgmt']; ?></span></td>
        <td width="87" align="center" bgcolor=<?php echo $row_Recordset1['ChgMgmt']; ?>><span class=contentAreaBodyText<?php echo $row_Recordset1['ChgMgmt']; ?>><?php echo $row_Recordset1['ChgMgmt']; ?></span></td>
        <td width="78" align="center" bgcolor=<?php echo $row_Recordset1['SysTest']; ?>><span class=contentAreaBodyText<?php echo $row_Recordset1['SysTest']; ?>><?php echo $row_Recordset1['SysTest']; ?></span></td>
        <td width="84" align="center" bgcolor=<?php echo $row_Recordset1['ERMProcComp']; ?>><span class=contentAreaBodyText<?php echo $row_Recordset1['ERMProcComp']; ?>><?php echo $row_Recordset1['ERMProcComp']; ?></span></td>
      </tr>
      <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
    </table></td>
  </tr>
</table>
<br>
<br>
<br>


</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
0
 
LVL 6

Accepted Solution

by:
aolXFT earned 300 total points
ID: 9860155
use array_merge();


$user_selection = array_merge($_SESSION, $_POST, $_GET);

Then $user_selection['whatever']  will be $_GET['whatever'] , $_POST['whatever'], or $_SESSION['whatever'](_get if it exists, or if not _post. If neither exist it will use _session)

0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question