Jump to content

vipsa

Members
  • Posts

    25
  • Joined

  • Last visited

Everything posted by vipsa

  1. I would like to know why the following $myString = “Here’s a little string”; // Displays “Here-s+a+little+string” echo strtr( $myString, “ ‘”, “+-” ) . “ < br/ > ”; is printing this Here’s+a+little+string instead of this Here-s+a+little+string
  2. I have this very simple lines of code: <!DOCTYPE html> <html> <head> <title> Chapter 5 Strings </title> </head> <body> <a href="index.php">Back home</a><br><br> <?php $mytext = <<<END_TEXT This is just a bunch of text I will write here so we can test a couple of things with text. I will add a few lines just so we have some. END_TEXT; ?> </body> </html> So I am getting this error Parse error: syntax error, unexpected end of file and I would like to know why as the syntax is correct
  3. @requinix I am using netbeans 7.2 and it flags that line as BAD value for attribute action DOUBLE_WHITEPSPACE in PATH There are no spaces in the path and if you run the code it gives an error
  4. Hi I have very little php experience and I battle to understand how it works so I really need explaining how certain things work. Like I have the following code: The line <form action="echo htmlentities($_SERVER['PHP_SELF']); ?>" method="GET"> says bad value and double white space in path which I cannot find as there are not white spaces in the path. Please help me understand this <?php if (isset($_GET['message'])) { // load font and image, calculate width of text $font = "times"; $size = 12; $image = imagecreatefrompng("button.png"); $tsize = imagettfbbox($size, 0, $font, $_GET['message']); // center $dx = abs($tsize[2] - $tsize[0]); $dy = abs($tsize[5] - $tsize[3]); $x = (imagesx($image) - $dx) / 2; $y = (imagesy($image) - $dy) / 2 + $dy; // draw text $black = imagecolorallocate($im,0,0,0); imagettftext($image, $size, 0, $x, $y, $black, $font, $_GET['message']); // return image header("Content-type: image/png"); imagepng($image); exit; } ?> <html> <head> <title>Button Form</title> </head> <body> <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="GET"> Enter message to appear on button: <input type="text" name="message" /><br /> <input type="submit" value="Create Button" /> </form> </body> </html>
  5. OK but before I submit that form it gives me this error and I got this code from a book so please tell me how the logic is backwards in the first block
  6. Hi I am a newbie. I get an error undefined variable subject and it is the line in the form where subject is but how can the variable be undefined if I define it in the beginning? Why then is the other variable $text also then not undefined. I also get allot of css code in the form <?php if(!isset($_POST['submit'])) { $from = 'corne@viprojects.net'; $subject = $_POST['subject']; $text = $_POST['emailbody']; $output_form = false; if(empty($subject) && empty($text)) { echo 'You forgot the subject and text'; } $output_form = true; if(empty($subject) && !empty($text)) { echo 'You forgot to enter the subject'; } $output_form = true; if(!empty($subject) && empty($text)) { echo 'You forgot to enter the text'; } } else { $output_form = true; } if($output_form) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label for="subject">Subject</label><br> <input type="text" id="subject" name="subject" value="<?php echo $subject; ?>"><br> <label for="emailbody">Email body</label><br> <textarea id="emailbody" name="emailbody" rows="5" cols="20" ><?php echo $text; ?></textarea><br> <input type="submit" name="submit" value="submit"> </form>' <?php } ?>
  7. My question is also this: "If I closed a database connection how do I open that connection again"
  8. Hi I am new to php I have a small form that collects names and email addresses and inserts it into a database. I have an existing database connection for another form so I want to use that connection so I included the connection file in this script. When I try and insert new content I get errors ( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\wamp\www\headfirst\emailscript\addemail.php on line 14 Here is the code <code> <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; echo "Hi $firstname $lastname. <br> Thank you for sunmitting the form<br> we will be sending you an email soon to $email"; include $_SERVER['DOCUMENT_ROOT'] . 'headfirst/includes/db_connection.inc'; $sql = "use elvis" . "insert into email_list (first_name, last_name, email)" . "values ('$firstname', '$lastname', '$email')"; mysqli_query($dbconnection, $sql); mysqli_close($dbconnection); ?> </code> So my question is this: "How do I use an existing connection and how do I initialize it or what do I have to do please? Help is appreciated
  9. If you have code like this where you have two conditions <code> if (isset($_POST['action'] and $_POST['action'] == 'login') { if (!isset($_POST['email'] or $_POST['email'] == '' or !isset($_POST['password']) or $_POST['password'] == '') } $GLOBALS['loginError'] = 'Please fill in borh field'; return false; } </code> Why am I getting this error: Parse error: syntax error, unexpected 'and' (T_LOGICAL_AND), expecting ',' or ')' in C:\wamp\www\learnphp\includes\access.inc.php on line 4
  10. Thank you now I understand it better!
  11. There are 2 files database.php jokes.html.php there is a database learnphp with a tables jokes and WITH RECORDS INSIDE THE TABLE I want to display the records on the jokes.html.php page If I run database.php alone then it works fine and I see the results If I open jokes.html.php then I get variable $jokes undefined and a Warning: Invalid argument supplied for foreach() in C:\wamp\www\learnphp\jokes.html.php on line 12 Here is the code for both files: DATABASE.PHP: <?php try { $pdo = new PDO('mysql:host=localhost;dbname=learnphp', 'corne', 'lekker1'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec('SET NAMES "utf8"'); } catch (PDOException $e) { $error = 'Unable to connect to the database server.'; include 'error.html.php'; exit(); } try { $sql = 'SELECT joketext FROM jokes'; $result = $pdo->query($sql); } catch (PDOException $e) { $error = 'Error fetching jokes: ' . $e->getMessage(); include 'error.html.php'; exit(); } while ($row = $result->fetch()) { $jokes[] = $row['joketext']; } include 'jokes.html.php'; HERE IS THE CODE FOR JOKES.HTML.PHP: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>List of Jokes</title> </head> <body> <p><a href="?addjoke">Add your own joke</a></p> <p>Here are all the jokes in the database:</p> <?php foreach ($jokes as $joke): ?> <blockquote> <p><?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8'); ?> </p> </blockquote> <?php endforeach; ?> </body> </html> Now can someone please tell me what is wrong and what I need to do to fix this. I will be very grateful if I could get a clear answer explaining how this is supposed to work Thank you
  12. I'm sorry but this does not answer my question because why would someone writing a book have code like this if it does not work and I have seen this before so why would the while loop include a file if the variable is not available to the file that is being included? There are results and if you run the code that is in the database connection script it displays the results fine. It is when you want to display the results on another page jokes.html.php when the problem starts for me
  13. The table contains three rows This is a joke Two men are sitting in a bar The blond parks her car Here is all the php code for the connection script which if ran on it's own works fine. But when I include a template page then the page says variable undefined. So how do I get the template page to use the $jokes array? <code> <?php try { $pdo = new PDO('mysql:host=localhost;dbname=learnphp', 'corne', 'lekker1'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec('SET NAMES "utf8"'); } catch (PDOException $e) { $error = 'Unable to connect to the database server.'; include 'error.html.php'; exit(); } try { $sql = 'SELECT joketext FROM jokes'; $result = $pdo->query($sql); } catch (PDOException $e) { $error = 'Error fetching jokes: ' . $e->getMessage(); include 'error.html.php'; exit(); } while ($row = $result->fetch()) { $jokes[] = $row['joketext']; } include 'jokes.html.php'; </code> and the page supposed to show the results the code is in my first post.
  14. I am a newbie and I have a few questions please: 1. I am trying to get data from a database and display it in a template. So I have created all the connection things and they work and I have created a while loop to loop through the result set and here is the code <code> while ($row = $result->fetch()) { $jokes[] = $row['joketext']; } include 'jokes.html.php'; </code> But when you go to jokes.html.php then it tells you that the array $jokes is undefined. My questions is this: If I create a php file and I create variables or set variables and I then import a template file "Are the variables then availablle to the template file I import? Here is the code for the template file </code> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>List of Jokes</title> </head> <body> <p>Here are all the jokes in the database:</p> <?php foreach ($jokes as $joke): ?> <blockquote> <p><?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8'); ?> </p> </blockquote> <?php endforeach; ?> </body> </html> </code> Any help is appreciated
  15. I am a complete beginner so I need help. I have the following code: <code> $foodprices = array("vegetables"=>array("Tomatoes"=>1.00, "Beet"=>20.5, "Onions"=>3.5), "Meat"=>array("Skaap"=>20.5, "Bees"=>30.5)); var_dump($foodprices); foreach($foodprices as $category) { echo "The current category is $category";//print the category foreach($category as $food => $price) { echo "<br>The price of {$food} is {$price}";//print each item and its price } } </code> I would like to print the category names as well so how do I print the key values of a multidimensional array please. Any help is appreciated
  16. The special characters \n and \t do not have any affect on my script. Why would that be please?
  17. Thanks for your comments but it still does not solve my problem as the script does not want to run even after fixing the isues as mentioned in the beginning of this thread. Any help will be appreciated.
  18. @priti: I did what you said but I don't understand what you mean by Donation::$totalDonated = Donation::$totalDonated - $donation; //$donation is not a class property ! (this line) and after removing the destruct function it works but only prints one donor instead of 5. Am I missing something? @Muddy_Funster: I will make sure to give more detail and full error description plus put the code inside code tags. I am new at this but I learn fast thx. The book I have says for php5 but I also think it is a bit outdated. Can you recommend a good book? Also is there a website where I can find examples of php projects so I can practise creating them? Thank you for the help.
  19. Hi I am a complete beginner to php programming and I need to learn it fast as I am not a youngster anymore. I have a book called php and mysql and I am working through it. Creating static member variables I get an error Undefined class constant 'totalDonated' The variable totalDonated is in fact defined in the class definition and here is the code: <?php class Donation { private $name; private $amount; static $totalDonated = 0; static $numberOfDonors = 0; function info() { $share = 100 * $this->amount / Donation::$totalDonated; return "{$this->name} donated {$this->amount} ({$share}%)"; } function __construct($nameOfDonor, $donation) { $this->name = $nameOfDonor; $this->amount = $donation; Donation::$totalDonated = Donation::$totalDonated + $donation; Donation::$numberOfDonors++; } function __destruct() { Donation::$totalDonated = Donation::$totalDonated - $donation; Donation::$numberOfDonars--; } } ?> and here is the code to make use of the class: <?php include "include/donation.inc"; $donors = array( new Donation("Nicholas", 85), new Donation("Thomas", 55), new Donation("James", 78), new Donation("Jeff", 123), new Donation("Sarah", 444) ); foreach($donors as $donor) print $donor->info() . "<br"; $total = Donation::totalDonated; $count = Donation::numberOfDonors; print "Total donations = {$total}<br>"; print "Number of donors = {$count}<br>"; ?> So it makes no sense at all why I would get this error. Please if someone can help so I can learn this language as fast as possible. Much appreciated.
  20. I have the following php script and get data from a database and prints in on a page however it does not print a carriage return. Why can this be? The script: <?php include "database.inc"; mysql_select_db("php", $connection); $result = mysql_query("SELECT * FROM orders", $connection); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { foreach ($row as $attribute) print "($attribute)"; print "\n"; } ?>
  21. Please can someone explain to me what the keyword as mean in the context of: foreach ($row as $attribute) I am new to php and some things just need a little explaining please.
  22. I am new to this so please understand my ignorance. I get the following error and I cannot figure out what it means since the code comes directly from a book. Here is the error: Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in C:\wamp\www\learnPHP\inheritance.inc on line 13 Here is the code <?php require "unitCounter.inc"; class caseCounter extends unitCounter { (line 13) var unitsPerCase; function addCase() { $this->add($this->unitsPerCase); } function caseCount() { return ceil($this->units/$this->unitsPerCase); } function caseCounter($caseCapacity) { $this->unitsPerCase = $caseCpacity; } } ?>
  23. Thank you for the help. I gathered that much but I can't understand why they had it like that in the book. So the __destruct function should not be there at all or how do I fix this? Also the newline character is not printing why would that be?
  24. I am brand new to php and the following code gives me an error of undefined variable although it seems to be defined. Please tell what I'm doing wrong cause this code comes from a book: <?php // Definition of the class Donation class Donation { private $name; private $amount; static $totalDonated = 0; static $numberOfDonors = 0; // Function to display what each person has donated function info() { $share = 100 * $this->amount / Donation::$totalDonated; return "{$this->name} donated {$this->amount} ({$share}%)"; } function __construct($nameOfDonor, $donation) { $this->name = $nameOfDonor; $this->amount = $donation; Donation::$totalDonated = Donation::$totalDonated + $donation; Donation::$numberOfDonors++; } function __destruct() { 109) Donation::$totalDonated = Donation::$totalDonated - $donation; Donation::$numberOfDonors--; } } It gives me the following error: Notice: Undefined variable: donation in C:\wamp\www\learnPHP\unitCounter.inc on line 109
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.