php - MySql - organizing variables for an array


You can simply use ORDER BY in the query for the dates (you apparently know this). To create the array for JS, I would use JSON.

$values = array(array('MOnth', 'Sales Price 1', 'Sales Price 2'));
while ($row = $result->fetch()) {
    $values[] = array_values($row);
}
$for_javascript = json_encode($values);

I did some reading on this a while back when I was getting started with Go. According to this link, http://peter.bourgon.org/go-in-production/, they recommend using CLI flags (parameters) instead of environment vars - they even convert environment vars to flags to their CLI apps.

It took some getting used to; but, I really do see the advantages of going pure CLI flags between development, staging and production environments - having specific scripts for each environment.

For example, here's a little web app I wrote recently:

// global flags
var isdebug bool
var port int
var cert string
var key string
var dbdsn string
var dbmaxidle int
var dbmaxopen int
var imguri string

// init is the entry point for the entire web application.
func init() {

    log.Println("Starting wwwgo ...")

    // setup the flags
    //flag.StringVar(&host, "host", "", "Specify a host to redirect to. Use this to redirect all traffic to a single url.")
    flag.IntVar(&port, "port", 8080, "Specify the port to listen to.")
    flag.BoolVar(&isdebug, "isdebug", false, "Set to true to run the app in debug mode.  In debug, it may panic on some errors.")
    flag.StringVar(&cert, "cert", "", "Enables listening on 443 with -cert and -key files specified.  This must be a full path to the certificate .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.")
    flag.StringVar(&key, "key", "", "Enables listening on 443 with -cert and -key files specified.  This must be a full path to the key .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.")
    flag.StringVar(&dbdsn, "dbdsn", "root:root@tcp(localhost:3306)/dev_db?timeout=5s&tls=false&autocommit=true", "Specifies the MySql DSN connection.")
    flag.IntVar(&dbmaxidle, "dbmaxidle", 0, "Sets the database/sql MaxIdleConns.")
    flag.IntVar(&dbmaxopen, "dbmaxopen", 500, "Sets the database/sql MaxOpenConns.")
    flag.StringVar(&imguri, "imguri", "/cdn/uploads/", "Set this to the full base uri of all images, for example on a remote CDN server or local relative virtual directory.")
    flag.Parse()

    // log our flags
    if isdebug != false {
        log.Println("DEBUG mode enabled")
    }
    if cert != "" && key != "" {
        log.Println("Attempting SSL binding with supplied cert and key.")
    }
    if dbdsn != "" {
        log.Printf("Using default dbdsn: %s", dbdsn)
    }

    ...
}

This really becomes nice in staging/production environments.

A quick ./wwwgo -h for "what the heck was that parameter?" gives you full documentation:

admin@dev01:~/code/frontend/src/wwwgo [master]$ ./wwwgo -h
Usage of ./wwwgo:
  -cert="": Enables listening on 443 with -cert and -key files specified.  This must be a full path to the certificate .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.
  -dbdsn="root:root@tcp(localhost:3306)/dev_db?timeout=5s&tls=false&autocommit=true": Specifies the MySql DSN connection.
  -dbmaxidle=0: Sets the database/sql MaxIdleConns.
  -dbmaxopen=500: Sets the database/sql MaxOpenConns.
  -imguri="/cdn/uploads/": Set this to the full base uri of all images, for example on a remote CDN server or local relative virtual directory.
  -isdebug=false: Set to true to run the app in debug mode.  In debug, it may panic on some errors.
  -key="": Enables listening on 443 with -cert and -key files specified.  This must be a full path to the key .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.
  -port=8080: Specify the port to listen to.

Very nice to have many options at CLI, and no documentation required - it's built into the flags package.

You can clearly see the defaults immediately.

With this type of documentation, I tend to setup all the defaults for common "development environments" that the team uses. We all have root/root access to our local databases. We all are using port 8080 for this particular web app during development, etc. That way, you just have to run:

go build
./wwwgo

And the app runs with all defaults - defaults that are documented. In production, just override the defaults. The built-in flag parsers will panic the application if any parameters are in the wrong format, which is also very nice.

I see where you are going with this, but you should stop. The problem is, you don't want to ever 'execute' the configuration file because so many random things could wind up in there and cause you problems. Its better to use INI or JSON files. Load the files into dicts and use the dicts directly or populate a configuration object with the values from the dicts.

See the configparser: https://docs.python.org/2/library/configparser.html

It can increase memory usage, for both RAM and program code.

Structure padding

Most architectures require that variables are somehow aligned. For example 32-bit variables may have to be aligned to addresses divisible by 4 to have efficient access.

Structure members are required to be in order they are declared. This means that compilers may need to add padding to keep data access fast.

struct { // Structure aligment is 4, to keep 'a' aligned
    uint32_t a;
    uint8_t b;
    // Compiler inserts 3 byte padding here to keep 'c' aligned
    uint32_t c;
    //... other data here
} Module;

Compilers can arrange "free" variables in memory in a way that is space efficent. For structures you need to do this manually. Usually this is done by ordering members from largest to smallest, or opposite.

Note that structre packing that many compilers offer is not solution for this. It typically decreases performance and/or increases program size.

Variable placement

I am simplifying this but:

Typically globals are divided in 2 groups: zero initialized and value initialized (other than zero). Before starting main, these both have to be initialized.

For zero initialized variables linker typically just groups them together in one location, and then does one big memset to set all bytes to zero in that area before calling main function.

Value initialized variables are similarly grouped in other location and then at program startup one big memcpy is done to copy initial values from program memory (arranged in same order) on your variables.

If you combine all variables to struct, then you are forcing all initial values to be copied from program memory if any of the values is non-zero.


If you are on desktop machine, these might not be problem. However, on embedded system where memory is tight, these have to be taken into consideration.

Note that I am not saying that this is a bad idea. Just keep structures modular too and avoid massive monolith structures.

first of all i have to say email by just html or javascript will end up looking weird just use php instead it is also quiet easy:

/first make the variables of all the things you need
        $naam = $_POST['vnaam'];
        $mail = $_POST['email'];
        $achternaam = $_POST['anaam'];
        $tel = $_POST['telefoon'];
        $message = $_POST['commentaar'];

        $Name = $naam; //senders name 
        $email = $mail; //senders e-mail adress 
        $recipient = "receivers email"; //recipient 
        $mail_body =  //add the variables and text here by using . u get the same as + in javascript ;
        //mail body 
        $subject = "Contact formulier van: " . $naam . " " . $achternaam; //subject 
        $header = "From: ". $Name . " <" . $email . ">
"; //optional headerfields 
if ($_POST) {
 mail($recipient, $subject, $mail_body, $header); //mail command
 exit;
        }

now the if statement is the most important part. it is the part where the mail function is requested from. the pros of php is that the receiver doesnt get an email with something like: idname=Dave or idcheckbox=true or false (weither or not he box is checked)

Comments


Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .