Beginning PHP4 | 8 | WebReference

Beginning PHP4 | 8

To page 1current pageTo page 3
[previous] [next]

Beginning PHP4

Adding data

Here's a SQL script to populate our mall table with its data.

INSERT INTO mall VALUES (1,'G','G1','Fast Snax',
            '555-7089','150,0,150,100,200,100,200,0','175,50',
            'food,sweets,soda,drinks,chips,cola,pies,fast food,candy');
INSERT INTO mall VALUES (2,'G','G2','ACME Newsagent',
            '555-6843','300,0,300,100,350,100,350,0','325,50',
            'newspapers,magazines,paper,stationary,newsagents,periodicals');
INSERT INTO mall VALUES (3,'G','G3','Fashion Warehouse',
            '555-7521','350,0,350,100,400,150,500,150,500,0','425,65',
            'clothes,clothing,shoes,pants,suits,dresses');
INSERT INTO mall VALUES (4,'G','G4','CD&#059; R Us',
            '555-0459','400,150,400,250,500,250,500,150','450,200',
            'cds,dvds,music,tapes,videos,rock,pop');
INSERT INTO mall VALUES (5,'G','G5','Garden Bounty Florists',
            '555-9561','400,250,400,350,500,350,500,250','450,300',
            'gardening,flowers,florists,arrangements,floral');
INSERT INTO mall VALUES (6,'G','G6','National Bank',
            '555-3675','400,350,350,400,350,500,500,500,500,350','425,440',
            'money,banks,banking,cheques,deposit,withdrawal,cash,atm');
INSERT INTO mall VALUES (7,'G','G7','Sweetime',
            '555-7659','300,400,300,500,350,500,350,400','325,450',
            'candy,sweets,candy floss,cola,sodas,drinks,chips,crisps');
INSERT INTO mall VALUES (8,'G','G8','The Classics',
            '555-4395','150,400,150,500,200,500,200,400','175,450',
            'cds,dvds,music,jazz,classics');
INSERT INTO mall VALUES (9,'G','G9','The Computer Matrix',
            '555-7001','100,350,150,400,150,500,0,500,0,350','65,435',
            'pc,computers,components,software,hardware,books,cds');
INSERT INTO mall VALUES (10,'G','G10','Bibliophile',
            '555-3752','100,250,100,350,0,350,0,250','50,300',
            'books,magazines,periodicals,reading');
INSERT INTO mall VALUES (11,'G','G11','Well Heeled',
            '555-2564','0,150,100,150,100,250,0,250','50,200',
            'shoes,trainers,boots,heels');
INSERT INTO mall VALUES (12,'G','G12','The Great Outdoors',
            '555-7269','0,0,0,150,100,150,150,100,150,0','65,70',
            'hiking,camping,rafting');

We can save it as map_data.sql and import it into our mapping database:


> mysql -u phpuser -pphppass mapping < map_data.sql

Testing our data

The coordinates for the outline of the shops are now all stored in our database. In order to test that everything is working correctly we are going to write a script to create an image of our mall.

Try It Out - Drawing The Mall
<?php
//drawmall.php
include "./common_db.inc";
Header("Content-type: image/png");
$image = ImageCreate(501,501);
$white = ImageColorAllocate($image,255,255,255);
$black = ImageColorAllocate($image,0,0,0);
$link_id = db_connect('mapping');
$query = "SELECT m_area FROM mall";
$mallResult = mysql_query($query,$link_id);
while ($mallRow = mysql_fetch_array($mallResult)) {
   $area = explode(",", $mallRow[0]);
   for ($i=0; $i &lt; count($area)-2; $i=$i+2) {
      ImageLine($image,$area[$i],$area[$i+1],$area[$i+2],$area[$i+3],$black);
   }
   ImageLine($image,$area[count($area)-2],
                    $area[count($area)-1], $area[0], $area[1], $black);
}
for ($i=0; $i&lt;count($area)-2; $i=$i+2) {
   ImageLine($image,$area[$i], $area[$i+1], $area[$i+2], $area[$i+3], $black);
}
ImageLine($image, $area[count($area)-2],
                  $area[count($area)-1], $area[0], $area[1], $black);
   ImagePNG($image);
   ImageDestroy($image);
   mysql_free_result($mallResult);
?>

When you run this script, you should see the image below:

2

To page 1current pageTo page 3
[previous] [next]

and
Revised: March 19, 2001
Created: March 19, 2001

URL: https://webreference.com/programming/php/beginning/chap16/2/