dongzi1209 2013-10-31 00:35
浏览 16

从一组信息中插入数据?

I am not sure how to approach this, I need some advice on inserting data from a list of names. This is statistical type of data. Say I have an array that looks like this.

$basketball = array (
    'teams' => array (
        'orange' => array (
            'names'  => array(
                'matt' => array('number' => '12', 'position' => 'guard'), 
                'john' => array('number' => '23', 'position' => 'forward'),
            ),
        ),
        'blue' => array (
            'names'  => array(
                'luke' => array('number' => '34', 'position' => 'guard'), 
                'brian' => array('number' => '45', 'position' => 'forward'),
            ),
        ),
    ),
);

Then inside my app I run a foreach on the names like this.

<?php foreach($basketball as $key => $teams): ?>
    <?php foreach($teams as $team => $names): ?>
    <div class="teams <?php echo $team ?>" data-score="0">
        <?php foreach($names as $key => $players): ?>
            <?php foreach($players as $player => $player_info): ?>
                <div class="home-stats stats <?php echo $team ?>" data-player="<?php echo $player ?>">
                    <ul>
                        <li class="name">
                        <span><?php echo $player ?></span>
                        <input type="hidden" name="home-playername" value="<?php echo $player ?>" />
                        </li>
                        <li class="points">
                        <span>0</span>
                        <input type="hidden" name="home-points" value="0" />
                        </li>
                    </ul>
                </div>
            <?php endforeach; ?>    
        <?php endforeach; ?>
    </div>   
    <?php endforeach; ?>
<?php endforeach; ?>

With this I am trying to figure out the best approach for adding the data, so that points matches the player, obviously I will have more stats and players but for the sake of example this is all I need.

I was thinking of using json and rewriting the name attributes with jQuery and then building an array, and having a row for each team name, but again I can't think of how to rename these inputs to build a solid array, that looks like (sorry if Its a bit messed up but it's just an idea)

  {"players":[{"matt":[{"stats":[{"points":"22"}]}], 
               "john":[{"stats":[{"points":"18"}]}]}]}

I guess this may be a pretty broad question, but summed up given the information if you were the developer how would you approach statistical style of data, the name of the player being the parent of all the information... OR... possibly the team name being the parent of all the information, either way once I figure the best approach I can go either way..

  • 写回答

1条回答 默认 最新

  • dongyudun6465 2013-10-31 00:53
    关注

    You should use a plugin like ember.js, angular.js or backbone.js. You should create a simple RESTful API to retreive the data from your server. To accomplish this task, proceed into three steps :

    1- Put your data in a database Make sure that all relation are well thought. Don't throw random information in your database. When I look at your example, it already seems like you have an issue with the way you organize your data. The names should be an attribute of an object, not the name of a key. Use the power of OOP.

    2- Map your database into objects Save yourself huge trouble and use doctrine. It helps you map data and relation between your entity. You could map your whole application with less then 50 lines of code.

    3- Return the data Return it in whatever format you need. JSON is very mainstream but you could decide to return XML or YAML. It's up to you. Use whatever you feel confortable with.

    I personnaly use Ember.js. It uses handlebars for your template. In the end your code should be super short and clean:

    <div id="comments">
        {{#each player}}
            <h2>#{{id}}">{{name}}</a></h2>
            <div>{{points}}</div>
        {{/each}}
    </div>
    

    If you don't know how these frameworks works take the time to learn them. The way you're doing it right now is wrong. Take the best habit possible and learn how to build a solid application using MVC architecture.

    Hope it helps

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集