Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 12th, 2003, 07:11 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 12 code error

I'm trying to run the userviewer.php but I keep getting this error:
"Notice: Undefined variable: action in c:\inetpub\wwwroot\userviewer.php on line 247" which refers to this code:
switch($action) {
   case "view_record":
      view_record();
   break;
   default:
      list_records();
   break;
}

I'm using the files supplied by Wrox "userviewer.php" and "common_db.inc". Does anyone have any clue as to why this won't work for me? My mind has turned to mush trying to figure it out.

Please help

Ron
Reply With Quote
  #2 (permalink)  
Old August 11th, 2003, 05:47 PM
Registered User
 
Join Date: Aug 2003
Location: Vancouver, BC, Canada.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to docsabre
Default

This code looks okay to me. Check to make sure the punctuation is okay (;'s and :'s in right places) and you are not missing a $ symbol. Also, the parser sometimes is off by several lines.

Reply With Quote
  #3 (permalink)  
Old August 22nd, 2003, 03:49 PM
Authorized User
 
Join Date: Jun 2003
Location: Indianapolis, IN, USA.
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In the program userviewer.php, before the statement in line 247
"switch($action) {" add the given code below:

  if(!isset($action))
     $action='list_records';

Now you can run your code without any warning message.


Jennifer D. Taylor
Wiley Technical Support
Reply With Quote
  #4 (permalink)  
Old August 30th, 2003, 02:51 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

This is another fine example of the lack of explicit definitions in Beginning PHP 4. When the book was issued the PHP group had not yet changed the error reporting directive in php.ini to E_ALL by default, showing all errors and notices including undefined variables and well a multitude of what may seem like a very strict way of writing PHP code. Instantly tightening up PHP's previously very loose way of interpretting code which Beg. PHP 4 relied very heavily on.

Using isset() which checks for a variable's existence and empty() which checks for null value is much, much better than simply going into a statement. The use of these functions is also very handy for tracking down bugs~!

Consider this example

if ($foo) {

    #Execute code relying on the existence and non-empty value of $foo

}

Well if you write a statement like this:

$foo = false;

$foo now contains a false value but would still be considered null, and the above statement would not execute.

Now you would want to use:

if (isset($foo)) {

   #Execute code which rely's on $foo's existence but not its value

    if (empty($foo)) {

       #If $foo contains an empty, null, zero, or false boolean value... execute this code

    }

    # Other statements relying on the existence of $foo

}

The undefined variable warning basically means that you need to explicitly set $foo's value before it may be compared or used in any context.
This is good because most every other language that exists requires an explicit variable declaration before the variable may be used. And again in the long run you will find that this will aid in the alleviation of errors and be a fantastic tool in the debugging process.

: )
Rich




:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #5 (permalink)  
Old August 31st, 2003, 02:08 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Well now thinking a bit more about this, this statement does not even rely on the existence of the variable, only that it contains a value other than null.

if ($foo) {

    #Execute code relying on the non-empty value of $foo

}

See these helpful urls for more information:
http://us2.php.net/isset
http://us2.php.net/empty
http://www.blueshoes.org/en/developer/php_cheat_sheet/

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #6 (permalink)  
Old September 10th, 2003, 01:19 AM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I hate to barge in on an old thread, but the above code won't work as you expect if register_globals is off, regardless of your error_reporting setting or using if(isset($foo)) instead of if($foo).

Using isset($foo) will suppress the warning, but at the cost of breaking the intended functionality of the site -- isset() will return false, because $foo doesn't exist, even if $_GET['foo'] does, and your action code will never execute.

Change your $foo to $_GET['foo'] or $_POST['foo'] as appropriate, and if you've got the time, read these other related posts:

  http://p2p.wrox.com/archive/beginnin...2002-11/17.asp
  http://p2p.wrox.com/topic.asp?TOPIC_ID=3935


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #7 (permalink)  
Old September 10th, 2003, 01:50 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I wasn't aware that there was a method that needed to be called upon... which apparently must be mentioned in the book. I hadn't bothered to have a look at the book.

My line of thought came from I saw that the use of isset was suggested but no real explaination why. I was attempting to explain the intended benefits of more explicit code execution. Rather than just saying isset should be used... but then have no idea why it should be used.

Yes I know... if I'm going to help I should get off my keister and dig out the book!

: )
Rich





:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #8 (permalink)  
Old September 10th, 2003, 12:49 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To be honest, I don't even *have* that book... I just remember hundreds of posts about the chapter 12 "User Viewer" and chapter 3 "Favorite Author" scripts not working, and register_globals was pretty much the reason for all the headaches. =)


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #9 (permalink)  
Old September 23rd, 2003, 06:13 AM
Registered User
 
Join Date: Sep 2003
Location: Novi Sad, , Yugoslavia.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?php
$dbhost = 'localhost';
$dbusername = 'phpuser';
$dbuserpassword = 'phppass';
$default_dbname = 'sample_db';
$default_sort_order = 'ASC';
$default_order_by = 'usernumber';
$records_per_page = 5;
$user_tablename = 'user';
$access_log_tablename = 'access_log';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$new_win_width = 600;
$new_win_height = 400;
function html_header() {
global $new_win_width, $new_win_height;
?>
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function open_window(url) {
var NEW_WIN = null;
NEW_WIN = window.open ("","RecordViewer", "toolbar=no,width="+
<?php echo $new_win_width ?>+
",height="+<?php echo $new_win_height?>+
",directories=no,status=no,scrollbars=yes,resize=n o,menubar=no");

....


Reply With Quote
  #10 (permalink)  
Old September 23rd, 2003, 01:55 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi Vlada,

Code:
<?php

//snip snip 

$new_win_width = 600;
$new_win_height = 400; 

function html_header()
{

    # You don't need the global keyword, use the $GLOBALS superglobal array instead
    # Instead of terminating PHP parsing just use single quotes to avoid escaping quotes.

    echo "<html>
        <head>
            <script language='JavaScript' type='text/javascript'>
                <!--

                    function open_window(url)
                    {

                        var NEW_WIN = null;

                        NEW_WIN = window.open ('','RecordViewer', 'toolbar=no,width=' + {$GLOBALS["new_win_width"]} + ', height= ' + {$GLOBALS["new_win_height"]} + ',directories=no, status=no, scrollbars=yes, resize=no, menubar=no');



        ";

}
In your original code you also forgot to terminate the line with a semi-colon.

<?php echo $new_win_width; ?>+

The global keyword is used inside of a function to export local function variables into the global scope. So along with a parse error (from the missing semicolon) you would have had empty output. To obtain the value of the width and height parameters, you would need to either pass these as arguments to the html_header() function or use the $GLOBALS supergloal array which transcends all variable scoping rules.

Read: http://us2.php.net/manual/en/languag...predefined.php
And: http://us2.php.net/manual/en/languag...bles.scope.php


Also see my HTML Standards Compliance FAQ:
(Avoid using capital letters in HTML tags!)

http://p2p.wrox.com/topic.asp?TOPIC_ID=4028

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 12 Error with binding Calendar? rsearing BOOK: Beginning ASP.NET 2.0 and Databases 2 October 9th, 2006 07:51 AM
Chapter 12 Code Behind Problem locknload7 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 March 30th, 2005 10:34 AM



All times are GMT -4. The time now is 01:50 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.