Skip to content

Instantly share code, notes, and snippets.

@alysdal
Last active April 25, 2016 04:26
Show Gist options
  • Save alysdal/bf66eacfb788ebb3ea8a7d33f2b7c9cf to your computer and use it in GitHub Desktop.
Save alysdal/bf66eacfb788ebb3ea8a7d33f2b7c9cf to your computer and use it in GitHub Desktop.
PHP eksempler fra en workshop
<?php
// database credentials
$dbHost = 'localhost';
$dbName = 'webworkshop';
$dbUser = 'root';
$dbPass = '';
try {
// attempt to connect to the database
$db = new PDO('mysql:host=' . $dbHost . ';dbname=' . $dbName . ';charset=UTF8', $dbUser, $dbPass);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // fetch results to an associative array
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); // show warnings, useful for debugging
} catch(Exception $e) {
// if we get any errors, print them.
echo $e->getMessage();
exit;
}
?>
<h2>Foreach loop</h2>
<?php
$arr = [1,2,3,4,5,6,7,8,9,10];
foreach ($arr as $value) {
echo $value . " øl";
}
?>
<?php
function addNumbers($a, $b) {
return $a + $b;
}
echo "3 + 4 = " . addNumbers(3, 4);
// 3 + 4 = 7
function echo8times($text) {
for ($i = 0; $i < 8; $++) {
echo $text;
}
}
echo8times("hej, ");
// hej, hej, hej, ...
<?php
require_once("dbconnection.php");
$name = 'Noam Chomsky';
$lastlogin = date("Y-m-d g:i:s", time()); // "now" formatted date like 2016-04-20 12:00:00
// this sql statement contains a placeholder for the variables $name (:name) and $lastlogin
$sql = "INSERT INTO persons (name, lastlogin) VALUES (:name, :lastlogin)";
$statement = $db->prepare($sql);
// bind values to the SQL statement
$statement->bindValue(':name', $name);
$statement->bindValue(':lastlogin', $lastlogin);
// execute the sql statement
$statement->execute();
?>
<?php
// page caption
$caption = "What is PHP?";
if ($caption) {
$content = "PHP is a...";
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>PHP</title>
</head>
<body>
<h2><?php echo $caption; ?></h2>
<p>
<?php echo $content; ?>
</p>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>PHP</title>
</head>
<body>
<h2>What is PHP?</h2>
<p>
PHP is a...</p>
</body>
</html>
<?php
require_once("dbconnection.php");
$name = 'Chomsky';
$value = 800; // light intensity
$timestamp = date("Y-m-d g:i:s", time()); // formated date like 2016-04-20 12:00:00
$sql = "INSERT INTO sensors (id, name, value, timestamp) VALUES (NULL, :name, :value, :timestamp)";
$statement = $db->prepare($sql);
// bind values to the SQL statement
$statement->bindValue(':name', $name);
$statement->bindValue(':value', $value);
$statement->bindValue(':timestamp', $timestamp);
// execute the sql statement
$statement->execute();
?>
SELECT * FROM sensors WHERE name = :name ORDER BY timestamp DESC
<?php
require_once("functions.php");
require_once("header.php");
?>
<h2>Forside</h2>
<p>Lorem ipsum dolor sit amet, consectetur <br>
ipsum dolor sit amet, consectetur adipis...</p>
<?php
require_once("footer.php");
?>
<?php
require_once("dbconnection.php");
$sql = "SELECT * FROM persons";
$statement = $db->prepare($sql);
// execute the sql statement
$statement->execute();
// get the result in array format.
$results = $statement->fetchAll();
?>
<h2>Login historik</h2>
<ul>
<?php
foreach ($results as $row) {
echo "<li>";
echo $row['lastlogin'];
echo " -- ";
echo $row['name'];
echo "</li>";
}
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment