Re: [PHP-DB] Shopping cart
On Sat, 2009-05-16 at 10:54 +0100, Vernon St Croix wrote: > Hi, > > I am pretty new to PHP and I am trying to create a shopping cart. > > I keep on getting the below error when trying to show the shopping list. > > Any guidance that can be provided will be very much appreciated > > Fatal error: Call to a member function query() on a non-object in > C:\wamp\www\draft\basket.php on line 36 > > mysql_connect.php > $con = mysql_connect("localhost","root",""); > if (!$con) > { > die('Could not connect: ' . mysql_error()); > } > mysql_select_db("rum", $con); > ?> > > basket.php > include("mysql.class.php"); > include ("header.php"); > include ("mysql_connect.php"); > include ("functions.php"); > ?> > > Rum Basket > echo writeCart(); > ?> > > > Rum on Offer > > $sql= 'SELECT * FROM spirits BY id'; > $result = $con->query($sql); > $output[]= ''; > while ($row = $result->fetch()) { > $output[] = ''.$row['name'].': £'.$row['price'].' href="cart.php?action=add&id= > '.$row['id'].'">Add to Cart'; > } > $output[] = ''; > echo join ('', $output); >?> > > > > include("footer.html"); > > ?> > > > cart.php > > > include ("header.php"); > > include ("mysql_connect.php"); > > include ("functions.php"); > > > > $cart = $_SESSION['cart']; > > > if(isset($_GET["action"])) > { $action = $_GET["action"]; } > else > { $action = ""; } > > > switch ($action) { > case 'add': > if ($cart) { >$cart .= ','.$_GET['id']; > } else { >$cart = $_GET['id']; > } > break; > case 'delete': > if ($cart) { >$items = explode(',',$cart); >$newcart = ''; >foreach ($items as $item) { > if ($_GET['id'] != $item) { > if ($newcart != '') { > $newcart .= ','.$item; > } else { > $newcart = $item; > } > } >} >$cart = $newcart; > } > break; > case 'update': > if ($cart) { > $newcart = ''; > foreach ($_POST as $key=>$value) { >if (stristr($key,'qty')) { > $id = str_replace('qty','',$key); > $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); > $newcart = ''; > foreach ($items as $item) { > if ($id != $item) { > if ($newcart != '') { >$newcart .= ','.$item; > } else { >$newcart = $item; > } > } > } > for ($i=1;$i<=$value;$i++) { > if ($newcart != '') { > $newcart .= ','.$id; > } else { > $newcart = $id; > } > } >} > } > } > $cart = $newcart; > break; > } > $_SESSION['cart'] = $cart; > > ?> > > > > Rum Basket > > echo writeCart(); > ?> > > > > > > Please Check Quantities... > > echo showCart(); > ?> > > Back to Rum List > > > > > > > include("footer.html"); > > ?> > > functions.php > > function writeCart() { > $cart = $_SESSION['cart']; > if (!$cart) { > return 'There is no alcohol in your Rum Basket'; > } else { > // Parse the cart session variable > $items = explode(',',$cart); > $s = (count($items) > 1) ? 's':''; > return 'There are'.count($items).' item'.$s.' in your > rum basket'; > } > } > > function showCart() { > $cart = $_SESSION['cart']; > if ($cart) { > $items = explode(',',$cart); > $contents = array(); > foreach ($items as $item) { >$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; > } > $output[] = ' id="cart">'; > $output[] = ''; > foreach ($contents as $id=>$qty) { >$sql = 'SELECT * FROM spirits WHERE id = '.$id; >$result = $con->query($sql); >$row = $result->fetch(); >extract($row); >$output[] = ''; >$output[] = ' class="r">Remove'; >$output[] = ''.$name.''; >$output[] = '£'.$price.''; >$output[] = ' size="3" maxlength="3" >'; >$output[] = '£'.($price * $qty).''; >$total += $price * $qty; >$output[] = ''; > } > $output[] = ''; > $output[] = 'Grand total: £'.$total.''; > $output[] = 'Update cart'; > $output[] = ''; > } else { > $output[] = 'You shopping cart is empty.'; > } > return join('',$output); > } > > ?> > > > > > Many Thanks > > Vee Hi, if i look at the code of basket.php its only 33 lines long, hence it must be counting the includes as well. Send me zip file of you project, i can help debug. regards, Max. Max E. Kimambo Franz-Stenzer-Str, 51 12679 Berlin Office: +493053080013 Mobile: +4917649520175 Fortune message of the moment You will triumph over your enemy. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Incrementing Primary Key
Hi Ethan, I am working currently as clinical developer and have developed multi site databases for clinical trials. I would say the approach you take will depend on number of factors. How is the site connectivity? I had a scenario where sites had erratic and slow connectivity so i had to setup MySql server at each site and let that data sync over to a central location, with help of mysqldump then i put all the data together into one database from 3 replicas. The application is more responsive over LAN and users are less likely to complain when entering data(can be a major difference between success / failure) If connectivity is not an issue and the app will reside on a central web server then putting everything in a single database is an option. Of course your database design will have to reflect that using appropriate keys to be able to tie the CRF data to a particular site patient visit combination. If you chose to go with replication, then you have to take care on how you auto increment values so that there are no collisions, best is to assign a range to each site this is the simplest way. 1001 - 5000 Site A 5001 - 10001 Site B Of course you will need some input from your statisticians on how they want the data, that will also drive your design decisions on what works best. Separate db or not: I would separate data for each study into its own database, its much easier in terms of administration and locking the study. Is the study single blinded, double blinded ? How will randomization take place ? These are all questions you need to know before you can decide how you structure your database and application therefore. If you have other questions feel free to get in touch. with kind regards, Max. Max Kimambo Franz-Stenzer-Straße, 51 12679, Berlin. T: +493057706550 (new number) M: +4917649520175 - Original Message - From: "Ethan Rosenberg" To: "php-db-lists.php.net" Sent: Wednesday, October 27, 2010 3:11:00 PM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna Subject: [PHP-DB] Incrementing Primary Key Dear List - Thanks for all your excellent help. I am setting up a database for medical research, which will be conducted at various sites. The sites will be identified by a letter {A,B,C }. The medical record number [primary key] will start at 1001 and increment by one(1) for each patient at each site; ie, A 1001, A1002, B1001, B1002 .. How do I do this? Do I need a separate database for each site? Ethan -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Incrementing Primary Key
- Original Message - From: "Richard Quadling" To: "Ethan Rosenberg" Cc: "php-db-lists.php.net" Sent: Wednesday, October 27, 2010 4:10:52 PM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna Subject: Re: [PHP-DB] Incrementing Primary Key On 27 October 2010 14:11, Ethan Rosenberg wrote: > Dear List - > > Thanks for all your excellent help. > > I am setting up a database for medical research, which will be conducted at > various sites. The sites will be identified by a letter {A,B,C }. The > medical record number [primary key] will start at 1001 and increment by > one(1) for each patient at each site; ie, A 1001, A1002, B1001, B1002 ... > How do I do this? > > Do I need a separate database for each site? > > Ethan I'd use an INSERT trigger to generate the value. I use MS SQL - no idea what DB you are using - and so here are what I'd do Table: Sites UniqueID int identity(1,1) SiteCode char(1) LastMedicalRecordNumber int default 0 Table:MedicalRecords UniqueID int identity(1,1) SiteID int // Foreign key to Sites.UniqueID MedicalRecordNumber int default 0 The trigger would be something like [UNTESTED] ... CREATE TRIGGER NewMedicalRecord ON MedicalRecords FOR INSERT AS UPDATE Sites SET LastMedicalRecordNumber = 1 + LastMedicalRecordNumber WHERE UniqueID IN (Inserted.SiteID) UPDATE MedicalRecords SET MedicalRecordNumber = Sites.LastMedicalRecordNumber FROM INSERTED INNER JOIN MedicalRecords ON INSERTED.UniqueID = MedicalRecords.UniqueID INNER JOIN Sites ON INSERTED.SiteID = Sites.UniqueID The app need not have any part is assigning something as important as the unqiue id of a row. That sort of integrity needs to be part of the database. The client app really wants to be as simple as possible. Using stored procedures and views (which are tuned once by the SQL Server) benefit the app in returning the required data faster and with less utilisation. Compare that against every identical query being compiled from scratch every single time. If you want to put the SiteCode on the MedicalRecord rather than the SiteID, you could. And then break the link between the MedicalRecords and Site tables. The trigger would use the SiteCode to link rather then the SiteID / Sites.UniqueId to get INSERTED connecting to Sites. As far as the app goes? You tell it which of the available sites the medical record is for and insert it (along with any other user supplied data). The integrity is preserved by the DB. "Just doing my job, sir!" Richard. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Well pointed out Richard, The App should be as simple as possible and should not try to replicate things are done easily by databases. Using stored procedures for all your CRUD and data integrity checks will save you quite a lot of time. with kind regards, Max. Max Kimambo Franz-Stenzer-Straße, 51 12679, Berlin. T: +493057706550 (new number) M: +4917649520175 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Incrementing Primary Key
From: "Ethan Rosenberg" To: rquadl...@googlemail.com, "Ethan Rosenberg" Cc: "php-db-lists.php.net" Sent: Thursday, October 28, 2010 4:55:34 AM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna Subject: Re: [PHP-DB] Incrementing Primary Key At 10:10 AM 10/27/2010, Richard Quadling wrote: >On 27 October 2010 14:11, Ethan Rosenberg wrote: > > Dear List - > > > > Thanks for all your excellent help. > > > > I am setting up a database for medical research, which will be conducted at > > various sites. Â The sites will be identified > by a letter {A,B,C }. Â The > > medical record number [primary key] Â will start at 1001 and increment by > > one(1) for each patient at each site; ie, A > 1001, A1002, B1001, B1002 .. > > How do I do this? > > > > Do I need a separate database for each site? > > > > Ethan > >I'd use an INSERT trigger to generate the value. > >I use MS SQL - no idea what DB you are using - and so here are what I'd do ... > >Table: Sites > UniqueID int identity(1,1) > SiteCode char(1) > LastMedicalRecordNumber int default 0 > >Table:MedicalRecords > UniqueID int identity(1,1) > SiteID int // Foreign key to Sites.UniqueID > MedicalRecordNumber int default 0 > >The trigger would be something like [UNTESTED] ... > >CREATE TRIGGER NewMedicalRecord ON MedicalRecords FOR INSERT AS > UPDATE Sites > SET LastMedicalRecordNumber = 1 + LastMedicalRecordNumber > WHERE UniqueID IN (Inserted.SiteID) > > UPDATE MedicalRecords > SET MedicalRecordNumber = Sites.LastMedicalRecordNumber > FROM >INSERTED >INNER JOIN >MedicalRecords ON INSERTED.UniqueID = MedicalRecords.UniqueID >INNER JOIN >Sites ON INSERTED.SiteID = Sites.UniqueID > > >The app need not have any part is assigning something as important as >the unqiue id of a row. That sort of integrity needs to be part of the >database. > >The client app really wants to be as simple as possible. Using stored >procedures and views (which are tuned once by the SQL Server) benefit >the app in returning the required data faster and with less >utilisation. Compare that against every identical query being compiled >from scratch every single time. > >If you want to put the SiteCode on the MedicalRecord rather than the >SiteID, you could. And then break the link between the MedicalRecords >and Site tables. The trigger would use the SiteCode to link rather >then the SiteID / Sites.UniqueId to get INSERTED connecting to Sites. > >As far as the app goes? > >You tell it which of the available sites the medical record is for and >insert it (along with any other user supplied data). The integrity is >preserved by the DB. "Just doing my job, sir!" > >Richard. > >-- >Richard Quadling >Twitter : EE : Zend >@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY Thank you. I'm using MySQL, which I do not think has the ability to auto-increment a primary key from any value other than 1. Here is some pseudo code. Please help me to set it up properly. Table Intake SiteID char(2) primary key not null, // This is A for site1, B for site 2 RecordNum int(10,0) primary key not null auto_increment, // increment starts from 10001 etc. Thanks Ethan MySQL 5.1 PHP 5 Linux [Debian (sid)] Hi Ethan, This will set a new auto increment value for a table . ALTER TABLE RecordNum AUTO_INCREMENT=1001 Regards, Max. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Data Migration
with kind regards, From: "Karl DeSaulniers" To: php-db@lists.php.net Sent: Monday, November 8, 2010 12:19:23 PM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna Subject: [PHP-DB] Data Migration Hello All, I was wondering if I could get some pointers on the best/safest method to migrate data on a fairly simple scale. I have a registration form that gets submitted. I want to take that info and store it in a table as a sort of Que. if you will. When the application is approved, I want to have something set up for the owner or his employees to access and be able to activate the account. Behind the scenes, the info from the registration table is simply moved to the users table and an email is sent out. What would be the simplest way to migrate the data between the tables? Is there a bulk way to move data in PHP? Or is that best/safest done in MySQL? Examples, tuts, keywords appreciated. TIA Karl DeSaulniers Design Drumm http://designdrumm.com PS: I'm on google now.. - Original Message - Just thinking about it, how about a stored procedure, that does select from registration table and insert into the user table, you can then delete the inserted record from the registration table. Something like this. Insert into table_users (fields1, field2) Select field1, field2 from registrations_table WHERE some_criteria you can have a marker field for tracking what was already worked up on. Just a thought. with kind regards, Max. Max Kimambo Franz-Stenzer-Straße, 51 12679, Berlin. T: +493057706550 (new number) M: +4917649520175 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] query help
From: "Vinay Kannan" To: "PHP DB" , "php mysql" Sent: Wednesday, November 17, 2010 2:51:35 PM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna Subject: [PHP-DB] query help Hello PHP Gurus, I need your help on an insert query. I wanted to know if there is way to insert an array of values into a DB. An eg would explain this better : If I have 2 tables in a DB, 1) users has 3 columns 2) hobbies = 5 columns I was thinking of having a single function which will perform the insert on any insert which happens on the entire website. Eg : This function can be called with 2 parameters, the first parameter the table name, and the second parameter is an array of values which will be inserted into the table. eg : Users has these columns [1]ID [2] Name [3]Location so the function call would be something like * insert_into_tbale(users,array[user_values])* ** Does this make sense ? Is this a good method to follow ? Thanks in advance ! Vinay Kannan. Hi Vinay, You may want to try codeigniter. More info here. http://codeigniter.com/user_guide/database/active_record.html#insert with kind regards, Max. Max Kimambo Franz-Stenzer-Straße, 51 12679, Berlin. T: +493057706550 (new number) M: +4917649520175 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php