Wrox Programmer Forums
|
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 13th, 2003, 10:06 AM
Authorized User
 
Join Date: Dec 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with homework CHPT8,Page265

Welcome.
This is my first time :).
Sorry for my english.
I got problem with file:bill.php on page 265.
There is a second version this file(first is on page261).There wrote something about delet prefix as homework.I don't know how i can do that.Can someone paste here listing this file?Please... :)
 my files:
menu1b.php**************************************** **
<?php
 $Entrees=array("Stek (9zl)", "Pizza (7zl)", "Makaron (6zl)");
  echo "<FORM MATHOD=POST ACTION='menu2b.php'>";
 echo "Co sobie zyczysz na obiad?";
 echo "<SELECT NAME='PERS_Entree'>";
 echo "<OPTION SELECTED VALUE=''>Wybierz...</OPTION>";
 echo "<OPTION>$Entrees[0]</OPTION>";
 echo "<OPTION>$Entrees[1]</OPTION>";
 echo "<OPTION>$Entrees[2]</OPTION>";
 echo "</SELECT><BR><BR>";
 if ($HTTP_POST_VARS)
 {
  while (list($lvar, $lvalue)=each($HTTP_POST_VARS))
  {
   if (ereg("^PERS_", $lvar, $throwaway))
   {
    echo "<INPUT TYPE=HIDDEN NAME='$lvar' VALUE='$lvalue'>\n";
   }
  }
 }
 echo "<INPUT TYPE=SUBMIT>";
 echo "</FORM>";
?>
menu2b.php**************************************** **********
<?php
 $Desserts=array("Jablecznik (3zl)","Nalesniki (3zl)","Lody (2zl)");
 echo "<FORM METHOD=POST ACTION='billb.php'>";
 echo "Co sobie zyczysz na deser?";
 echo "<SELECT NAME='PERS_Dessert'>";
 echo "<OPTION SELECTED VALUE=''>Wybierz...</OPTION>";
 echo "<OPTION>$Desserts[0]</OPTION>";
 echo "<OPTION>$Desserts[1]</OPTION>";
 echo "<OPTION>$Desserts[2]</OPTION>";
 echo "</SELECT><BR><BR>";
 if ($HTTP_POST_VARS)
 {
  while (list($lvar, $lvalue)=each($HTTP_POST_VARS))
  {
   if (ereg("^PERS_", $lvar, $throwaway))
   {
    echo "<INPUT TYPE=HIDDEN NAME='$lvar' VALUE='$lvalue'>\n";
   }
  }
 }
 echo "<INPUT TYPE=SUBMIT>";
 echo "</FORM>";
?>
billb.php***************************************
<?php
 $total=0;
 echo "Zamawiales nastepujace pozycje:<BR><BR>";
 if($HTTP_POST_VARS)
 {
  while (list($lvar, $lvalue)=each($HTTP_POST_VARS))
  {
   if(ereg("^PERS_", $lvar, $throwaway)>0)
   {
    echo "$lvar: $lvalue<BR>";
    if (ereg("[0-9]+", $lvalue, $regs)) $total+=$regs[0];
   }
  }
 }
 echo "RAZEM= " . $total . " zl<BR>";
?>
 
Old December 15th, 2003, 01:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

I'm sorry but I don't quite understand what you want to do. I did notice a typo in your first page. The "method" attribute of your <form> tag is misspelled: You have "MATHOD" and it should be "method".

Also, you should use isset() to check the existence of a variable, use $_POST instead of $HTTP_POST_VARS, and use foreach() instead of while(list() = each()).

Here's a quick rewriting of billb.php to reflect these changes.

<?php

$total = 0;
echo "Zamawiales nastepujace pozycje:<br><br>\n";

if (isset($_POST))
{
    foreach ($_POST as $lvar => $lvalue)
    {
        if(ereg("^PERS_", $lvar, $throwaway)>0)
        {
            echo "$lvar: $lvalue<br>\n";

            if (ereg("[0-9]+", $lvalue, $regs))
            {
                $total += $regs[0];
            }
        }
    }
}

echo "RAZEM= " . $total . " zl<br>\n";
?>


If you are still having problems, please try asking your question again.


Take care,

Nik
http://www.bigaction.org/
 
Old December 15th, 2003, 07:34 PM
Authorized User
 
Join Date: Dec 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Main problem was "MATHOD". :]].
Big thanks for that.
Second problem:
I don't want see word:"PERS_" in answer.
example:there is: "PERS_Dessert: Nalesniki (3zl)"
There should bee: "Dessert: Nalesniki (3zl)"
What i have to do?

 
Old December 15th, 2003, 08:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, you can rename your form input fields to not include the PERS_ text. This might be made easier if you use arrays to store your form input data. You can organize all your desserts in an array, all your entrees in a different array, etc.

For more info, read my tip from a while back:
  http://p2p.wrox.com/archive/beginnin...2002-08/52.asp


If you'd just like to get your script working as soon as possible, then you can modify your regular expression check to only match the part you want to keep:

if(ereg("^PERS_(.*)$", $lvar, $matches)>0)
{
    echo "{$matches[1]}: $lvalue<br>\n";
    ...
}

$matches is my "regs" parameter. It is an array that stores all the parenthesized matches in the regular expression. $matches[0] is always the entire source string, if there was a match. $matches[1], $matches[2], ... are all the sub-parts of the regular expression that were enclosed in parenthesis.

In your expression, I added the text (.*)$, which means "and every character up till the end of the string.

Because I have the .* in parenthesis (that's the "every character" part), whatever matches THAT part of the expression will be put in it's own index into the $matches array. Since it's the first parenthesized expression, it will be $matches[1].

For more information:
  http://www.php.net/ereg


Take care,

Nik
http://www.bigaction.org/
 
Old December 17th, 2003, 07:27 PM
Authorized User
 
Join Date: Dec 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:) thanks for solve my problem.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 Homework - Help please satish_appasani BOOK: Professional WCF Programming: .NET Dev with Windows Communication Found ISBN: 9780470089842 0 July 29th, 2008 06:50 AM
Water Bill Homework DweeLer C++ Programming 10 November 9th, 2005 06:40 PM
My Homework (I solved it) yui0329 C# 1 June 10th, 2005 11:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.