samedi 25 avril 2015

creating an array from database


I'm using a array to get a range between 1011-2371 but I'didnt need all digits. It's a long list thats why I import the CSV to the database.

I want to create a array from a database table. But I can't get it.

Here's a rough mockup of the old php:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <input type="text" placeholder="1234AB" name="postcode" />
    <input type="submit" value="verstuur" />
</form>
<?php

if($_SERVER['REQUEST_METHOD'] == "POST") {
    $postcode = range(1011,2371);
    if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
        if(in_array($_POST['postcode'],$postcode)) {
            echo 'FreshFoods is beschikbaar bij jou in de buurt.';
        } else {
            echo  'FreshFoods is nog niet beschikbaar bij u in de buurt.';
        }
    } else {
        echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
    }
}
?>

and this is what I try do get the array from the database:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <input type="text" placeholder="1234AB" name="postcode" />
    <input type="submit" value="verstuur" />
</form>
<?php
require_once 'db_config.php'; 

if($_SERVER['REQUEST_METHOD'] == "POST") {
    $postcode = array();
    $result = mysql_query("SELECT postcode FROM postcode_check");

    if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
        if(in_array($_POST['postcode'],$postcode)) {
            echo 'FreshFoods is beschikbaar bij jou in de buurt.';
        } else {
            echo  'FreshFoods is nog niet beschikbaar bij u in de buurt.';
        }
    } else {
        echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
    }
}
?>

the db_config.php file looks like:

<?php 
$db = array ( 
    'host' => 'localhost', 
    'user' => 'root', 
    'pass' => 'root', 
    'dbname' => 'testzip' 
); 

if(!mysql_connect($db['host'], $db['user'], $db['pass'])) 
{ 
    trigger_error('Fout bij verbinden: '.mysql_error()); 
} 
elseif(!mysql_select_db($db['dbname'])) 
{ 
    trigger_error('Fout bij selecteren database: '.mysql_error()); 
} 
else 
{ 
    $sql = "SET SESSION sql_mode = 'ANSI,ONLY_FULL_GROUP_BY'"; 
    if(!mysql_query($sql)) 
    { 
        trigger_error('MySQL in ANSI niet mogelijk'); 
    } 
} 
?>


Aucun commentaire:

Enregistrer un commentaire