Jump to content

Why this code doesn't run?


Truman

Recommended Posts

I'm working on multidimensiona arrays, I ran this code thru php checker and everything looks fine.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Larry Ullman's books and chapters.</title>
</head>
<body>
<h1>Some of Larry Ullman's books</h1>
<?php // Script 7.4 - books.php
$phpvqs = array (1 => 'Getting Started with PHP', 'Variables', 'HTML Forms and PHP', 'Using Numbers');
$phpadv = array (1 => 'Advanced PHP Techniques', 'Developing Web Applications', 'Advanced Databe Concepts', 'Security Techniques');
$phpmysql = array (1 => 'Introduction to PHP', 'Programming with PHP', 'Creating Dynamic Web Sites', 'Introduction to MySQL');
$books = array (
'PHP VQS' => $phpvqs,
'PHP Advanced VQP' => $phpadv,
'PHP and MySQL VQP' => $phpmysql
);
print "<p>The third chapter of my first book is <i>{$books['PHP VQS'][3]}</i>.</p>";
print "<p>The first chapter of my second books is <i>{$books['PHP Advanced VQP'][1]}</i>.</p>";
print "<p>The fourth chapter of my fourt books is <i>{$books['PHP and MySQL VQP'][4]}</i>.</p>";
foreach ($books as $key => $value)
{
print "<p>$key: $value</p>\n";
}
?>
</body>
</html>

But it sends message:

Notice: Array to string conversion in C:\xampp\htdocs\my-site\books.php on line 24

PHP VQS: Array


Notice: Array to string conversion in C:\xampp\htdocs\my-site\books.php on line 24


PHP Advanced VQP: Array


Notice: Array to string conversion in C:\xampp\htdocs\my-site\books.php on line 24


PHP and MySQL VQP: Array

 

-------------------------------------------------------------------------------------------------------------------

line 24 is: print "<p>$key: $value</p>\n";

Edited by Truman
Link to comment
Share on other sites

The issue is this:

foreach ($books as $key => $value)
{
print "<p>$key: $value</p>\n";
}

$value is an array, not a string, so you can't just put it inside a string.

Link to comment
Share on other sites

Or run $value as an another foreach to retrieve each value

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Larry Ullman's books and chapters.</title>
    </head>
    <body>
        <h1>Some of Larry Ullman's books</h1>
        <?php
        // Script 7.4 - books.php
        $phpvqs = array(1 => 'Getting Started with PHP', 'Variables', 'HTML Forms and PHP', 'Using Numbers');
        $phpadv = array(1 => 'Advanced PHP Techniques', 'Developing Web Applications', 'Advanced Databe Concepts', 'Security Techniques');
        $phpmysql = array(1 => 'Introduction to PHP', 'Programming with PHP', 'Creating Dynamic Web Sites', 'Introduction to MySQL');
        $books = array(
            'PHP VQS' => $phpvqs,
            'PHP Advanced VQP' => $phpadv,
            'PHP and MySQL VQP' => $phpmysql
        );
        print "<p>The third chapter of my first book is <i>{$books['PHP VQS'][3]}</i>.</p>";
        print "<p>The first chapter of my second books is <i>{$books['PHP Advanced VQP'][1]}</i>.</p>";
        print "<p>The fourth chapter of my fourt books is <i>{$books['PHP and MySQL VQP'][4]}</i>.</p>";
        foreach ($books as $key => $value) { //key and value(array) for books array
            print "<p>$key:";
            $join = "";

            foreach ($value as $value2) { //value(array) of above read out as individual values
                $join.= " $value2, ";
            }
            $join = rtrim($join, ', ');
            print $join;
            print ".</p>\n";
        }
        ?>
    </body>
</html>
  • Like 1
Link to comment
Share on other sites

So the author of this book is wrong? That's strange...

p.s. I understand that you are right. It doesn't work.

A simple fix would be to join the array elements into a string.

foreach ($books as $key => $value)
{
print "<p>$key: " . implode(', ', $value) . "</p>\n";
}
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

I'm making now a template and within header.html I placed two date functions but when I run the code I don't see them on the browser. Don't know why, everything is by the book.

