I'm calling the same php function, in which I pass as a parameter a result of the database, twice in the script but it only works on the first call.
I noticed that if I call the function that returns the result of the query and move it to another variable that is passed as a parameter to the function that did not work before, it starts to work.
I know I'm letting some point of php work, I'd like a hand in it.
Remembering that I'm calling the same function because I need a select exactly the same as the above, does anyone suggest any more optimized way to do this? Because the function loads approximately 13,000 bank records which makes the page take a long time to load.
<!--TIMES-->
<div class="page-header">
<h4>Times</h4>
<hr>
</div>
<?php
$registro = lista_times();
//$registro2 = lista_times();
?>
<div style="padding: 20px; align-content: center" class="row" id="times-row">
<div class="col-sm-6">
<div class="form-group">
<label for="time_casa" class="col-sm-2 control-label">Casa</label>
<div class="col-sm-10">
<?php //var_dump($registro); ?>
<select style="width: 300px" class="form-control chosen-select" id="time_casa" name="time_casa" data-error="Por favor, selecione o time de casa." required>
<?php echo preenche_time_combo($registro); ?>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<?php //var_dump($registro); ?>
<label for="time_fora" class="col-sm-2 control-label">Fora</label>
<div class="col-sm-10">
<select style="width: 300px" class="form-control chosen-select" id="time_fora" name="time_fora" data-error="Por favor, selecione o time de fora." required>
<?php echo preenche_time_combo($registro); ?>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
<!--TIMES-->
lista_times()
executes the query and returns the result and preenche_time_combo()
returns a string with the html code with an option for each element retrieved from the database.
//Lista todos os jogos ativos
function lista_times()
{
$link = conectar();
$query = "SELECT tb_time.id as id_time, tb_time.nome_time, tb_campeonato.nome_camp
FROM tb_campeonato, tb_time
WHERE tb_time.tb_campeonato_id = tb_campeonato.id";
$result = mysqli_query($link, $query) or die(print_r(mysqli_error()));
return $result;
}
function preenche_time_combo($result)
{
$header_atual="";
$html="";
while ($registro = mysqli_fetch_assoc($result)) {
if($registro['nome_camp'] != $header_atual){
if($header_atual != ""){
$html .= "</optgroup>";
}
$html .= "<optgroup label='".$registro['nome_camp']."'>";
$header_atual = $registro['nome_camp'];
}
$html .= "<option value='" . $registro['id_time'] . "'>" . $registro['nome_time'] . "</option>";
}
$html .= "</optgroup>";
return $html;
exit();
}