hello!

i'm a super newbie at php i'm trying to learn some scripts, what im trying to do now is a
simple shop system.

i have a table that contains item_id , item_name, price, quantity

what i want to do is that a customer will choose an item, how many will he like and compute the total amount, after a customer bought some items i want the quantity will auto update after purchase

thanks!

A good plan. Now go ahead and do it. If you encounter any problems, come back with them.

hello!

i'm a super newbie at php i'm trying to learn some scripts, what im trying to do now is a
simple shop system.

i have a table that contains item_id , item_name, price, quantity

what i want to do is that a customer will choose an item, how many will he like and compute the total amount, after a customer bought some items i want the quantity will auto update after purchase

thanks!

Make a dropdown list which will contain the item_id using the following code

<select id="items">
   <option value="0" onchange='getItems();'>- Select an Item -</option>
   <?php
      $res = mysql_query("select item_id from table");
      while ($r = mysql_fetch_array($res))
      {	
	echo "<option value=".$r['item_id'].">".$r['item_id']."</option>\n";
      }
    ?>
</select>

<script type="text/javascript">
function getItems()
{
     var item_id = document.getElementsById('items').value;
}

</script>

Now make three TextBoxes. These will contain the item_name, price, quantity for a particular item_id.

Are you familiar with a word AJAX ??? If so then i will show you further..

sorry bout that i was just trying to see a simple code on how i could update an inventory so i can analyze it and apply it on my scripts

here my source codes

This is my home.php

<?php
	session_start();
	require_once("include/connect.php");
	if(isset($_GET['page'])) {
		$page = array("products" , "cart");
		if(in_array($_GET['page'] , $page)) {
			$page = $_GET['page'];
		} else {
			$page = "products";
		}
		} else {
			$page = "products";
		}
		?>

<html>
<head>
	<link rel="stylesheet" href="css/reset.css" />
	<link rel="stylesheet" href="css/style.css" />
	


</head>

<body>

	<div id="container">
		<div id="main"> <?php require($page . ".php"); ?></div>
		<div id="sidebar">
		
		
		<h1><br> &nbsp;&nbsp;Books to be Purchase </h1><br><br><br>
			<b><?php
				if(isset($_SESSION['cart'])) {
				$sql = "SELECT * FROM products WHERE id_products IN (";
				foreach($_SESSION['cart'] as $id=>$value) {
					$sql .=$id . ",";
				}
				
				$sql = substr($sql,0,-1) . ") ORDER BY id_products ASC ";
				$query = mysql_query ($sql);
				if(!empty($query)) {
				while($row = mysql_fetch_assoc($query)){
				
		?>
			
		<p> &nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['name']; ?> <?php echo "x" . $_SESSION['cart'][$row['id_products']]
			['quantity']; ?> </p>
			
			<?php 
				}
				} else {
				echo "<i><center>You need to add an item <br /> <br> for it <br><br> to be visible here</i><br><br></center>";
			
			
			
				}
				} else {
					echo "<p> &nbsp;&nbsp;&nbsp;&nbsp;Your Cart is Empty . <br /> &nbsp;&nbsp;&nbsp;&nbsp;Please Add Some Products </p>";
					}
					
					echo "<br><a href='home.php?page=cart'> &nbsp;&nbsp;&nbsp;&nbsp;Go to Cart </a><br><br>
					<form method='post' action='home2.php'>
	<input type='submit' value='Go Back' /></form>"
					?> <b>
		<br><br><br><br><br>

		
		</div>
	</div>	
	
	

</body>

</html>

now my products.php

<?php	

	if(isset($_GET['action']) && $_GET['action'] == "add") {
		$id = intval($_GET['id']);
		if (isset($_SESSION['cart']  [$id])) {
		$_SESSION ['cart'][$id]['quantity']++;
	} else {
	$sql2 = "SELECT * FROM products WHERE id_products=($id)";
	$query2=mysql_query($sql2);
	if (mysql_num_rows($query2) !=0) {
	$row2=mysql_fetch_array($query2);
	$_SESSION['cart'][$row2['id_products']] = array("quantity"=> 1, "price" =>$row2['price']);
	} else {
	$message = "This product id is invalid";
	}
	}
	}
	?>


<h2 class="message">
  <?php if(isset($message)) {echo $message;} ?>
</h2>

<h1>List of Books</h1>