<!DOCTYPE html>
<head>
<title>
</title>
<?php // Print the page title
if (defined('TITLE')) {
print TITLE;
} else {
print 'Raise High the Roof Beam! A J.D. Salinger Fan Club';
}
?>
</head>
<body>
<div id="wrapper">
 <div id="header">
  <p class="description">A J.D. Salinger Fan Club</p>
  <h1><a href="index.php">Raise High the Roof Beam!</a></h1>
  <ul id="nav">
    <li><a href="books.php">Books</a></li>
    <li><a href="#">Stories</a></li>
    <li><a href="#">Quotes</a></li>
    <li><a href="login.php">Login</a></li>
    <li><a href="register.php">Register</a></li>
  </ul>	
 </div><!-- header -->
 <div id="sidebar">
  <h2>Favorite Quotes</h2>
   <p class="news">I don't exactly know what I mean by that, but I mean it.< /br> <em>Catcher in the Rye</em></p>
   <p class="news">I privately say to you, old friend...please accept from me this unpretentious bouqet of early-blooming parentheses: (((()))). <em>Raise high the Roof Beam, Carpenters and Seymour: An Introduction</em></p>
   <p><?php
  date_default_timezone_set('America/New_York');
  print date('g:i a l F j');
   ?></p>
    </div><!-- sidebar -->
 <div id="content">
 <!-- BEGIN CHANGEALBE CONTENT. -->
 <!-- Script 8.6 - header.html -->
Edited by Truman
Link to comment
Share on other sites

If you wish to use PHP then the file extension needs to be changed to .php. You could also change the server settings to run PHP code in files with the .html extension but I don't think that is necessary.

  • Like 1
Link to comment
Share on other sites

Can you please take a look at this code, from some reason it doesn't work.

<?php // Script 8.9 - register.php
define ('DEFINE', 'register');
include('templates/header.html');
print '<h2>Registration Form</h2>
  <p>Register so that you can take advantage of certain features like this, that, and the other things</p>';
print '<style type="text/css" media="screen">
  .error { color: red; }
 </style>';
 if ($_SERVER['REQUESTED_METHOD'] == 'POST') {
    $problem = FALSE;
	if (empty($_POST['first_name'])) {
	   $problem = TRUE;
	   print '<p class="error">Please enter your first name!</p>';
	   }
    if (empty($_POST['last_name'])) {
	   $problem = TRUE;
	   print '<p class="error">Please enter your last name!</p>';
	   }
	if (empty($_POST['email'])) {
	   $problem = TRUE;
	   print '<p class="error">Please enter your email address.</p>';
	   }
    if (empty($_POST['password1'])) {
	   $problem = TRUE;
	   print '<p class="error">Please enter a password.</p>';
	}	 
    if ($_POST['password1'] != $_POST['password2'])	 {
	   $problem = TRUE;
	   print '<p class="error">Your password did not match your confirm password.</p>';
	}
	if (!$problem) {
	print '<p>You are now registered.</p>';
	}
	$_POST = array();
} else {
	print '<p class="error">Please try again!</p>';
   }

?>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" value="<?php if (isset($_POST['first_name'])) { print 
	htmlspecialchars($_POST['first_name']);  } ?>" /></p> 
<p>Last Name: <input type="text" name="last_name" size="20" value="<?php if (isset($_POST['last_name'])) { print 
	htmlspecialchars($_POST['last_name']); 
 } ?>" /></p>
<p>Email: <input type="text" name="email" size="20" value="<?php if (isset($_POST['email']) { print 
	htmlspecialchars($_POST['email']);} ?> /></p>
<p>Password: <input type="password" name="password1" size="20" value="<?php if (isset($_POST['password1']) { print 
	htmlspecialchars($_POST['password']); } ?> /></p>
<p>Confirm Password: <input type="password" name="password2" size="20" value="<?php if (isset($_POST['password2']))  { print htmlspecialchars ($_POST[password2']); } ?>" /></p>
<p><input type="submit" name="submit" value="Register!"> /></p>
</form>
<?php include('templates/footer.html'); ?>

I receive a note:
Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\my-site\register.php on line 46

 

line 46 is:

<p>Email: <input type="text" name="email" size="20" value="<?php if (isset($_POST['email']) { print 
Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>View Your Settings</title>
<style type="text/css">
	body { font-size: x-large;
	       color: #999;
	}
<?php // Script 9.2 - view_settings.php
 if (isset($_COOKIES['font_size'])) {
 print "\t\tfont-size: " . htmlentites($_COOKIES['font_size']) . "; \n 
 } else {
 print "font-size: medium;"; 
 }
 if (isset($_COOKIES['font_color'])) {
 print "color: #" . htmlentities($_COOKIES['font_color']) . ";\n";
 } else {
 print "\t\tcolor: #ooo;";
 }
 ?>
 </style>
 </head>
 <body>
  <p><a href="customize.php">Customize your settings.</a></p>
  <p><a href="reset.php">Reset your settings.</a></p>
 <p>yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda</p>
</body>
</html>

Can you please tell me why I receive this message>

Parse error: syntax error, unexpected 'size' (T_STRING) in C:\xampp\htdocs\my-site\view_settings.php on line 15

line 15 is:

 print "font-size: medium;"; 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...