Jump to content

vipsa

Members
  • Posts

    25
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

vipsa's Achievements

Member

Member (2/5)

0

Reputation

  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
×
×
  • 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.