Jump to content

Archived

This topic is now archived and is closed to further replies.

Yojance

Need Help understaing this script

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]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Hey Ken, thanks for your link, I read and I still find it confusing. Im going to keep looking at it hoping I can get something out of it. If someone else has can explain it to me, I would really appreacite that.

Share this post


Link to post
Share on other sites

×

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.