File: exercise/exampleproducts.php

Recommend this page to a friend!
  Classes of Jorge Castro  >  Chaos Machine One  >  exercise/exampleproducts.php  >  Download  
File: exercise/exampleproducts.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Chaos Machine One
Generate random data that obeys to formulas
Author: By
Last change:
Date: 1 year ago
Size: 3,426 bytes



Class file image Download

use eftec\chaosmachineone\ChaosMachineOne;
$chaos=new ChaosMachineOne();




$sql="CREATE TABLE `products` (
  `IdProduct` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(50) NOT NULL DEFAULT '',
  `Price` int(11) NOT NULL DEFAULT '0',
  `Image` varchar(50) NOT NULL DEFAULT '',
  `Description` varchar(2000) NOT NULL DEFAULT '',
  `Weight` int(11) NOT NULL DEFAULT '0',
  `IdCategory` int(11) NOT NULL,
  PRIMARY KEY (`IdProduct`)

$db=new PdoOne("mysql","localhost","root","abc.123","chicago");
try {
} catch(
Exception $exception) {
// table already created

"<h1>Creating $numFiles products</h1>";

// this examples works with files. It does the next tasks.
// * it fills the database with information based on the files (products)
// * it copies the files from the source /machines to destination /databasefile changing the filename
$chaos->table('products',$numFiles) // the table products must exist!.
field('IdProduct','int', 'identity', 0)
field('Name', 'string', 'database', '', 0, 50)
field('Price', 'int', 'database', 0, 0, 45)
field('Image', 'string', 'database', '') // it is store on the db.
->field('ImageSource', 'string', 'variable', '') // it is the local file (it is not store on the db)
->field('ImageDestination', 'string', 'variable', '') // it is the destination file. (it is not store on the db)
->field('Description', 'string', 'database', '', 0, 100)
field('Weight', 'int', 'database', 0)
field('IdCategory', 'int', 'database', 0)
setArray('productimages', $files)
setArray('productimagescode', $filesWithoutExtension)
setArray('productnames', ['Augers','Concrete Tools','Compactors','Demolition Tools','Drain Cleaners','Plumbing Tools','Floor Cleaners','Floor Care','Refinishers'])
setFormat('productformat', ['{{productnames}} {{productimagescode}}', '{{productnames}}'])
setFormat('imageformat', ['{{IdProduct}}_{{productimages}}'])
gen('when always set IdProduct.add=1') // it increases 1 by one.
->gen('when always set Name.value=randommask("? Model #####-00","productformat")')
gen('when always set Price.value=random(10,100)')
gen('when always set Image.value=randomformat("imageformat")')
gen('when always set ImageSource.value=$localfolder + arrayindex("productimages")')
gen('when always set ImageDestination.value=$destinationfolder + Image.getvalue')
gen('when always set ImageDestination.copyfilefrom=ImageSource.getvalue')
//->gen('when always set Image.value=IdProduct.getvalue and Image.add="_" and Image.add=arrayindex("productimages")') // it's the same than randomformat("imageformat")
->gen('when always then Description.value=randomtext("Lorem ipsum dolor","loremIpsum",1,4,30)')
gen('when always set Weight.value=random(2,10)')
gen('when always set IdCategory.value=random(1,4)')


For more information send a message to info at phpclasses dot org.