HiveBrain v1.2.0
Get Started
← Back to all entries
snippetphpMinor

Best way to create SVG from database? (Using PHP)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
createphpwaydatabasesvgusingfrombest

Problem

I've got a SQL database that keeps track of which services a client has. I need to create an image using SVG that only displays the services that the client has. The database stores the services in the format 1,2,3,6,7,9. Here's what I have:

prepare("SELECT services FROM *** where name= :name");
    $grabServices->execute(array(":name" => "test" ));
    $services = $grabServices->fetchAll();
    $services = explode(",", $services[0][services]);
    var_dump($services);
?>

//snip...

    

    

//etc, etc,


This seems incredibly tedious to me. The only other way I can think to do this would have all the SVG elements created in a PHP array, but that seems even more wasteful. Is there a better way to do this?

Solution

 array(
        'x1' => 820,
        'y1' => 120
    ),
    '2' => array(
        'x1' => 905,
        'y1' => 180
    )
);
?>

 $format ) : ?>
    
        " y1="" />
    


I didn't do it for every field, but you get the idea.

This is tedious to write the array. But you have to write it somewhere. If there is no logic, the computer can't guess.

I think it's easier to read/write a simple array than all this XML though.

Code Snippets

<?php
$formats = array(
    '1' => array(
        'x1' => 820,
        'y1' => 120
    ),
    '2' => array(
        'x1' => 905,
        'y1' => 180
    )
);
?>

<?php foreach( $formats as $key => $format ) : ?>
    <?php if ( in_array( (int) $key, $services ) ) : ?>
        <line x1="<?php print $format['x1']; ?>" y1="<?php print $format['y1']; ?>" />
    <?php endif; ?>
<?php endforeach; ?>

Context

StackExchange Code Review Q#13494, answer score: 4

Revisions (0)

No revisions yet.