-
Posts
3,584 -
Joined
-
Last visited
-
Days Won
3
Everything posted by JonnoTheDev
-
Implications of database schema on PHP implementation
JonnoTheDev replied to NotionCommotion's topic in PHP Coding Help
I can only see the need for 2 tables. A documents table and a document_types table. The documents table references the documents_type table by type_id I.e. documents ---------- doc_id type_id date_added doc_name document_types ---------- type_id type_title document_types would contain people, companies, and projects, and any other types. Each has an id and is used in a join from documents when you want to say fetch all company documents. You do not want to be creating separate tables for each type as there is no flexibility in the design and as you have already found out it is hard to create the business logic. -
Here's a challenge for anyone using OOP so you can see how it benefits. I've started it off and you can see there are issues that need to be corrected. An online shop needs an order dispatch system for sending out parcels using different couriers. At the start of the day a batch will be started and at the end of the day it will be closed when no more parcels are going to be shipped. This is called the dispatch period. Each parcel sent out with a courier is called a consignment. Each consignment will be given a unique number and each courier will provide an algorithm for generating the number. At the end of the dispatch period a list of all consignment numbers need to be sent to each courier. Each courier has their own method for accepting the list i.e. UPS uses email, Fedex uses FTP. Build a structure using OOP to facilitate the implementation of the above scenario. It does not need to be a final product. Assume that your library is given to another developer to use who will build a front end for it that will have the following features: 1. Start a new batch 2. Add a consignment 3. End the batch /** * Interface CourierInterface */ interface CourierInterface { /** * Fetch a consignment number * @return int */ public function fetchConsignmentNumber(); /** * Dispatch a consignment number to a courier */ public function dispatch($number); } class FedexCourier implements CourierInterface { /** * @var int */ private static $count = 10000; /** * We will use a simple static counter here to return a number * @return int */ public function fetchConsignmentNumber() { self::$count++; return self::$count; } /** * Send to courier * @param $number * @return string */ public function dispatch($number) { } } class Consignment { /** * @var CourierInterface */ protected $courier; /** * @var int $number Should contain a consignment number */ protected $number; /** * Consignment constructor. * @param CourierInterface $courier */ public function __construct(CourierInterface $courier) { $this->courier = $courier; // fetch a consignment number from the courier $this->number = $this->courier->fetchConsignmentNumber(); } /** * Simple getter * @return int */ public function getNumber() { return $this->number; } /** * @return mixed */ public function process() { return $this->courier->dispatch($this->number); } } class Batch { /** * @var array */ protected $consignments = []; /** * Batch constructor. * Start the dispatch period * This could take in a storage dependency for persistence */ public function __construct() { } /** * Add a consignment to the current batch * * @param Consignment $consignment */ public function addConsignment(Consignment $consignment) { $this->consignments[] = $consignment; } /** * Process each consignment */ public function endDispatchPeriod() { // loop through each of the consignments // the storage dependency could be use here to track the consignment numbers // i.e. $c->getNumber() foreach ($this->consignments as $c) { $c->process(); } } } Client <?php // client code // start a new batch $batch = new Batch(); // add some consignments $parcel1 = new Consignment(new FedexCourier()); $batch->addConsignment($parcel1); $parcel2 = new Consignment(new FedexCourier()); $batch->addConsignment($parcel2); // end the batch $batch->endDispatchPeriod(); ?>
-
You are using PHP 4 syntax that is obsolete. You should not use the var keyword, nor should you give a method the same name as the class. In PHP 4 this was how you defined the constructor. I suggest you read up on how to use classes in PHP 5. Refactored <?php class Customer extends VerySimpleModel implements TemplateVariable { public static $meta = array( 'table' => CUST_TABLE, 'pk' => array( 'cust_id' ), 'joins' => array( 'location' => array( 'constraint' => array( 'cust_id' => 'loc.cust_id' ), ), 'service' => array( 'constraint' => array( 'service_level' => 'service.id' ), ), ), ); public $id; public $name; public $contact; public $fname; public $lname; public $contact_phone; public $phone_number; public $fax_number; public $location; public $domain; public $service_level; public $row; public $address; private $_location = null; public function getCustomer( $id = 0 ) { echo 'here'; $this->id=0; if($id && ($info=$this->getInfoById($id)) ){ $this->row=$info; $this->id=$info["id"]; $this->name=$info["cust_name"]; $this->domain=$info["domain"]; $this->location=$this->getLocationInfo($id); $this->service_level=$info["service_level"]; } } } //page that is loading the Grid data $custs = Customer::objects();
-
Have a look here https://github.com/ziadoz/awesome-php Most of these will be available through packagist
- 3 replies
-
- open-source
- packagist
-
(and 1 more)
Tagged with:
-
Legend! Had to make a slight mod as, 'quantity' was ambiguous. I owe you a pint, cheers! SELECT product_model_id,model_code,GROUP_CONCAT(qty, ':', total_ordered ORDER BY qty ASC) AS qty_orders FROM product_model LEFT JOIN ( SELECT product_model_id, quantity AS qty, COUNT(op_id) AS total_ordered FROM order_product GROUP BY product_model_id, qty ) AS sq USING (product_model_id) GROUP BY product_model_id;
- 2 replies
-
- mysql
- group_concat
-
(and 1 more)
Tagged with:
-
I am trying to generate a report that will assist a client in determining price changes for their products. The query I need to use is bugging the hell out of me. I have extracted a portion of it as the other parts are irrelevant. What I am trying to do is display the number of times a product has been ordered of a certain quantity. I am attempting to concatenate the quantity with the number ordered. i.e. product_model_id model_code qty_orders ---------------------- --------------- ------------- 12 12345 1:32,2:12,3:2 So for the above product (12) the following info is returned A quantity of 1 has been ordered 32 times A quantity of 2 has been ordered 12 times A quantity of 3 has been ordered 2 times I can pull the qty_orders data out without issue by specifying the id of the product, however I need to display for all products including the additional information (product_model_id, model_code). Here is the database structure: product_model ------------- product_model_id model_code order_product ------------- op_id order_id product_model_id quantity I know that I need to do the sub query within a join but I can't get it quite right. Here is the part I have down. I need to get rid of the WHERE product_model_id=12 bit and create a join to product_model so I can display all products. SELECT GROUP_CONCAT(quantity, ':', total_ordered) AS qty_orders FROM ( SELECT quantity, COUNT(op_id) AS total_ordered FROM order_product WHERE product_model_id=12 GROUP BY quantity ) AS sq Complex SQL isn't really my forte so any help would be appreciated.
- 2 replies
-
- mysql
- group_concat
-
(and 1 more)
Tagged with:
-
1. Remove any PHP / Server Side code 2. Strip out any <img> tags 3. Strip out any <script> tags 4. Strip out any <link> tags to CSS files Job done. Should run like lightning!
-
Also consider Yii http://www.yiiframework.com/
-
For dedicated servers I would like to add OVH http://www.ovh.co.uk/products/dedicated_offers.xml We have about 5 of these servers running CentOS.
-
It's real beginner orientated so just a warning.
-
WTF. The original post is supposed to help, not start a bitching session. Stop please.
-
There have been many posts on this forum in regards to beginners learning Object Oriented Programming in PHP. It is a difficult concept to grasp when you are new to the principles and benefits of this style of programming and can be quite hard to understand from some of the responses you may receive on this forum from more advanced members. So, I stumbled across this website today which has video tutorials that explain the core principles very well. If you are a beginner then you should definately take a look and it should make the subject a lot clearer. http://www.killerphp.com/tutorials/object-oriented-php/
-
Which PHP Editor do you think is the best? [v2]
JonnoTheDev replied to Daniel0's topic in Miscellaneous
Keldorn, this is for you [attachment deleted by admin] -
Which PHP Editor do you think is the best? [v2]
JonnoTheDev replied to Daniel0's topic in Miscellaneous
Trialing DWCS4. Couldn't get used to netbeans. Happy so far. The code colouring is far better than any other IDE. Too much customising in netbeans.