简体   繁体   English

yii,php,如何访问/解析数组中的正确记录/选定记录

[英]yii, php, how to access/ parse the correct record/ selected record in an array

I am a beginner with yii and I am having trouble accessing the correct item in an array. 我是yii的初学者,无法访问数组中的正确项目。 I was hoping to get some help. 我希望得到一些帮助。 I am going to paraphrase the code here. 我将在这里解释代码。 If it is easier to enter the actual code let me know. 如果更容易输入实际代码,请告诉我。

My view "punt" is rendered and the "actionPunt" function is called in the "CompetitionsController". 渲染了我的视图“平底船”,并在“ CompetitionsController”中调用了“ actionPunt”函数。 The function goes and retrieves games and their data with the following: 该函数执行以下操作,并检索游戏及其数据:

// function actionPunt in CompetitionsController // CompetitionsController中的actionPunt函数

$model=Competitions::model()->with(array('round', 'round.games', 'round.games.team1', 'round.games.team2', 'round.games.league',))  ->findByPk($id);

For every ($model->round->games as $g) the following two things occur: 对于每个($ model-> round-> games $ g),发生以下两件事:

$leagues[$g->league_id] = array('name'=>$g->league->name, 'image'=>$g->league->image, 'federation'=>$g->league->federation_image);

$games[$g->league_id][$g->group][] = $g;

Then the view is rendered with the following: 然后,使用以下内容呈现视图:

$this->render('punt', array('model'=>$model, 'games'=>$games, 'leagues'=>$leagues, 'default_league'=>$default_league,));

//punt view //平底船视图

<table>
For every ($games as $game)
{   
    for every ($game as $group)
    {
        $grp=reset($group);

        for every ($group as $g)
        {
            <tr><td><?php echo CHtml::image($image_url.$g->team1->image, $g->team1->name); ?></td>      
                <td><?php echo CHtml::image($image_url.$g->team2->image, $g->team2->name); ?></td>
                <td><?php if($g->hasGameStarted())
                    {
                          echo CHtml::link('Punt', '#', array('onclick'=>'showDialog('.$model->id.','.$g->id.'); return false;',));
                    } ?>
            </td></tr>
        }
    }
}

So basically I have a list of games, with team1 and team2, the games are grouped by league, when a league is clicked the games are refreshed to display the games in that league 所以基本上我有一个游戏列表,其中有team1和team2,这些游戏按联赛分组,当单击联赛时,刷新游戏以显示该联赛中的比赛

function showDialog(comp,game)
{   
     $("#competition_id").val(comp);    
     $("#game_id").val(game);   
     $("#punt_dialog").dialog("open");
}

punt_dialog called from the showdialog function is a CJuiDialog widget that is used to draw a form. 从showdialog函数调用的punt_dialog是一个CJuiDialog小部件,用于绘制表单。 The form has the following elements: 该表单具有以下元素:

//form id "punt_form" //表单ID“ punt_form”

<label><input type="radio" name="punt" value="1">team1</label><label><input type="radio" name="punt" value="2">team2</label>

<label><input type="hidden" id="game_id" name="game_id" value="0" /><input type="hidden" id="competition_id" name="competition_id" value="0" /><input type="submit" value="Punt!" />

The code works fine, the dialog displays I can select team1 or team2 and the selection is saved and displayed correctly. 代码工作正常,对话框显示“我可以选择team1或team2”,并且选择内容已保存并正确显示。 However I do not want to display "team1" and "team2". 但是我不想显示“ team1”和“ team2”。 I want to display the name of team. 我想显示团队名称。 I have tried many things including the following: 我尝试了很多事情,包括以下内容:

Firstly in the function call to javascript: 首先在对javascript的函数调用中:

showDialog('.$model->id.','.$g->id.');

I have tried adding the parameters: $g->team1->name and $g->team2->name 我尝试添加以下参数:$ g-> team1-> name和$ g-> team2-> name

then adding two more parameters to: 然后再添加两个参数:

function showDialog(comp,game)

However the result of this is the dialog box does not appear when the link is cleared, infact the screen, the whole screen moves up and to the left a pixel or two. 但是,这样做的结果是,清除链接后,对话框没有出现,但实际上是在屏幕上,整个屏幕向上和向左移动了一两个像素。

Secondly I have tried the following: 其次,我尝试了以下方法:

<label><input type="radio" name="punt" value="1">$g->team1->name</label>

This proved a false dawn as this value always contains team1 of the first game in the array 这证明是虚假的曙光,因为此值始终包含阵列中第一场比赛的team1

And lastly I also tried as a test, in the form itself: 最后,我还尝试了以下形式的测试:

   $testingteam=Game::model()->findByPk($g->id);

and then 接着

<label><input type="radio" name="punt" value="1"><?php echo $testingteam->league_id; ?></label>

However this also always displayed the league of the first game in the array. 但是,这也始终显示阵列中第一场比赛的联赛。

My goal is to display the teams and league of the selected game in the punt_dialog widget/form. 我的目标是在punt_dialog小部件/表单中显示所选游戏的球队和联赛。 Note the two values parsed to showDialog are competition_id and game_id, competition_id is different from league, there are many leagues in a competition. 请注意,解析到showDialog的两个值是Competition_id和game_id,Competition_id与League不同,一个竞赛中有很多联赛。

Thanks in advance for any help. 在此先感谢您的帮助。

Brun. 布伦

Whats wrong with: 有什么问题:

echo CHtml::link('Punt', '#', array(
     'onclick'=>'showDialog(
             '.$model->id.','.$g->id.', 
             "'.$g->team1->name.'", 
             "'.$g->team2->name.'"); 
      return false;',));
function showDialog(comp,game, team1, team2)
{   
     $("#competition_id").val(comp);    
     $("#game_id").val(game);   
     $("#team1").text(team1);   
     $("#team2").text(team2);   
     $("#punt_dialog").dialog("open");
}
<label id="team1"><input type="radio" name="punt" value="1">team1</label>
<label id="team2"><input type="radio" name="punt" value="2">team2</label>

<label><input type="hidden" id="game_id" name="game_id" value="0" />
<input type="hidden" id="competition_id" name="competition_id" value="0" />
<input type="submit" value="Punt!" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM