私は2つのファイルを持っています。index.php と cart.php。すべての関数、データベースへの接続などは、cart.php ファイルにあります。ユーザーが HREF リンクをクリックした場合に呼び出したい関数があります。
私のindex.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div id="wrapper" align="center" style="width:90%; height:auto; margin-left:auto; margin-right:auto;">
<?php cart(); ?>
<br><br>
<div id="sidebar" align="left" style="width:15%; height:auto; background-color:#999999; float:left;">
<ul style="list-style-type:none;">
<li><a href="#" id="all">ALL</a></li>
<li><a href="#" id="shirts">SHIRTS</a></li>
<li><a href="#" id="hoodies">HOODIES</a></li>
</ul>
</div>
<div id="products" style="width:85%; height:auto; background-color:#888888; float:left;">
<?php
products_all();
?>
</div>
</div>
</body>
</html>
次に、HREF リンクが特定の関数 (cart.php にある) を呼び出し、それを div タグ内 (私の場合は div id="products") に表示するようにするにはどうすればよいですか?
おそらく簡単です、私は初心者です。
念のため、ここに私のcart.phpがあります。
<?php
session_start();
$page = 'index.php';
// ***
/*
$mysql_host = "***";
$mysql_database = "***";
$mysql_user = "***";
$mysql_password = "***";
*/
// localhost
$mysql_host = "localhost";
$mysql_database = "cartt";
$mysql_user = "root";
$mysql_password = "";
mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
mysql_select_db($mysql_database) or die(mysql_error());
if (isset($_GET['add'])) {
$quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
while ($quantity_row = mysql_fetch_assoc($quantity)) {
if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) {
$_SESSION['cart_'.(int)$_GET['add']]+='1';
}
}
header('Location: '.$page);
}
if (isset($_GET['remove'])) {
$_SESSION['cart_'.(int)$_GET['remove']]--;
header('Location: '.$page);
}
if (isset($_GET['delete'])) {
$_SESSION['cart_'.(int)$_GET['delete']]='0';
header('Location: '.$page);
}
function products_all() {
$get_all = mysql_query('SELECT id, name, description, price FROM products ORDER BY id DESC');
if (mysql_num_rows($get_all)==0) {
echo "There are no products to display!";
}
else {
echo '<table width="900px" allign="center" cellpadding ="5" style="background-color:transparent;">
<tr align="center" valign="middle">';
$i = 0;
while ($get_row = mysql_fetch_assoc($get_all)) {
echo '<td><img src="./cartimages/'.$get_row['id'].'.jpeg" alt=" " height="225px" align="center"><br />'.$get_row['name'].'<br />'.$get_row['description'].'<br />£'.number_format($get_row['price'], 2).'<br /><a href="cart.php?add='.$get_row['id'].'">Add</a></td>';
$i++;
if ($i == 4) {
echo '</tr>
<tr align="center" valign="middle">';
$i = 0;
}
}
}
}
function products_shirts() {
$get_shirts = mysql_query('SELECT id, name, description, price FROM products WHERE type = "shirt" ORDER BY id DESC');
if (mysql_num_rows($get_shirts)==0) {
echo "There are no products to display!";
}
else {
echo '<table width="900px" allign="center" cellpadding ="5" style="background-color:transparent;">
<tr align="center" valign="middle">';
$i = 0;
while ($get_row = mysql_fetch_assoc($get_shirts)) {
echo '<td><img src="./cartimages/'.$get_row['id'].'.jpeg" alt=" " height="225px" align="center"><br />'.$get_row['name'].'<br />'.$get_row['description'].'<br />£'.number_format($get_row['price'], 2).'<br /><a href="cart.php?add='.$get_row['id'].'">Add</a></td>';
$i++;
if ($i == 4) {
echo '</tr>
<tr align="center" valign="middle">';
$i = 0;
}
}
}
}
function products_hoodies() {
$get_hoodies = mysql_query('SELECT id, name, description, price FROM products where type = "hoodie" ORDER BY id DESC');
if (mysql_num_rows($get_hoodies)==0) {
echo "There are no products to display!";
}
else {
echo '<table width="900px" allign="center" cellpadding ="5" style="background-color:transparent;">
<tr align="center" valign="middle">';
$i = 0;
while ($get_row = mysql_fetch_assoc($get_hoodies)) {
echo '<td><img src="./cartimages/'.$get_row['id'].'.jpeg" alt=" " height="225px" align="center"><br />'.$get_row['name'].'<br />'.$get_row['description'].'<br />£'.number_format($get_row['price'], 2).'<br /><a href="cart.php?add='.$get_row['id'].'">Add</a></td>';
$i++;
if ($i == 4) {
echo '</tr>
<tr align="center" valign="middle">';
$i = 0;
}
}
}
}
function paypal_items() {
$num = 0;
foreach($_SESSION as $name => $value) {
if ($value!=0) {
if (substr($name, 0 , 5)=='cart_') {
$id = substr($name, 5, strlen($name)-5);
$get = mysql_query('SELECT id, name, price, shipping FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$num++;
echo '<input type="hidden" name="item_number_'.$num.'" value="'.$id.'">';
echo '<input type="hidden" name="item_name_'.$num.'" value="'.$get_row['name'].'">';
echo '<input type="hidden" name="amount_'.$num.'" value="'.$get_row['price'].'">';
echo '<input type="hidden" name="shipping_'.$num.'" value="'.$get_row['shipping'].'">';
// echo '<input type="hidden" name="shipping2_'.$num.'" value="'.$get_row['shipping'].'">';
echo '<input type="hidden" name="quantity_'.$num.'" value="'.$value.'">';
}
}
}
}
}
function cart() {
$total = 0;
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_array($get)) {
$sub = $get_row['price']*$value;
echo $get_row['name'].' x '.$value.' @ £'.number_format($get_row['price'], 2).' = £'.number_format($sub, 2).'<a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br />';
}
}
$total += $sub;
}
}
if ($total==0) {
echo "Your cart is empty.";
}
else {
echo '<p>Total: £'.number_format($total, 2).'</p>';
?>
<p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="*************">
<?php paypal_items(); ?>
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="amount" value="<?php echo $total; ?>">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but03.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
</p>
<?php
}
}
?>