Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymer and PHP

Tags:

php

polymer

Hi I am just beginning to learn Polymer. I collect data as below using PHP. I can embed this in a DIV and display some data but I would like to make this bit of code into a Polymer element with parameters that I can import and the json data also into an external Polymer element that I can use to populate the page. I am struggling to get started, I know this is basic but if anyone has a pointer to get me on my way that would be great.

<?php 
$host="host";
$username="username"; 
$password="password"; 
$db_name="database";
$db_query="SELECT * FROM table"; 

$mysqli = new mysqli("$host", "$username", "$password", "$db_name");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$mysqli->real_query("$db_query");
$res = $mysqli->use_result();
$rows = array();
while ($row = $res->fetch_assoc()) {
    $rows[] = $row;
}
print json_encode($rows);

?>      
like image 796
user2148935 Avatar asked Jul 19 '14 16:07

user2148935


1 Answers

If I understand you need to create service element in my case:

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/core-ajax/core-ajax.html">

<polymer-element name="category-service" attributes="categories">
    <template>
        <style>
        :host {
          display: none;
        }
        </style>
        <core-ajax id="ajax"
        auto
        url="../api/get_category_data.php"
        on-core-response="{{categoriesLoaded}}"
        handleAs="json">
        </core-ajax>
    </template>
    <script>
        Polymer('category-service',
        {
            created: function()
            {
                this.categories = [];
            },

            categoriesLoaded: function()
            {
                console.log('call cat loaded');
                this.categories = this.$.ajax.response.slice(0);
            }
        });
    </script>
</polymer-element>

then you need to create element to display data

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/paper-item/paper-item.html">
<link rel="import" href="category-service.html">

<polymer-element name="category-list" attributes="show">
    <template>
        <style>
        :host {
          display: block;
          width: 100%;
        }
        .paper_item 
        {
            margin: 10px;
            background-color: rgb(255, 255, 255);
        }
        </style>
        <category-service id="service" categories="{{categories}}"></category-service>

        <template repeat="{{category in categories}}">
            <paper-item label="{{category.category_name}}"
                        icon="settings"
                        class="paper_item"
                        center horizontal layout>
            </paper-item>
        </template>
    </template>
    <script>
        Polymer('category-list',
            {
            }
        );
    </script>
</polymer-element>

for getting data you do right

<?php

    require_once 'DB_Connect.php';

    $db = new DB_Connect();
    $db->connect();

    $result = mysql_query("SELECT * FROM ad_category") or die(mysql_error());

    while($row=mysql_fetch_assoc($result))
    $output[]=$row;
    print(json_encode($output));
    mysql_close();

?>

hope it help for you

like image 180
Donearh Avatar answered Oct 03 '22 23:10

Donearh