简体   繁体   中英

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. 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". The function goes and retrieves games and their data with the following:

// function actionPunt in CompetitionsController

$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:

$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

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. The form has the following elements:

//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. However I do not want to display "team1" and "team2". I want to display the name of team. I have tried many things including the following:

Firstly in the function call to javascript:

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

I have tried adding the parameters: $g->team1->name and $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

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. 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.

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!" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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