Jump to content

Diether

Members
  • Posts

    41
  • Joined

  • Last visited

Everything posted by Diether

  1. Hi guys Good day, how to insert multiple data in the database? when i insert data that has multiple orders, only one item is inserted in the database. pls help me to put this on loop. Heres the code i use: foreach ($_SESSION["cart_array"] as $each_items){ $item_id = $each_items['item_id']; $quantity = $each_items['quantity'] ; $sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'"); while($row = mysql_fetch_array($sql)){ $product_name = $row['name']; $price = $row['price']; $total_price = $price * $quantity; mysql_query("INSERT INTO customer_order( id,quantity,item_id, total_price,shipping_address, shipping_date,customer_id) VALUES ('','$quantity','$item_id','$total_price', '','', '$lastId')") or die (mysql_error()); } } Heres what i did but it gives me syntax error, pls help me to do the proper looping for this. thanks foreach ($_SESSION["cart_array"] as $each_items){ $item_id = $each_items['item_id']; $item_id_count = count($item_id) ; $quantity = $each_items['quantity'] ; $sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'"); while($row = mysql_fetch_array($sql)){ $product_name = $row['name']; $price = $row['price']; $total_price = $price * $quantity; foreach($i=0,$i < $item_id_count,$i++){ mysql_query("INSERT INTO customer_order( id,quantity,item_id, total_price,shipping_address, shipping_date,customer_id) VALUES ('','$quantity','$item_id','$total_price', '','', '$lastId')") or die (mysql_error()); } } }
  2. array masters.. please help me to solve my problem
  3. <?php ob_start(); ?> <?php session_start();?> <?php include("includes/functions.php");?> <?php error_rep(); ?> <?php require_once("includes/connection.php");?> <?php //Section 1 (if user attempts to add something to the cart from the product page) if (isset($_POST['pid'])) { $pid = $_POST['pid']; $wasFound = false; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $pid) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); } } redirect_to("cart.php"); exit(); } ?> <?php // Section 2 (if user chooses to empty their shopping cart) if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php // Section 3 (if user chooses to adjust item quantity) if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $item_to_adjust) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity))); } // close if condition } // close while loop } // close foreach loop } ?> <?php // Section 4 (if user wants to remove an item from cart) if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php // Section 5 (render the cart for the user to view on the page) $pp_checkout_btn =""; $cartOutput = ""; $cartTotal = ""; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM product WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $product_name = $row["name"]; $price = $row["price"]; $details = $row["details"]; } //$_SESSION["cart_array"][0]['pricetotal'] = $pricetotal = $price * $each_item['quantity']; $pricetotal = $price * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; echo $price ."<br/>"; echo $pricetotal ; $pricetotal = number_format($pricetotal,2); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> <input type="hidden" name="amount_' . $x . '" value="' . $price . '"> <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> '; // Create the product array variable // Dynamic table row assembly $cartOutput .= "<tr>"; $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /> <img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>'; $cartOutput .= '<td>' . $details . '</td>'; $cartOutput .= '<td>Php ' . $price . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> <input name="adjustBtn' . $item_id . '" type="submit" value="Update" /> <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> </form></td>'; //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>'; $cartOutput .= '<td>' . $pricetotal . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /> <input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; // $cartOutput .= '<td>' . $size . '</td>'; //experiment $cartOutput .= '</tr>'; $i++; } $productItems = $_SESSION["cart_array"] ; // foreach($productItems as $key => $productItem){ $newIndex = $key + 1; } $productItems[$newIndex]['pricetotal'] = $cartTotal ; // $cartTotal = number_format($cartTotal,2); $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." Php</div>"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Your Cart</title> </head> <body> <div id="wrapper"> <?php include("includes/header.php"); ?> <div id="mainMenu"> <!-- Start css3menu.com BODY section id=1 --> <ul id="css3menu1" class="topmenu"> <li class="topfirst"><a href="#" style="height:18px;line-height:18px;">Dashboard</a></li> <li class="topmenu"><a href="#" style="height:18px;line-height:18px;">order</a></li> <li class="topmenu"><a href="#" style="height:18px;line-height:18px;">Customer</a></li> <li class="topmenu"><a href="#" style="height:18px;line-height:18px;">Inventory</a></li> <li class="toplast"><a href="#" style="height:18px;line-height:18px;">Report</a></li> </ul> <p style="display:none"><a href="http://css3menu.com/">Easy CSS Drop Down Menu Css3Menu.com</a></p> <!-- End css3menu.com BODY section --> </div> <!-- end of mainmenu --> <div id="Buttons"> <a href = "index.php">Continue shopping </a><br/> <a href = "delivery_details.php">Checkout </a> </div> <!--buttons --> <div id="pageContent" style="margin:24px; text-align:left;"> <br /> <table width="100%" border="1" cellspacing="0" cellpadding="6"> <tr> <td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td> <td width="45%" bgcolor="#C5DFFA"><strong>Product Description</strong></td> <td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td> <td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td> <td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td> <td width="9%" bgcolor="#C5DFFA"><strong>Remove</strong></td> <td width="45%" bgcolor="#C5DFFA"><strong>size</strong></td> </tr> <?php echo $cartOutput; ?> </table> <?php echo $cartTotal; ?> <br /> <br /> <br /> <br /> <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a> </div><!-- End of pageContent --> <br /> <?php echo "<br/>"; // $_SESSION['newProductItems'] = $productItems; foreach($productItems as $key => $item) { if(isset($result[$key])) { $productItems[$key]['price'] = $result[$key]; } } echo "<pre>"; print_r($productItems); echo "</pre>"; ?> <?php include("includes/footer.php"); ?> <?php ob_flush(); ?> </div> <!--end of mainWrapper --> </body> </html> Here is the cart.php code.. the array is came from a session variable..
  4. Hi guys good day, im a student and i want to practice my php skills, i have a big problem in arrays that i cant solve almost a week so i decided to attach the php files as well as its database.pls help me so that i can study this later. i'm really having a hard time in arrays. please help me so that i can move on. There are 3 most important forms in my php files. 1. index.php 2.product.php 3. cart.php Here is my screenshot of what i want to do in cart.php. i want to include the unit price and the sumofprice in my array as indicated in my screenshot so that i can include this information in a session variable later on. My screenshot: My phpfiles : http://www.mediafire.com/?od2jdnd71v5xvud
  5. hi Guys Good day.. i have this two array that i want to combine: 1st array: when using print_r($productItems) Here is the result: Array ( [0] => Array ( [item_id] => 13 [quantity] => 4 ) [1] => Array ( [item_id] => 15 [quantity] => 1 ) [2] => Array ( [pricetotal] => 3709 ) ) 2nd array: When using print_r($prices) Here is the result: Array ( [price] => 3,200.00 ) Array ( [price] => 509.00 ) By using the array_merge, i came up with a solution: Here is what i did: $result = array_merge($productItems, $prices); print_r($result); Here is the output: Array ( [0] => Array ( [item_id] => 13 [quantity] => 4 ) [1] => Array ( [item_id] => 15 [quantity] => 1 ) [2] => Array ( [pricetotal] => 3709 ) [price] => 509.00 ) but thats not the result i want to get : this is my expected output: Array ( [0] => Array ( [item_id] => 13 [quantity] => 4 [price] => 3,200.00 ) [1] => Array ( [item_id] => 15 [quantity] => 1 [price] => 509.00 ) [2] => Array ( [pricetotal] => 3709 ) ) please help me to do this,, im stuck for almost 3 hours already thats why i seek for help. thanks
  6. <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { $product_list .= "<table><tr><td>Product ID</td><td>Items</td><td>Price</td><td>Date</td><td>Action</td></tr>"; while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $details = $row["details"]; $size = $row["size"]; $price = $row["price"]; $quantity = $row["quantity"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $product_list .= "<tr><td>$id</td> <td><strong>$product_name</strong></td> <td>Php$price</td> <td><em>$date_added</em></td> <td><a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br /></td></tr>"; } } else { $product_list = "<tr><td colspan=5>You have no products listed in your store yet</td></tr>"; } ?> this solve my problem a while ago.. hope it will help a beginner like me
  7. <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { $product_list .= "<table><tr><td>Product ID</td><td>Items</td><td>Price</td><td>Date</td><td>Action</td></tr>"; while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $details = $row["details"]; $size = $row["size"]; $price = $row["price"]; $quantity = $row["quantity"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $product_list .= "<tr><td>$id</td> <td><strong>$product_name</strong></td> <td>Php$price</td> <td><em>$date_added</em></td> <td><a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br /></td></tr>"; } } else { $product_list = "<tr><td colspan=5>You have no products listed in your store yet</td></tr>"; } ?> this solves my problem a while ago. hope it can help beginners
  8. what line are you pertaining that it gives me this kind of result?
  9. this code does not follow my desired output. any solution guys.? please help me to solve this
  10. Hi guys Good day, this codes works perfectly for me. <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $details = $row["details"]; $size = $row["size"]; $price = $row["price"]; $quantity = $row["quantity"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $product_list .= "Product ID: $id - <strong>$product_name</strong> - Php$price - <em>Added $date_added</em> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; } } else { $product_list = "You have no products listed in your store yet"; } ?> My problem is i want to refactor this part and put it into table in order to give a pleasant look: $product_list .= "Product ID: $id - <strong>$product_name</strong> - Php$price - <em>Added $date_added</em> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; My Screenshots for my desired output. Here is what i did to refactor but it did not give me a good result: $product_list .= "?> <html> <table > <tr> <td> Product ID: <?php echo $id ?> </td> <td> <?php echo $product_name; ?> </td> <td> Php <?php echo $price; ?> </td> <td> Added <?php echo $date_added; ?> </td> <td> <?php echo <a href='inventory_edit.php?pid=$id'>edit</a> • ?> </td> <td> <?php echo <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; ?> </td> </tr> </table> pls help me to solve my desired output,, thanks
  11. hi Guys, Pls give me an example code so that $pricetotal will not be null? just an example code so that i will have the idea on how to fix that. thanks
  12. Hi guys, this codes works perfectly to me, except that i would like to add pricetotal in session variable. my problem is pricetotal does not display values when i use print_r. Heres the output when i use print_r to display session variable: Array ( [0] => Array ( [item_id] => 15 [quantity] => 56 [pricetotal] => ) ) It should be like this: Array ( [0] => Array ( [item_id] => 15 [quantity] => 56 [pricetotal] => 5500) ) please help me to fix my problem: thanks the code use: <?php //Section 1 (if user attempts to add something to the cart from the product page) if (isset($_POST['pid'])) { $pid = $_POST['pid']; $wasFound = false; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1,"pricetotal" => $pricetotal)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $pid) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1,"pricetotal" => $pricetotal))); $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1, "pricetotal" => $pricetotal)); } } redirect_to("cart.php"); exit(); } ?> <?php // Section 2 (if user chooses to empty their shopping cart) if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php // Section 3 (if user chooses to adjust item quantity) if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $item_to_adjust) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity,"pricetotal" => $pricetotal))); } // close if condition } // close while loop } // close foreach loop } ?> <?php // Section 4 (if user wants to remove an item from cart) if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php // Section 5 (render the cart for the user to view on the page) $pp_checkout_btn =""; $cartOutput = ""; $cartTotal = ""; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM product WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $product_name = $row["name"]; $price = $row["price"]; $details = $row["details"]; } $pricetotal = $price * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; $pricetotal = number_format($pricetotal,2); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> <input type="hidden" name="amount_' . $x . '" value="' . $price . '"> <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> '; // Create the product array variable // Dynamic table row assembly $cartOutput .= "<tr>"; $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /> <img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>'; $cartOutput .= '<td>' . $details . '</td>'; $cartOutput .= '<td>Php ' . $price . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> <input name="adjustBtn' . $item_id . '" type="submit" value="Update" /> <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> </form></td>'; //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>'; $cartOutput .= '<td>' . $pricetotal . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /> <input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $cartOutput .= '</tr>'; $i++; } $cartTotal = number_format($cartTotal,2); $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." Php</div>"; } ?>
  13. pls help me to fix my codes, it took me two hours already and i can't move on
  14. Heres my problem. i have two data from same form that i want to insert in the different table in my database. 1. data came from the form( firstname,lastname,phonenumber etc.) 2. data came from session variable(item id, quantity, total etc) when the customer clicks the FINISH button, the data will be insert in the two separate tables in the database. Here's my screenshot of my form: http://img6.uploadho...96ab1f360bd.jpg that code above is the solution to my problem.
  15. hi guys Good day, , i have two data from same form that i want to insert in the different table in my database. 1. data came from the form( firstname,lastname,phonenumber etc.) 2. data came from session variable(item id, quantity, etc) im getting this error, please help me to solve this. thanks Notice: Undefined variable: itemId in C:\xampp\htdocs\sample\delivery_details.php on line 16 Notice: Undefined variable: qty in C:\xampp\htdocs\sample\delivery_details.php on line 17 Notice: Undefined variable: itemid in C:\xampp\htdocs\sample\delivery_details.php on line 19 Notice: Undefined variable: qty in C:\xampp\htdocs\sample\delivery_details.php on line 19 the code i use: <?php if (isset($_POST['submit'])){ $firstName = $_POST['firstName']; $lastName = $_POST['lastName']; $phoneNumber = $_POST['phoneNumber']; $emailAddress = $_POST['emailAddress']; $Address = $_POST['Address']; $query = mysql_query("INSERT INTO customer (id, first_name, last_name, phone_number,email_address,address) VALUES ('','$firstName', '$lastName',$phoneNumber,'$emailAddress','$Address')"); $getID = mysql_query("SELECT id FROM customer where first_name = '$firstName' and last_name = '$lastName'"); $num_row = mysql_num_rows($getID); if($num_row !=0){ while($row = mysql_fetch_array($getID)){ $_SESSION["cart_array"]["item_id"] = $itemId; $_SESSION["cart_array"]["quantity"] = $qty; $sql1 = mysql_query("INSERT INTO order (id,quantity,item_id) VALUES ('',$itemid,$qty )"); } } } ?>
  16. Hi guys, Pls give me advice. Heres my problem. i have two data from same form(cart.php) that i want to insert in the different table in my database. 1. data came from the form( firstname,lastname,phonenumber etc.) 2. data came from session variable(item id, quantity, total etc) when the customer clicks the FINISH button, the data will be insert in the two separate tables in the database. pls give me an idea or sample codes on how to do that, thanks Here's my screenshot of my form: http://img6.uploadhouse.com/fileuploads/17206/17206849acfcfc9c2d269f15d122d96ab1f360bd.jpg
  17. Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'DB_SERVER' (11004) inC:\xampp\htdocs\photo_exercises\includes\database.php on line 12 Database connection failed: Unknown MySQL server host 'DB_SERVER' (11004) it gives me this error now. no, they are not in the same folder, pls help me on how to solve this.
  18. hi guys ,, i am studying OOP in php until i encounter this error : Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'DB_SERVER' (11004) in C:\xampp\htdocs\photo_exercises\includes\database.php on line 12 Fatal error: Cannot access empty property in C:\xampp\htdocs\photo_exercises\includes\database.php on line 13 i use the following code: *database.php <?php require_once("config.php"); class MYSQLDatabase{ private $connection; function __construct() { $this->open_connection(); } public function open_connection(){ $connection =mysql_connect(DB_SERVER, DB_USER, DB_PASS); if(!$this->$connection){ die("Database connection failed: ".mysql_error()); }else{ $db_select = mysql_select_db(DB_NAME, $this->connection); if(!$db_select){ die("Database selection failed: " .mysql_error()); } } } public function close_connection(){ if(isset($this->$connection)){ mysql_close($connection); unset($this->$connection); } } public function query($sql){ $result = mysql_query($sql,$this->connection); $this->confirm_query($result); return $result; } public function mysql_prep( $value ) { $magic_quotes_active = get_magic_quotes_gpc(); $new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0 if( $new_enough_php ) { // PHP v4.3.0 or higher // undo any magic quote effects so mysql_real_escape_string can do the work if( $magic_quotes_active ) { $value = stripslashes( $value ); } $value = mysql_real_escape_string( $value ); } else { // before PHP v4.3.0 // if magic quotes aren't already on then add slashes manually if( !$magic_quotes_active ) { $value = addslashes( $value ); } // if magic quotes are active, then the slashes already exist } return $value; } private function confirm_query($result){ if(!result){ die("database query failed: " .mysql_error()); } } } $database = new MYSQLDatabase(); $database->close_connection(); ?> *config.php <?php //if is not define then do nothing otherwise take a second and define it // Database Constants defined('DB_SERVER') ? null : define("DB_SERVER", "localhost"); defined('DB_USER') ? null : define("DB_USER", "root"); defined('DB_PASS') ? null : define("DB_PASS", ""); defined('DB_NAME') ? null : define("DB_NAME", "photos"); *index.php ?php require_once("../includes/database.php"); if(isset($database)){ echo "true"; }else{ echo "false"; } echo "<br/>"; echo $database->mysql_prep("it's working? <br/>"); ?>
  19. up for my thread pls help me for my self study
  20. Hi guys Good day. i want to print the id of navigation links for testing but i dont get any response. Pls help me guys to solve my problem. Thanks in advance content.php <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php");?> <?php include_once("includes/header.php");?> <?php if (isset($_GET['subj'])){ $sel_subj = $_GET['subj']; $sel_page = ""; } elseif (isset($_GET['page'])) { $sel_subj = ""; $sel_page = $_GET['']; }else{ $sel_subj = ""; $sel_page = ""; } ?> <table id = "structure"> <tr> <td id = "navigation"> <ul class ="subjects"> <?php $subject_set = get_all_subjects(); while ($subject = mysql_fetch_array($subject_set)) { echo "<li><a href = \"content.php?subj = ". urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a></li>"; $page_set = get_page_for_subject($subject["id"]); echo "<ul class = \"pages\" >"; while ($page = mysql_fetch_array($page_set)) { echo "<li><a href = \"content.php?page = " . urlencode($page["id"]) . " \">{$page["menu_name"] }</a> </li>"; } echo "</ul>"; } ?> </ul> </td> <td id = "page"> <h2>Content Area</h2> <?php echo $sel_subj; ?></br> <?php echo $sel_page ; ?> </td> </tr> </table> <?php require("includes/footer.php"); ?> functions.php <?php function confirm_query($result_set){ if (!$result_set) { die("Database query failed: " . mysql_error()); } } function get_all_subjects(){ global $connection; $query = "SELECT * FROM subjects ORDER BY position ASC"; $subject_set = mysql_query($query, $connection); confirm_query($subject_set); return $subject_set; } function get_page_for_subject($subject_id){ global $connection; $query = "SELECT * FROM pages WHERE subject_id = {$subject_id} ORDER BY position ASC"; $page_set = mysql_query($query, $connection); confirm_query($page_set); return $page_set; } ?> this is the part of my code that i want to print but no response when click the links <?php if (isset($_GET['subj'])){ $sel_subj = $_GET['subj']; $sel_page = ""; } elseif (isset($_GET['page'])) { $sel_subj = ""; $sel_page = $_GET['']; }else{ $sel_subj = ""; $sel_page = ""; } ?> <?php echo $sel_subj; ?></br> <?php echo $sel_page ; ?>
  21. if (isset($_POST['item_to_adjust']) { $item_to_adjust =(int) $_POST['item_to_adjust']; ////////////////////////////////// // some codes to adjust the cart// ////////////////////////////////// header("location : cart.php"); //kill the script exit(); } ChristianF.. Am I in the right track now? my problem now is the code that would adjust the cart? i wish i can make this work. regards
  22. thanks men. Can you help to solve my problem on how to simplify this line ? <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 3 (if user chooses to adjust item quantity) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $item_to_adjust) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity))); } // close if condition } // close while loop } // close foreach loop } ?>
  23. i will try this.. then i will leave comment later if i got the right answer. thanks anyway,
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.