<center>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<table>
		<tr>
			<th>Subject Code&nbsp;&nbsp;</th>
			<th>Title of the Book&nbsp;&nbsp;</th>
			<th>Price&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
			<th align="center">Action&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
			
		</tr>
		
	
			
	<?php 
		$sql = "SELECT * FROM products  ORDER BY  name ASC";
		$query = mysql_query($sql);
		while ( $row=mysql_fetch_assoc($query)) {
	?>
		<tr>
			
				<td height="21"><?php echo $row['name']; ?></td>
				<td><?php echo $row['description']; ?></td>
				<td>P <?php echo $row['price']; ?></td>
				<td><center><a href="home.php?page=products&action=add&id=<?php echo $row['id_products']; ?>">Add  </center></a></td>
				
				
		
	</tr>
			
	<?php
	
		}
		
		?>
			
			
			
		
			

</table></center>

     <div align="center"></div>

and my cart.php where the computations work

<?php
	if(isset($_POST['submit'])) {
	foreach($_POST as $key => $value) {
		$key = explode("-" , $key);
		$key = end($key);
		$key = explode("submit",$key );
		$key = end($key);
		
		if($_POST['quantity-' .$key] <= 0) {
		 	unset($_SESSION['cart'][$key]);
		} else {
			$_SESSION['cart'][$key]['quantity'] = $value;
			}
			}
		} error_reporting(0);
	?>

	
<h1>View Cart</h1>
	<a href="home.php?page=products" title="Go back to products page"> Go back to products page </a><br><br>
	<?php $sql = " SELECT * FROM products WHERE id_products IN (";
	foreach ($_SESSION['cart'] as $id => $value) {
			$sql .= $id. ",";
		}
		$sql = substr($sql,0,-1). ") ORDER BY name ASC";
		$query = mysql_query($sql);
		if(empty($query)) {
		echo "<br /><span class='i'> You need to add an item before you can view it here </span><br><br><br><br>" ;
		}
		?>
		
<form method="post" action="home.php?page=cart">
<fieldset>
		<table>
			<tr>
				<th>Name</th>
				<th>Quantity</th>
				<th>Price per item</th>
				<th>Total Cost</th>
			</tr>
			
			
	<?php
	 
		$sql = "SELECT * FROM products WHERE id_products IN (";
		foreach ($_SESSION['cart'] as $id => $value ) {
			$sql .= $id . ",";
		}
		
		$sql = substr($sql,0,-1) . ") ORDER BY name ASC";
		$query = mysql_query($sql);
		$total_price = 0;
		if(!empty($query)) {
		while($row = mysql_fetch_array($query)) {
			$subtotal = $_SESSION['cart'][$row['id_products']]['quantity']*$row['price'];
			$total_price += $subtotal;
			?>
			
	<tr>
		<td><?php echo $row['name']; ?></td>
		<td><input type="text" name="quantity-<?php echo $row['id_products']; ?>" size="5" value="<?php echo $_SESSION['cart'][$row['id_products']]['quantity']; ?>" /> </td>
		<td><?php echo "$" . $row['price']; ?></td>
		<td><?php echo "$" . $_SESSION['cart'][$row['id_products']]['quantity']*$row['price']; ?></td>
	</tr>
	
	<?php
		} }
		?>
		
	<tr>
		<td></td>
		<td></td>
		<td>Total Price:</td>
		<td><?php echo "P" . $total_price; ?></td>
	</tr>
	</table><br><br>
	
<input type="submit" name="submit" value="Update Cart" />

<fieldset>
</form><br><br>
<form method="post">
	<input type="submit" value="Buy Items" /></form><br /><br /><br />

<p>To remove an item, set quantity to 0</p>

now i like to learn how it can auto update the quantity on the database that's why i haven't created the next form yet

What is the "quantity"? Is this the number of items in stock? Then you should have a trigger in your database which decreases the number of items in stock when an item is sold. Learn about mysql triggers. Solve this on the database level, not in PHP.

sorry i thought it will happen like this, i will call the "quantity" in the database base on the item selected, construct an if statement , make computations , and replace the "quantity" with the new quantity after the computation (like editing the information from the database).

Thanks for the info!

Always add a field to store quantity of a product available. Every time, a user purchase something, you need to run the query to update the available quantity of that product in the database table.

question, when the quantity reaches zero can i be able to do a statement that won't let the item be added to the list?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.