Jump to content


Photo

Need Help understaing this script


  • Please log in to reply
2 replies to this topic

#1 Yojance

Yojance
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 08 June 2006 - 07:09 PM

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:

<?php else: // Default page display

Why is there a (:) instead of a { .
And why is it that at the end, we must end it if an endif; 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.



<!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>


#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 June 2006 - 07:36 PM

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

#3 Yojance

Yojance
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 08 June 2006 - 07:54 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users