Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active March 25, 2024 04:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save code-boxx/88304ef6f002a678ee6b6950a500cdcf to your computer and use it in GitHub Desktop.
Save code-boxx/88304ef6f002a678ee6b6950a500cdcf to your computer and use it in GitHub Desktop.
HTML CSS Javascript PHP MySQL To Build A Website

USE HTML CS JS PHP MYSQL TO BUILD A WEBSITE

https://code-boxx.com/use-html-css-javascript-php-mysql/

IMAGES

book

NOTES

  1. This tutorial is not a "5 mins web development crash course". Please go through the proper basic HTML/CSS/Javascript tutorials.
  2. Create a dummy database and import 1-products.sql.
  3. Change the database settings in 2-products.php to your own.
  4. Launch 3-html-page.php in your web browser for the demo.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) PRODUCTS TABLE
CREATE TABLE `products` (
`product_id` bigint(20) NOT NULL,
`product_name` varchar(128) NOT NULL,
`product_description` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `products`
ADD PRIMARY KEY (`product_id`),
ADD KEY `product_name` (`product_name`);
ALTER TABLE `products`
MODIFY `product_id` bigint(20) NOT NULL AUTO_INCREMENT;
-- (B) DUMMY PRODUCTS
INSERT INTO `products` (`product_id`, `product_name`, `product_description`) VALUES
(1, 'Vulture Of Fortune', 'A sweated bump consents across the here separator.'),
(2, 'Guardian Without Duty', 'Does a table migrate inside an excessive paranoid?'),
(3, 'Enemies Without Hope', 'A cured parameter fears behind the phenomenon.'),
(4, 'Lords Of The Void', 'The diary scores around the generalized lie.'),
(5, 'Doctors And Aliens', 'The diary scores around the generalized lie.'),
(6, 'Blacksmiths And Criminals', 'A considerable snail works into a purchase.');
<?php
// (A) DATABASE SETTINGS - CHANGE TO YOUR OWN !
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (B) CONNECT TO DATABASE
$pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_NAMED
]);
// (C) GET PRODUCTS
$stmt = $pdo->prepare("SELECT * FROM `products`");
$stmt->execute();
$products = $stmt->fetchAll();
<!DOCTYPE html>
<html>
<head>
<title>Products Demo Page</title>
<!-- (A) CSS & JS -->
<link href="4-style.css" rel="stylesheet">
<script src="5-script.js"></script>
</head>
<body>
<!-- (B) BOOKS LIST -->
<div id="books"><?php
require "2-products.php";
foreach ($products as $p) { ?>
<div class="bookWrap" data-id="<?=$p["product_id"]?>">
<img class="bookImg" src="book.png">
<div class="bookTitle"><?=$p["product_name"]?></div>
<div class="bookDesc"><?=$p["product_description"]?></div>
</div>
<?php }
?></div>
</body>
</html>
/* (A) WHOLE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body {
max-width: 600px;
padding: 10px;
margin: 0 auto;
background: #f2f2f2;
}
/* (B) BOOKS */
#books {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-gap: 10px;
}
.bookWrap {
border: 1px solid #dbdbdb;
background: #fff;
}
.bookImg {
display: block;
width: 100%;
}
.bookTitle, .bookDesc { padding: 10px; }
.bookTitle {
font-weight: 700;
font-size: 16px;
color: #333;
}
.bookDesc {
padding-top: 0;
font-size: 14px;
color: #848484;
}
window.addEventListener("load", () =>{
// (A) GET ALL BOOKS
var books = document.querySelectorAll(".bookWrap");
// (B) ATTACH ONCLICK TO ALL BOOKS
for (let book of books) {
book.onclick = () => {
// (B1) GET SELECTED BOOK ID, NAME, DESCRIPTION
var id = book.dataset.id,
name = book.querySelector(".bookTitle").innerHTML,
desc = book.querySelector(".bookDesc").innerHTML;
// (B2) SHOW IN DIALOG BOX
alert(`You have selected - ID: ${id}, TITLE: ${name} DESC: ${desc}`);
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment