Jump to content

Recommended Posts

I recently bought 2 books from Site Point and there is a bit of code that I do not understand, since the books doesn't explain it. I'm hoping one of you smart php guys can explain it to me.
The part of the code I do not understand the very next line after the form, this line:

[code]<?php else: // Default page display[/code]

Why is there a ([b]:[/b]) instead of a { .
And why is it that at the end, we must end it if an [b]endif;[/b] I never seen that one neither.


Another thing I find stupid is that everytime I hit refreshed, the last joke it's added again to the database, which makes no sense to me. How could I eliminate that from happeing over and over.

Would any of you care to explain to me in plain english why it has to be done like that?
Thanks for your help in advance.



[code]<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome to Jokes DB!</title>
</head>

<body>
<?php
    if(isset($_GET['addjoke'])): //User wants to add a joke
    
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Type your joke here:<br/>
<textarea name="joketext" rows="10" cols="40"></textarea></label><br/>
<input type="submit" value="SUBMIT" />
</form>

<?php else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'user', 'pass');
if (!$dbcnx) {
    exit('<p> Unable to connect to the database server at this time.</p>');
}

// Select your jokes database
if (!@mysql_select_db('joke')) {
exit('<p>Unable to locate the joke database at this time.</p>');
}

// If a joke has been submited, add it to the database
if (isset($_POST['joketext'])) {
$joketext = $_POST['joketext'];
$sql ="INSERT INTO ijdb SET joketext='$joketext', jokedate=CURDATE()";

  if (@mysql_query($sql)) {
  echo '<p>Your joke has been added</p>';
  } else {
     echo '<p>Error adding submitted joke: '.mysql_error(). '</p>';
    }
}

echo '<p>Here are all the jokes in our database';

//Request the text of all jokes
$result = @mysql_query('SELECT joketext FROM ijdb');
if(!$result) {
    exit('<p>Error performing the query: ' .mysql_error(). '</p>');
}


//Display the text of each joke in a paragraph

while ($row = mysql_fetch_array($result)) {
echo '<p>'.$row['joketext']. '</p>';
echo '<em>'.$row['jokedate']. '</em>';
}

/// When clicked , this link will load this page within the joke submission form displayed.

echo '<p><a href="' .$_SERVER['PHP_SELF']. '?addjoke=true">Add a Joke!</a></p>';
endif;
?>
</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/11516-need-help-understaing-this-script/
Share on other sites

In answer to your first question, using the ":" is an alternative syntax for control structures. See [a href=\"http://us3.php.net/manual/en/control-structures.alternative-syntax.php\" target=\"_blank\"]this[/a] page in the fine manual.

Ken
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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