Friday, April 15, 2011

Friday, April 15th 2011


<?php
//create short variable names
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];

if (!$isbn || !$author || !$title || !$price) {
echo "You have not entered all the required details.<br /> Please go back and try again.";
exit;
}

//I think that i have get magic quotes off.
if (!get_magic_quotes_gpc()) {
$isbn = addslashes($isbn);
$author = addslashes($author);
$title = addslashes($title);
$price = doubleval($price);
}

@ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');

if (mysqli_connect_errno()){
echo "Error: Could not connect to database. Please try again later.";
exit;
}

$query = "insert into books values ('" . $isbn . "', '" . $author . "', '" . $title . "', '" . $price . "')";
$result = $db->query($query);

if ($result) {
echo $db->affected_rows . " book inserted into database . ";
} else {
echo "An error has occurred. The item was not added.";
}

$db->close();
?>




<?php
$dogs = array('Lassie' => 'Collie', 'Bud' => 'Sheepdog', 'Rin-Tin-Tin' => 'Alsatian');

$birds = array('parrot', 'magpie', 'lorikeet', 'cuckoo');

printf("

There are %d dogs and %d birds.

", count($dogs), count($birds));

$birds[] = 'ibis';
printf("<p>There are now %d birds:

", count($birds));
printf("<pre>%s</pre>\n", var_export($birds, TRUE));

$birds[10] = 'heron';
unset($birds[3]);
?>





<?php
$birds = array('parrot', 'magpie', 'lorikeet', 'cuckoo');
$more_birds = array_pad($birds, 6, 'some bird');

printf("<p>Birds:</p><pre>%s</pre>\n", var_export($birds, TRUE));
printf("<p>More birds:

%s</pre>\n", var_export($more_birds, TRUE));

?>


Here is another tutorial dealing with arrays



<?php
$birds = array('parrot', 'magpie', 'lorikeet', 'cuckoo');
$more_birds = array_pad($birds, 6, 'some bird');

printf("<p>Birds:</p><pre>%s</pre>\n", var_export($birds, TRUE));
printf("<p>More birds:</p>%s</pre>\n", var_export($more_birds, TRUE));

?>




<?php
$dogs = array('Lassie' => 'Collie', 'Bud' => 'Sheepdog', 'Rin-Tin-Tin' => 'Alsatian');

$pups = array_pad($dogs, 5, 'mutt');
printf("<p>Pups (left padding):</p><pre>%s</pre>\n", var_export($pups, TRUE));

$pups = array_pad($dogs, -5, 'mutt');
printf("<p>Pups (left padding):</p><pre>%s</pre>\n", var_export($pups, TRUE));

printf("<p>Dogs:</p><pre>%s</pre>\n", var_export($dogs, TRUE));

?>


Here is an example dealing with foreach looping


<?php
$dogs = array('Lassie' => 'Collie', 'Bud' => 'Sheepdog', 'Rin-Tin-Tin' => 'German Shepard', 'Snoopy' => 'Beagle');

foreach($dogs as $name => $breed)
print "$name is a $breed.<br/>\n";

$birds = array('parrot', 'magpie', 'lorikeet', 'cuckoo');

foreach($birds as $bird)
print "$bird";

print "<br/>";

$birds[] = 'ibis';
$birds[10] = 'heron';
unset($birds[3]);

foreach($birds as $bird)
print "$bird ";
?>

No comments: