RE: session variables - help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Allen McCabe [mailto:allenmccabe@xxxxxxxxx]
> Sent: 14 August 2009 06:58

 
> My ai with using unset($var) in update_order.php is to set the
> SESSION
> variable for an item to ' ' (empty) so that it would not show up on
> the
> order summary (because my writeResultRow() function will only write
> a row if
> that variable is greater than 0).
> 
> I just can't figure out what I'm missing here. Before I received
> your
> response, I made a few changes to my code, which helped streamline
> the
> calculating parts (grabbing values from SESSION instead of POST, and
> now
> when I update order_summary, the values will remain because it pulls
> them
> from the SESSION).
> 
> I want to edit the values in the SESSION, so that when
> update_order.php
> redirects to order_process.php, the values are changed, and if
> applicable,
> an item is removed from the html table (if the quantity is less than
> 1).
> 
> Here is some more complete code:
> 
> [code = order_process.php]
> 
> <?php
> session_start();
> // POST ALL $_POST VALUES, CREATE AS VARIABLES IN SESSION
> foreach($_POST as $k=>$v) {
>  $_SESSION[$k]=$v;
> }

This has just destroyed anything that was previously in the session, so if you're recycling from the update_order.php script, you've just thrown away whatever that script did!  You need to make this conditional on having arrived here from the initial form -- various ways you could do that, but I leave you to figure that one out.

(Also, personally, if I were doing this at all, I would just copy the array as a single entity:

    $_SESSION['_POST'] = $_POST;

and then reference individual elements through that as, e.g., $_SESSION['_POST']['School']. That's probably a matter of personal style as much as anything, but gives you another way to think about.)


[ . . . . ]


> 
> <?php
> 
> function findTotalCost($b, $c) {
>  $total = $b * $c;
>  return $total;
> }
> 
> function writeResultRow($a, $b, $c, $d, $e, $f) {
>  if($a != '') {
>   echo "\n<tr>\n\t";
>   echo "<td'>".$b."</td><td>".$c."</td><td>".$d."</td>";
>   echo "<td>".$e."</td><td>&nbsp;</td><td><input type='text'
> value='".$a."'
> name='".$a."' id='".$a."' size='2'
> /></td><td>=</td><td>\$".$f."</td>";
>   echo "</tr>";
>  }
> }
> 
> //SETS $Total_show_01 to PRICE * QUANTITY
> //FORMATS TOTAL
> //IF A QUANTITY IS ENTERED, WRITES THE ROW WITH CURRENT VARIABLES
> $Total_show_01 = findTotalCost($shows['show_01']['price'],
> $_SESSION['show_01_qty']);
> $Total_show_01_fmtd = number_format($Total_show_01, 2, '.', '');
> writeResultRow($_SESSION['show_01_qty'], $shows['show_01']['title'],
> $shows['show_01']['date'], $shows['show_01']['time'],
> $shows['show_01']['price'],$Total_show_01_fmtd);
> 
> //ABOVE LINES REPEATED FOR ALL 38 ENTITIES (show_01 to show_38)

AAAAAARRRRRRRGGGGGGGGGHHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!

This cries out for an array-based solution -- repeating near-identical code that many times is totally ludicrous, and should be a major clue that you need to refactor.  You'll have to forgo using indexes like ['show_01'] and use straight integers, but the massive reduction in repetitive code (and hence far fewer opportunities for mistakes!) will be well worth it.

Something like:

   for ($i=1; $i<=38; ++$i):
      $Total[$i] = findTotalCost($shows[$i]['price'], $_SESSION['qty'][$i]);
      $Total_fmtd[$i] = number_format($Total[$i], 2, '.', '');
      writeResultRow($_SESSION['qty'][$i], $shows[$i]['title'], $shows[$i]['date'], $shows[$i]['time'], $shows[$i]['price'],$Total_fmtd[$i]);
   endfor;

[ . . . . ]
 
> Now, here is the update_order.php code in entirety:
> 
> [code]
> 
> <?php
> session_start();
> foreach ($_SESSION as $var => $val) {
>  if ($val == "0") {
>   unset($_SESSION[$var]);
>  } elseif ($val == '') {
>   unset($_SESSION[$var]);
>  } else {
>   $val = $_SESSION[$var];

That line is back-to-front -- you're assigning the current value in the session to $val, which is then immediately thrown away as the foreach loop starts a new iteration. What you mean is $_SESSION[$var] = $val.


Cheers!

Mike
 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.ford@xxxxxxxxxxxxxx 
Tel: +44 113 812 4730






To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux