Use a db to create a dynamic SVG


#1

I am, wondering if this would be possible?

have about 300 racks and have been asked with making a SVG out of em all
I noticed that all the racks either have 40,41,42,43,44,45 slots
and also not each rack has the same attributes
I think can us that and create a table

create table racks (
   rack_id INT NOT NULL AUTO_INCREMENT,
   location VARCHAR(100) NOT NULL,
   elevation VARCHAR(100) NOT NULL,
   cabinet VARCHAR(100) NOT NULL,
   power_panel VARCHAR(100) NOT NULL,
   circuit_breaker VARCHAR(100) NOT NULL,
   number_of_slots INT NOT NULL,
   PRIMARY KEY ( rack_id )
);

to hold all my racks
Then because only certain spaces would be taken up on each rack, can I use his stable to hel the utilized spaces inside each rack?

create table rack_slots (
   rack_slot_id INT NOT NULL AUTO_INCREMENT,
   rack_id VARCHAR(100) NOT NULL,
   slot_number_range INT NOT NULL,
   devic VARCHAR(100) NOT NULL,
   PRIMARY KEY ( rack_slot_id ),
   FOREIGN KEY ( rack_id )
);
then I can use INSERT statements to create the racks and fll the slots up
Then should I us a SELECT query to display all he racks?
I created a different template for the 6 different size of racks which would depend  on the number_of_slots record for each record in the racks table.Am I setting this up right?

#2

Well other than that I don’t think a single INT is sufficient to define ‘slot_number_range’, and you should probably spell ‘device’ correctly, sure.

rack_slot_id is technically an unnecessary artificial key (there exists a natural composite key (rack_id,slot_number_range), but its effectiveness is dependant on use case I suppose.


#3

ok, made a few corrections to the tables

create table racks (
   rack_id INT NOT NULL AUTO_INCREMENT,
   location VARCHAR(100) NOT NULL,
   elevation VARCHAR(100) NULL,
   title VARCHAR(100) NOT NULL,
   power_panel VARCHAR(100) NOT NULL,
   circuit_breaker VARCHAR(100) NOT NULL,
   number_of_slots INT NOT NULL,
   PRIMARY KEY ( rack_id )
);

create table rack_slots (
   rack_id INT NOT NULL,
   beginning_slot INT NOT NULL,
   ending_slot INT NOT NULL,
   device VARCHAR(100) NOT NULL,
   FOREIGN KEY ( rack_id )
);

I corrected my typo, got rid of the unnecessary PK, and got rid of the range thing and instead went with a beginning and end value which will be numbers.
I think alls good so can I start populating the tables like

INSERT INTO racks ( location, elevation, title, power_panel, circuit_breaker, number_of_slots) VALUES ( 'Server Room', '255-A', 'Cabinet 3', '2', '4', 45 );

and

INSERT INTO rack_slots ( rack_id, beginning_slot, ending_slot, device) VALUES ( 1, 4, 9, 'A Device');

#4

It should be noted that you should still define a PK, although it can be a composite natural one.

Depending on your database engine, something along the lines of

ALTER TABLE rack_slots ADD PRIMARY KEY (rack_id,beginning_slot)

#5

ok, think I got some of the code down,
I created the 2 tables and inserted 1 record into each.

But as a test, I want to create 1 rack
http://lukesspot.com/indus_links/create-rack.php?id=1



<?php
$servername = "";
$username = "";
$password = "";
$dbname = "racks";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} ?>
<html>
 <head>
 </head>
 <body>
<?php
$rack_id = $_GET['id'];
$sql = "SELECT number_of_slots, location, elevation, title, circuit_breaker, power_panel FROM racks WHERE rack_id = ".$rack_id;
$result = $conn  ->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row["location"]. ", " . $row["elevation"]. ", " . $row["circuit_breaker"]. ", " . $row["title"]. ", " . $row["power_panel"]. ", " . $row["number_of_slots"]. "<br>";


switch ($row['number_of_slots']) {
	
	case 45:
		$myfile = fopen("templates/45_slot.svg", "a") or die("Unable to open file!");
		echo fread($myfile,filesize("templates/45_slot.svg"));
		fclose($myfile);		
		break;
	case 44:
		echo"Use 44 rack templarte";
		break;
	case 43:
		echo"Use 43 rack templarte";
		break;
	case 42:
		echo"Use 42 rack templarte";
		break;
	case 41:
		echo"Use 41 rack templarte";
		break;
	case 40:
		echo"Use 40 rack templarte";
		break;
	default:
		echo 'WTF?';
}
    }
} else {
    echo "0 results";
}
$conn->close();
?>
</body>
</html>

The template exists,
http://lukesspot.com/indus_links/templates/45_slot.svg
why is it not shown?


#6

I notice that ifI simply include it, the svg appears,

	case 45:
		$myfile = fopen("templates/45_slot.svg", "a") or die("Unable to open file!");
		include("templates/45_slot.svg");;		
		break;

http://lukesspot.com/indus_links/create-rack.php?id=1
How do I copy/rename the SVG, then add links or slots from the rack_slots table?


#7

am getting this warning


didn’t I use copy correctly?

	case 45:
		$my_file = "templates/45_slot.svg";
		include($my_file);
		copy($my_file, '../racks/cabinet_'.$rack_id.'.svg');
		echo $my_file.' => ../racks/cabinet_'.$rack_id.'.svg';
		break;

#8

The error states there is no file with that name.

Is the count range 0…44 and 45 is out of range?


#9

no,the count is only 40-45 so 6 possible values
Im simply trying to create a SVG, which should be an exact copy of
http://lukesspot.com/indus_links/templates/45_slot.svg
and place it in a separate directory (I figure Id need to create the file first

$my_new_file = fopen('../racks/cabinet_'.$rack_id.'.svg', "w") or die("Unable to open file!");

But does;t that create a file and allow it to be written to, cause,
http://lukesspot.com/indus_links/create-rack.php?id=1
Cause the next step would be adding some links to the newly created SVG


#10

The error also states “or directory”. Does the directory exist?

I usually try writing to the same directory and if that works then expand the path.

Does the desired directory have write permissions set to 0777?


#11

that was it, changed the permissions

Now that I have a blank file,
http://lukesspot.com/indus_links/racks/cabinet_1.svg
Hop do I copy whats in the template file to it so I can start adding links?


#12

Please supply more information:

  1. “whats in the template file” - please tell us

  2. “to it” - what is it?

  3. “adding links” - to what?

  4. have you enabled delare(string_types=1);, error_reporting(-1);, ini_set(‘display_errors’, ‘true’);

  5. any errors or warnings?

  6. what have you tried?


#13

my bad

  1. The template file,
    http://lukesspot.com/indus_links/templates/45_slot.svg

  2. copy it to the file we just created
    http://lukesspot.com/indus_links/racks/cabinet_1.svg

  3. Id like to add links inside the new SVG so I can fill some of the slots in the rack

  4. I dont even know how to edit the SVG so I can add links inside using php
    I was thinking, can I create (instead of a SVG file, a PHP file
    Then edit that, is there a way to save the code outputted in the php file to a SVG file?


#14

ok, I copied the fie and gave it a
PHP extension
http://lukesspot.com/indus_links/populate_rack.php?id=1
with

$svg = show_source('racks/cabinet_'.$rack_id.'.php');
echo $svg;	

I’m sshowing the markup of the file that was created…
Now to add the links, I’l l use PHP to do this, but how do I add them just after the second

<rect>

(just before all the
<text>
Then, how do I save the file to give it a .svg extension? rename()