聚合物和PHP [英] Polymer and PHP

查看:98
本文介绍了聚合物和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我才刚刚开始学习Polymer. 我使用PHP收集以下数据.我可以将其嵌入到DIV中并显示一些数据,但是我想将这段代码放入具有可导入参数的Polymer元素中,并将json数据也放入可用于填充页面的外部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);

?>      

推荐答案

如果我理解您需要在我的情况下创建服务元素:

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>

您可以正确获取数据

<?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();

?>

希望对您有帮助

这篇关于聚合物和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