Skip to content

Instantly share code, notes, and snippets.

@imme-emosol
Created December 18, 2011 00:38
Show Gist options
  • Save imme-emosol/1491939 to your computer and use it in GitHub Desktop.
Save imme-emosol/1491939 to your computer and use it in GitHub Desktop.
bestelling doen
<?php
/** Deze 3 regels in een algemeen stuk code ( bijv. index.php ) : **
session_start();
if ( !isset( $_SESSION[ 'bestellingen' ] ) )
$_SESSION[ 'bestellingen' ] = array();
/**/
/** Dit ook in een algemeen deel: **
$db = new MySQLi( 'localhost' , 'root' , '' , 'webwinkel' );
/**/
do
if ( isset( $_POST[ 'afrekenen' ] ) )
{
$afrekenlijst = array();
foreach ( $_POST as $pid => $aantal )
{
if ( 'product_' !== substr( $pid , 0 , 8 ) )
continue;
$afrekenlijst[ 1 * substr( $pid , 8 ) ] = 1 * $aantal;
}
$sql = 'CREATE DATABASE IF NOT EXISTS bestellingen ( bestelling_id INT( 3 ) UNSIGNED NOT NULL AUTO_INCREMENT , klant_data VARCHAR( 222 ) NOT NULL , product_data VARCHAR(222) NOT NULL ) AUTO_INCREMENT = 7 DEFAULT CHARACTER SET=utf8_general_ci DEFAULT COLLATE = utf8 ';
$db->query( $sql );
$klant_data = '';
if ( !isset( $_POST[ 'naam' ] , $_POST[ 'adres' ] ) )
break;
$product_data = '';
foreach ( $afrekenlijst AS $pid => $no )
$product_data .= $db->real_escape_string( $pid . ':' . $no . ';' );
if ( empty( $klant_data ) || empty( $product_data ) )
break;
$sql = 'INSERT INTO bestellingen ( klant_data , product_data ) VALUES ( \'' . $klant_data . '\' , \'' . $product_data . '\' )';
$db->query( $sql );
}
while ( FALSE );
/** Dit stuk code kan ook in een algemeen deel : **
$bestellijst = array();
if ( !empty( $_SESSION[ 'bestellingen' ] ) )
{
$db = new MySQLi( 'localhost' , 'root' , '' , 'webwinkel' );
$sql = 'SELECT * FROM `producten` WHERE product_id IN(' . implode( ' , ' , array_keys( $_SESSION[ 'bestellingen' ] ) ) . ') ';
$r = $db->query( $sql )
OR die( '<h2>Oeps, er ging iets mis.</h2>' . $db->error . '<hr />' . $sql . '<pre>^^</pre>' );
while ( $product = $r->fetch_assoc() )
$bestellijst[] = $product + array( 'aantal' => $_SESSION[ 'bestellingen' ][ $product[ 'product_id' ] ] );
}
/**/
## making required variables for output available.
if ( !isset( $bestellijst ) )
$bestellijst = array();
?>
<form action="<?php echo $_SERVER[ 'REQUEST_URI' ]; ?>" method="POST">
<dl>
<dt><label for="naam">naam</label></dt>
<dd><input type="text" name="naam" id="naam" value="<?php echo htmlentities( @$_POST[ 'naam' ] ); " /></dd>
<dt><label for="adres">adres</label></dt>
<dd><input type="text" name="adres" id="adres" value="<?php echo htmlentities( @$_POST[ 'adres' ] ); " /></dd>
</dl>
<input type="hidden" name="afrekenen" value="TRUE :þ " />
<?php foreach ( $bestellijst as $product ) : ?>
<input type="hidden" name="product_<?php echo $product[ 'product_id' ]; ?>" value="<?php echo $product[ 'aantal' ]; ?>" />
<?php endforeach; ?>
<input type="submit" name="actie" value="afrekenen" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment