Oggi vi voglio spiegare come creare un nezionio online senza usare CMS già pronti.
Allora inanzitutto, bisogna sapere, cosa si vuole vendere, una vo0lta deciso, aprite un programma di scrittura (notepad, o altro).
Create un file di nome index:
<html>
<head><title>Nome negozio</title></head>
<body>
</body>
</html>
Questo dovrebbe essere il codice sorgente iniziale, prima che subisca modifiche, ora, per continuare io vi consiglio di usare un editor html, per migliorare un po’ la grafica, inserire bottoni, immagini, testi ecc…
Ok, abbiamo finito di creare l’index, ora dobbiamo creare due file importantissimi:
–>cart.php che conterra i prodotti messi nel carrello;
–>catalog.php, che conterra i prodotti in vendita;
Apriamo il file cart.php e inseriamo per esteso il seguente codice:
<?php
session_start();
if (!isset($_SESSION[’cart’])) {
$_SESSION[’cart’] = array();
}
if (isset($_GET[’empty’])) {
// Empty the $_SESSION[’cart’] array
unset($_SESSION[’cart’]);
header(’location: ‘ . $_SERVER[’PHP_SELF’] . ‘?’ . SID);
exit();
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Shopping cart</title>
<meta http-equiv=”content-type”
content=”text/html; charset=iso-8859-1″ />
</head>
<body>
<h1>Your Shopping Cart</h1>
<?php
$items = array(
‘Canadian-Australian Dictionary’,
‘As-new parachute (never opened)’,
‘Songs of the Goldfish (2CD Set)’,
‘Ending PHP4 (O\’Wroxey Press)’);
$prices = array( 24.95, 1000, 19.99, 34.95 );
?>
<table border=”1″>
<thead>
<tr>
<th>Item Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
$total = 0;
for ($i = 0; $i < count($_SESSION[’cart’]); $i++) {
echo ‘<tr>’;
echo ‘<td>’ . $items[$_SESSION[’cart’][$i]] . ‘</td>’;
echo ‘<td align=”right”>$’;
echo number_format($prices[$_SESSION[’cart’][$i]], 2);
echo ‘</td>’;
echo ‘</tr>’;
$total = $total + $prices[$_SESSION[’cart’][$i]];
}
?>
</tbody>
<tfoot>
<tr>
<th align=”right”>Total:</th><br>
<th align=”right”>$<?php echo number_format($total, 2); ?></th>
</tr>
</tfoot>
</table>
<p><a href=”catalog.php”>Continue Shopping</a> or
<a href=”<?php echo $_SERVER[’PHP_SELF’]; ?>?empty=1″>Empty your cart</a></p>
</body>
</html>
Salviamo e chiudiamo.
poi apriamo il file catalog.php ed inseriamo il seguente codice:
<?php
session_start();
if (!isset($_SESSION[’cart’])) {
$_SESSION[’cart’] = array();
}
if (isset($_GET[’buy’])) {
// Add item to the end of the $_SESSION[’cart’] array
$_SESSION[’cart’][] = $_GET[’buy’];
header(’location: ‘ . $_SERVER[’PHP_SELF’] . ‘?’ . SID);
exit();
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Product catalog</title>
<meta http-equiv=”content-type”
content=”text/html; charset=iso-8859-1″ />
</head>
<body>
<p>Your shopping cart contains <?php echo count($_SESSION[’cart’]); ?> items.</p>
<p><a href=”cart.php”>View your cart</a></p>
<?php
$items = array(
‘Canadian-Australian Dictionary’,
‘As-new parachute (never opened)’,
‘Songs of the Goldfish (2CD Set)’,
‘Ending PHP4 (O\’Wroxey Press)’);
$prices = array(24.95, 1000, 19.99, 34.95);
?>
<table border=”1″>
<thead>
<tr>
<th>Item Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
for ($i = 0; $i < count($items); $i++) {
echo ‘<tr>’;
echo ‘<td>’ . $items[$i] . ‘</td>’;
echo ‘<td>$’ . number_format($prices[$i], 2) . ‘</td>’;
echo ‘<td><a href=”‘ . $_SERVER[’PHP_SELF’] .
‘?buy=’ . $i . ‘”>Buy</a></td>’;
echo ‘</tr>’;
}
?>
</tbody>
</table>
<p>All prices are in imaginary dollars.</p>
</body>
</html>
Ora salviamo e chiudiamo.Con questo abbiamo finito, ifatti ci basterà inserire un piccolo link nell’homepage che porti al catalogo.
Il negozio non avrà sicuramente una visuale come oscommerce, però lo avrete creato voi!
Alla prossima, in cui inserirò altri particolari per migliorarlo!