PHP - How To Make My Print Button Only Appear If There Is Data..
so this is my code
Code: [Select] <? $b=$_GET['b']; $m=$_GET['m']; $y=$_GET['y']; if ($b <> '' && $m <> '' && $y <> '') { $branch=$b; $leavemonth=$m; $leaveyear=$y; } else { $branch=$_POST['branch']; $leavemonth=$_POST['leavemonth']; $leaveyear=$_POST['leaveyear']; } $connection=mysql_connect("$server", "$username", "$password") or die("Could not establish connection"); mysql_select_db($database_name, $connection) or die ("Could not select database"); $query="SELECT * from tblworkgroup"; $result=mysql_query($query);?> Branch <select name="branch" > <option value="all">ALL Branches</option> <? while($row=mysql_fetch_array($result)) { ?> <option value= "<?=$row['WorkGroupID']?>" <? if ($branch==$row['WorkGroupID']){ echo 'selected'; } ?> ><?=$row['WorkGroupName']?></option> <? } ?> </select> </td> </tr> <br><br><br> Month <select name="leavemonth" > <option value = "1" <? if ($leavemonth==1){ echo 'selected'; } ?>>January</option> <option value = "2" <? if ($leavemonth==2){ echo 'selected'; } ?>>February</option> <option value = "3" <? if ($leavemonth==3){ echo 'selected'; } ?>>March</option> <option value = "4" <? if ($leavemonth==4){ echo 'selected'; } ?>>April</option> <option value = "5" <? if ($leavemonth==5){ echo 'selected'; } ?>>May</option> <option value = "6" <? if ($leavemonth==6){ echo 'selected'; } ?>>June</option> <option value = "7" <? if ($leavemonth==7){ echo 'selected'; } ?>>July</option> <option value = "8" <? if ($leavemonth==8){ echo 'selected'; } ?>>August</option> <option value = "9" <? if ($leavemonth==9){ echo 'selected'; } ?>>September</option> <option value = "10" <? if ($leavemonth==10){ echo 'selected'; } ?>>October</option> <option value = "11" <? if ($leavemonth==11){ echo 'selected'; } ?>>November</option> <option value = "12" <? if ($leavemonth==12){ echo 'selected'; } ?>>December</option> </select> Year <select name="leaveyear" > <option value = "2010" <? if ($leaveyear==2010) { echo 'selected';} ?>>2010</option> <option value = "2011" <? if ($leaveyear==2011) { echo 'selected';} ?>>2011</option> <option value = "2012" <? if ($leaveyear==2012) { echo 'selected';} ?>>2012</option> <option value = "2013" <? if ($leaveyear==2013) { echo 'selected';} ?>>2013</option> <option value = "2014" <? if ($leaveyear==2014) { echo 'selected';} ?>>2014</option> <option value = "2015" <? if ($leaveyear==2015) { echo 'selected';} ?>>2015</option> <option value = "2016" <? if ($leaveyear==2016) { echo 'selected';} ?>>2016</option> <option value = "2017" <? if ($leaveyear==2017) { echo 'selected';} ?>>2017</option> </select> <input type='submit' name='submit' value='Preview' Class="button" onclick='return validate()'> </div> </form> <br /><br /> <div id="box" valign="top"> <h3> <strong>Leave Application Details</strong>  </h3> <br><br> <FORM name="printbutton" method="post" align="left" action="report_print.php" target="_blank"> <input type="hidden" name="leavemonth" value="<?php echo $leavemonth; ?>"> <input type="hidden" name="leaveyear" value="<?php echo $leaveyear; ?>"> <input type="hidden" name="branch" value="<?php echo $branch;?>"> <Input type = "Submit" Name = "Submit1" Class="button" VALUE = "Print Leave Details"> </form> <table width="80%" align="center" > <thead> <tr> <th width="700px" align="left">Name</a></th> <th width="500px" align="center"></a>Application Date</th> <th width="500px" align="center"></a> </th> <th width="500x" align="right"></a>Reason</th> </tr> </thead> <tbody> <? /*echo "branch:".$branch.'<br />'; echo "leavemonth:".$leavemonth.'<br />'; echo "leaveyear:".$leaveyear.'<br />';*/ //-------------------------------------------------- if ($leavemonth =='' || $leaveyear =='' || $branch == '') { // one value missing, so don't display } else { // start display data $branchtracker = ''; if ($branch=='all'){ $branch = '%'; } $rs = mysql_query( " call vwleavereport('$leavemonth', '$leaveyear', $branch, 0);"); $query="SELECT *, MONTH(tblleaveapplication.DateFrom) as DFMonth, MONTH(tblleaveapplication.DateTo) as DTMonth, YEAR(tblleaveapplication.DateFrom) as DFYear FROM `tblleaveapplication` LEFT JOIN `tblemployee` ON tblleaveapplication.employeeid = tblemployee.id WHERE WorkGroupID LIKE '".$branch."' AND ( (MONTH(tblleaveapplication.DateFrom)=".$leavemonth." AND YEAR(tblleaveapplication.DateFrom)=".$leaveyear.") OR (MONTH(tblleaveapplication.DateTo)=".$leavemonth." AND YEAR(tblleaveapplication.DateFrom)=".$leaveyear.") ) ORDER BY WorkGroupID "; $result = mysql_query($query); while($row=mysql_fetch_array($result)) { if ($branchtracker<>$row['WorkGroupID']){ echo '<tr><td colspan=4 style="background-color: #e8e8e8; font-weight: bold;">'.$row['WorkGroupID'].'</td></tr>'; $branchtracker = $row['WorkGroupID']; } echo '<tr>'; echo '<td>'.$row['EmployeeName'].'</td>'; echo '<td>'.$row['DateFrom'].'</td>'; echo '<td>'.$row['DateTo'].'</td>'; echo '<td>'.$row['Purpose'].'</td>'; echo '</tr>'; } } ?> the problem is i want the print button only appear when there is output.. Similar TutorialsWhat I want to do is what is in the shown in the table. I want to only get the Math subject and order the units from unit 1 (UI) to the last available unit in the database; after this, under each unit, to show the homework related to each unit. I have tried to save it into an array with mysql_fetch_array but it stores the row that involves one unit. I want all the units and only the units with their homework under them.
My database is attached to this post in a txt file.
hope someone can help.
<!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>Documento sin título</title> </head> <body> <form action="" method="post" name="form1" id="form1"> <table width="200" border="1"> <tr> <td>Subject:</td> <td>Math</td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td>UI</td> <td>UI</td> <td>UII</td> <td>UIII</td> <td>UII</td> <td>...</td> </tr> <tr> <td>Home work</td> <td>Math work1</td> <td>Math work2</td> <td>Math work3</td> <td>Math work4</td> <td>Math work5</td> <td>...</td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> </table> </form> </body> </html> Hi - I designed a regular website using html. One page I used was on the previous site, and it had links to a database. Its for an online wedding registry. Problem is, on the page that pulls up with the couples list of wish list items, there is a "Print the Bliss List" link that is not working, right below the list. It is just seen as text on the page, not a button element. Can you look at the code and see why this isn't working or what I can do to fix it? I use Dreamweaver CS5 and am familiar with working in html, and css code, but not php. Here is the link to the page in question:
http://getinteriormo...s2.php?id=03313
thanks so much!
I am making a pos(point of sale) application using object oriented php. I have found thermal receipt printers support ESC/POS . i want that when i will press button to print directly , it will do automatically. anyone can help me please. Thanks in advance. I apologize if I am in the wrong forum so I hope somebody can help. I have a function which sends an emails to client everytime an order is processed. They have asked me to add a print button in the emails so they could just print the email. I have tried using javascript and call the window.print function but when I click the button nothing happens. I know that it is possible to add a print button in an email but I can't figure out what I am doing wrong. Here is some of the code use to create the email using paypal ipn script. Code: [Select] $body = "<html>". "<head>". "<script type='text/javascript'>". "//<![CDATA[ function PrintEmail(){ window.print(); } //]]> ". "</script>". "</head>". "<body>". "<a href='javascript:window.print()'>Print Page</a><br />". "<input type='button' onclick='javascript:PrintEmail();' value='Print This Page' /><br /><b>Customer Name</b>: ".$name."<br />"; foreach($id_str_array as $key => $value){ $id_quantity_pair = explode(":", $value); $product_id = $id_quantity_pair[0]; // Get the product ID $cameo_color = $id_quantity_pair[1]; $product_quantity = $id_quantity_pair[2]; // Get the quantity $product_line1 = $id_quantity_pair[3]; // Get Line 1 $product_line2 = $id_quantity_pair[4]; // Get Line 2 $product_picture = $id_quantity_pair[5]; // Get picture $discount_code = $id_quantity_pair[6]; $body .= "<b>Product</b>: ".$product_id."<br />". "<b>Quantity</b>: ".$product_quantity."<br />". "<b>Engraving Line 1</b>: ".$product_line1."<br />". "<b>Engraving Line 2</b>: ".$product_line2."<br />". "<b>Your picture</b>: <img src='http://t3.sysguru.com/mcf/pictures/".$product_picture."' height='300' width='556' /><br />"; } $body .= "<b>Total</b>: $".$_POST['mc_gross']."</body></html>"; $subject = "Transaction Completed"; $mailer->SendPayPalInfo($subject, $body); I tried just calling the javascript print function with a link and aslo create a function and called the function from the button and neither of these methods work. here is the code which send the message out. Code: [Select] function SendPayPalInfo($subject, $body){ $headers = "From: " .EMAIL_FROM_ADDR. "\r\n"; $headers .= "Reply-To: ".EMAIL_FROM_ADDR."\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; return mail(EMAIL_FROM_ADDR, $subject, $body, $headers); } Am I missing something in the headers of am I coding this wrong? Any help would be appreciated. Hello I would like to know how to print data generated by PHP into a bond paper? is there a php function that i can use? Hi guys, I have a little problem here. I have stored the data in firstname column of mysql database. I am trying to achieve by print the data from the mysql and input the data on my php page, but the page is blank. Here it is the code: <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'myusername'); define('DB_PASSWORD', 'mypassword'); define('DB_DATABASE', 'mydatabasename'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $login = clean($_GET['user']); $password = clean($_GET['pass']); if($login == '') { $errmsg_arr[] = 'Login ID missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'PASSWORD ID missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $qry="SELECT * FROM members WHERE login='$login' AND passwd='$password'"; $result=mysql_query($qry) or die('Error:<br />' . $qry . '<br />' . mysql_error()); if(mysql_num_rows($result) > 0) { $row = mysql_fetch_row($result); $firstname = htmlspecialchars($author['firstname']); echo("$firstname <br>"); } else { echo 'Username and/or Password not found.'; } } ?> Do you know why the php page is blank as I effort to get the data from mysql?? Any idea?? so here the code that i am confusing the whole month , how can i get it to print out the filtered data
$sql="SELECT COUNT(*) as conflicts im so frustrated on myself that cant do anything Hi all, This has just been bugging me forever, I actually just hardcoded the line it in years ago to just be done with it. But when I use the while loop to pull up the data for this table, it prints out every row but the last one. I've wracked my brain for years trying to fix it, and just cannot find the solution. Below is the entire page code I'm using and you can see at the bottom what I have sadly hardcoded in to make the last row display. That's the row that will not show up. If you want to see the actual live page it's he https://www.lakerstats.com/numerical-roster/ The UNK line at the end if the one that never prints out that I hardcoded. Page code as mentioned above. Appreciate any help! [php] $db = mysql_connect("localhost", "xxxx", "xxxx"); mysql_select_db("lakerstats",$db); echo "<table class=numeric WIDTH=100% cellpadding=0 cellspacing=0 border=0> <tr><td class=left colspan=\"2\" height=10><font size=\"2\" color=#000000>Current players are listed in ALL CAPS.<br />Retired uniforms are listed in <b><font color=\"#FF0000\">red</font></b>.<br /></font></td></tr><tr>"; $uniforms = "SELECT p.fname, p.lname, p.playerid, u.number, p.current_roster, u.retired, u.retired_player, p.profile_name, p.uniform_1, p.uniform_2 FROM player p, uniforms u WHERE u.number = p.uniform_1 or u.number = p.uniform_2 or u.number = p.uniform_3 or u.number = p.uniform_4 ORDER BY u.number+0, p.years, p.lname, p.fname"; $uniresult = mysql_query($uniforms) or die ("Query failed"); $numofrows = mysql_num_rows($uniresult); //let's get the number of rows in our result so we can use it in a for loop $temp = "0"; $sHTMLTemp = ""; while ($unirow = mysql_fetch_array($uniresult)) // Where $result is your result set { if ($temp != $unirow['number']) { $temp = $unirow['number']; if ($unirow['number'] == "100") { $unirow['number'] = "0"; } else if ($unirow['number'] == "101") { $unirow['number'] = sprintf("%02d", 0); } else if ($unirow['number'] == "102") { $unirow['number'] = "UNK"; } else { $unirow['number'] = $unirow['number']; } $sHTMLTemp = preg_replace("/(.+),/","\\1",$sHTMLTemp); print $sHTMLTemp ; print "</tr>"; $sHTMLTemp = ""; if ($unirow[retired] == "yes") { echo "<tr><td class=left height=10><font size=\"2\" color=\"#FF0000\"><b>#" . $unirow[number] . "</b></font><font size=\"2\"> - "; } else { echo "<tr><td class=left height=10><font size=\"2\" color=\"#000000\">#" . $unirow[number] . "</font><font size=\"2\"> - "; } } if ($unirow[current_roster] == "yes") { $unirow[fname] = strtoupper($unirow[fname]); $unirow[lname] = strtoupper($unirow[lname]); } else { $unirow[fname] = $unirow[fname]; $unirow[lname] = $unirow[lname]; } if ($unirow[retired_player] == $unirow[playerid]) { $sHTMLTemp .= "<b><a href=\"/player-stats-bio/?pname=".$unirow['profile_name']."\"><font color=\"#FF0000\">" . $unirow['fname'] . " " . $unirow['lname'] . "</font></a></b>, "; } else { $sHTMLTemp .= "<a href=\"/player-stats-bio/?pname=".$unirow['profile_name']."\">" . $unirow['fname'] . " " . $unirow['lname'] . "</a>, "; } } echo "<a href=\"/player-stats-bio/?pname=jim_fritsche\">Jim Fritsche</a>, <a href=\"/player-stats-bio/?pname=bobby_watson\">Bobby Watson</a>"; echo "</font></td></tr>"; echo "</font></td></tr></table>"; mysql_close($db); [/php] Edited September 14, 2020 by ban5150 Hi guys, I have a problem of prints of the data from table1. I am checking out the data of username and password in the url to go through in members table and then checking the username in table1 to get matching data and select rows with the fields id of teststrings1, teststrings2 and teststrings3, then print out the data. Here it is the code <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'myusername'); define('DB_PASSWORD', 'password'); define('DB_DATABASE', 'mydatabasetable'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $username = clean($_GET['user']); $password = clean($_GET['pass']); if($username == '') { $errmsg_arr[] = 'username ID missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'PASSWORD ID missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $qry="SELECT * FROM members WHERE username='$username' AND passwd='$password'"; $result=mysql_query($qry) or die('Error:<br />' . $qry . '<br />' . mysql_error()); if($result) { $qrytable1="SELECT teststrings1, teststrings2, teststrings3 FROM table1 WHERE username='$username'"; $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error()); echo "<p id='teststrings1'>"; echo $row['teststrings1'] . "</p>"; echo "<p id='teststrings2'>"; echo $row['teststrings2'] . "</p>"; echo "<p id='teststrings3'>"; echo $row['teststrings3'] . "</p>"; } } ?> When I am am checking the data of username and password through in members table and then checking the username in table1 before select the rows with the fields id of teststrings1, teststrings2 and teststrings3 to prints out, but it have print out as blank on php page. Any idea? The following code returns thousands of results separated by a semicolon; Code: [Select] while($getpostid_result=mysql_fetch_array($getpostid)) { $postusername=$getpostid_result['username']; echo $postusername.";"; } the problem is it does not print everything in one row.it breaks into the nextline after if it encounters certain chatacters or some other reason. I want it to able to print everything in one row irrespective of the number of results returned. Hello, I am fairly new to PHP and I found some php code to print an array of numbers that create a combination - however, its slams all the numbers together without a space or a comma separator. How can I separate the numbers in this array with the given code? Ive attached a photo of the code.
Hey again, was making a registration form with php and postgresql and im stuck at making a 'log out' button, i need to make something simple like - press on a link, it redirects u to the index page and if u try to go 'back' it wouldn't let u.. I found something on google like this: <a href="test.php?logout=1">Log out</a> and on test.php: <?php if(isset($_GET['logout'])){ session_unset; session_destroy; } ?> Succesfully logged out, go back to index page: <a href="index.php">Home</a> but i didnt use any sessions at all.. is there something like 'session_destroy' with postgre? i mean smth like 'pg_desroy'? ok, how do I make this button go to the specific link that is set with it.......it goes to page without eventid=eventid: Code: [Select] <form action= \"editevent.php?eventid=".$row['eventid']."\" method=\"link\"><INPUT TYPE=\"submit\" VALUE=\"Edit\"></form>"; Hi I have made this simple login page by setcookies function and I want now to make a logout button. Here the code <?php /* PHP Form Login Remember Functionality with Cookies */ if(!empty($_POST["remember"])) { setcookie ("username",$_POST["username"],time()+ 3600); setcookie ("password",$_POST["password"],time()+ 3600); setcookie ("color",$_POST["color"],time()+ 3600); //3600 = 1 hour //86400 = 1 day //(8640*30) = 1 month echo "Cookies Set Successfuly"; } else { setcookie("username",""); setcookie("password",""); setcookie("color",""); echo "Cookies Not Set"; } ?> <form action="Cookies.php" method="post" style="border: 2px dotted blue; text-align:center; width: 400px;"> <p>Welcome <?php echo ( !empty($_POST ['username']) ) ? $_POST ['username'] : 'USER'; ?> </p> <p>Username: <input name="username" type="text" value="<?php if(isset($_COOKIE["username"])) { echo $_COOKIE["username"],( !empty($_POST ['username']) ) ? $_POST ['username'] : '';} ?>" > </p> <p>Password: <input name="password" type="password" value=" <?php if(isset($_COOKIE["password"])) { echo $_COOKIE["password"]; } ?>" > </p> <p>Choose Your Favorite Color: <input name="color" type="color" value="<?php if(isset($_COOKIE["color"])) { echo $_COOKIE["color"]; } ?>"> </p> <p><input type="checkbox" name="remember" /> Remember me</p> <p><input type="submit" value="Login"></p> </form> Any idea ?? i got a submit button... if user click the submit button iwant to page will auto referesh... can someone teach me Apologies for this because it's probably very simple, but I've never worked with JavaScript or PHP or anything before.
So the website I am producing is to sell tickets for something. To purchase these tickets the user is required to click on the seats they want and it will pop up in a box on the left hand side confirming their seat selection and how much the ticket is.
The website has to feature a "Reset Database" button which totally resets everything. No tickets are selected anymore, the user is logged out etc. I have this working fine.
It also has to feature a "Cancel" button, which simply removes the seats the user has selected, but still keeps them logged in. How do I code this?
This is what the "Cancel" button looks like on the Index page:
<div id="theButtons"> <input type="button" value="Cancel Choices" title="Cancel Choices" onclick="cancel()" /> </div> And this is my code on the JavaScript page: function cancel(){ var s = document.getElementsByTagName('space'); window.location="index.php"; } How do I get this working? Edited by DavidD95, 08 November 2014 - 10:36 AM. Hi, Sorry if this is in the wrong place. I really just need a search term to use for what i'm trying to do. I would like my buttons to appear pressed on each page. For example if you are on the home page the home button will appear pressed. If you are on the screenshots page the screenshots button will appear pressed. Pretty much i want my nav bar to display what page is being viewed. I'm using SMF forums and i've created an extension of my forum that i'm using on the root of my site. As far as i can tell i have the correct code added to the forum that should highlight the home button while on the home page. The part i'm missing is the code to add onto the home page. This is the code I have on the button. $buttons = array( 'overview' => array( 'title' => $txt['overview'], 'href' => 'http://www.wararmada.com', 'show' => true, 'sub_buttons' => array( 'alliance' => array( 'title' => $txt['alliance'], 'href' => $scripturl . '?action=overview;area=alliance', 'show' => true, ), This is the code i have in the language file. $txt['overview'] = 'overview'; I'm not sure what this is called. I don't know anything about php but so far i've been able to get by just using google but I don't really know how to search for this. Thanks for all the help! I need to have several sets of radio buttons, each of which will be used to assess criteria, e.g. endurance, strength, posture, etc. I've made a function, as follows: Code: [Select] <?php function makeRadio($min, $max, $lbl) { echo "<div class='inline_label'>".$lbl."</div>"; for ($i = $min; $i <= $max; $i++) { echo "<input type='radio' name='".$lbl."' value='".$i."' id='".$lbl.$i."_".($i-1)."'"; if(isset($_POST[$lbl])) { echo " checked='checked'"; } echo "/>".$i." "; } echo "<br />"; } I'm calling the function like this: Code: [Select] <form id="form1" name="form1" method="post" action="" > <?php makeRadio(1,10, 'Endurance'); makeRadio(1,10, 'Strength') ?> <p><input type="submit" name="submit" id="submit" value="Submit" /></p> </form> On form submission, however, I can't get the buttons to be sticky? Where am I going wrong in my function code? TIA hi i have daily data table it's contain ID, Date, and Rain i need to make a new table but tendays period ,,, any clues ,,, will be very greatfull thanks a lot ,,, i have created a shopping cart.but i dont know how i can add all the choosen data into a database.i just follow the step from youtube.i want to know how i can add one button so all the product will be recorded into database because it already have 1 button to update the shopping cart but it do not have button to record all the choosen product.
here is all the file that i have created.
index2.php
<? session_start(); error_reporting(E_ALL); require("connection.php"); if(isset($_GET['page'])){ $pages=array("products", "cart"); if(in_array($_GET['page'], $pages)) { $_page=$_GET['page']; }else{ $_page="products"; } }else{ $_page="products"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <body> </body> </html> <link rel="stylesheet" href="style2.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script> <script type="text/javascript"> $(function() { }); </script> </head> <body> <div id="container"> <div id="main"> <?php require($_page.".php"); ?> </div> <div id="sidebar"> <h1>Cart</h1> <?php if(isset($_SESSION['cart'])){ $sql="SELECT * FROM products WHERE id_product IN ("; foreach($_SESSION['cart'] as $id => $value) { $sql.=$id.","; } $sql=substr($sql, 0, -1).") ORDER BY name ASC"; $query=mysql_query($sql); while($row=mysql_fetch_array($query)){ ?> <p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p> <?php } ?> <hr /> <a href="index2.php?page=cart">Go to Cart</a> <?php }else{ echo "<p>Your Cart is empty</p>"; } ?> </div> </div> </body>products.php <?php if(isset($_GET['action']) && $_GET['action']=="add"){ $id=intval($_GET['id']); if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]['quantity']++; }else{ $sql_s="SELECT * FROM products WHERE id_product={$id}"; $query_s=mysql_query($sql_s); if(mysql_num_rows($query_s)!=0){ $row_s=mysql_fetch_array($query_s); $_SESSION['cart'][$row_s['id_product']]=array( "quantity" => 1, "price" => $row_s['price'] ); }else{ $message="This product id it's invalid!"; } } } ?> <h1>Product List</h1> <?php if(isset($message)){ echo "<h2>$message</h2>"; } ?> <table> <tr> <th>Name</th> <th>Description</th> <th>Price</th> <th>Action</th> </tr> <?php $sql="SELECT * FROM products ORDER BY name ASC"; $query=mysql_query($sql); while ($row=mysql_fetch_array($query)) { ?> <tr> <td><?php echo $row['name'] ?></td> <td><?php echo $row['description'] ?></td> <td>RM <?php echo $row['price'] ?></td> <td><a href="index2.php?page=products&action=add&id=<?php echo $row['id_product'] ?>">Add To Cart</a></td> </tr> <?php } ?> </table>cart.php <?php if(isset($_POST['submit'])){ foreach($_POST['quantity'] as $key => $val) { if($val==0) { unset($_SESSION['cart'][$key]); }else{ $_SESSION['cart'][$key]['quantity']=$val; } } } ?> <a href="index2.php?page=products">Go back to product page</a> <h1>View Cart</h1> <form method="post" action"index2.php?page=cart"> <table> <tr> <th>Name</th> <th>Quantity</th> <th>Price</th> <th>Item Price</th> </tr> <?php $sql="SELECT * FROM products WHERE id_product IN ("; foreach($_SESSION['cart'] as $id => $value) { $sql.=$id.","; } $sql=substr($sql, 0, -1).") ORDER BY name ASC"; $query=mysql_query($sql); $totalprice=0; while($row=mysql_fetch_array($query)){ $subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price']; $totalprice+=$subtotal; ?> <tr> <td><?php echo $row['name'] ?></td> <td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>"</td> <td>RM <?php echo $row['price'] ?></td> <td>RM <?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?></td> </tr> <?php } ?> <tr> <td>Total Price: <?php echo $totalprice ?></td> </tr> </table> <button type="submit" name="submit"> Update Cart</button> </form> <br /> <p> To remove an item,set the quantity to 0</p> |