0
I am calling the same php function, in which I pass as parameter a database result, twice in the script but it only works in the first call.
I realized that if I call the function that returns the query result and pass to another variable that in turn is passed as parameter to the function that previously did not work, it goes to work.
I know I’m missing some point of php working, I’d like a hand in this.
Remembering that I am calling the same function because I need a select exactly like the previous one, does anyone suggest any more optimized way to do this? Because the function carries 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 html code with an option for each element recovered 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();
}
Amigo 2 combos with 13 thousand records will claw, you ever tried using an ajax to mount an autocomplete? Want some plugin suggestions?
– Euler01
@Euler01, I would love some suggestions. I did some research on how to optimize this but could not find a satisfactory solution. Thank you if your friend can help me, hug!
– Guilherme Ramalho
The
mysqli_fetch_assoc
, according to the documentation, "returns an associative matrix that corresponds to the obtained line and moves the internal pointer of the data forward." That is, you would need to restart the pointer to the first record to go through the result again (withmysqli_data_seek
). I’m not sure it’s a good alternative.– Caesar
Are you really going to go through with the 13,000 records on one page? http://answall.com/questions/146765/optimizar-chargingde-select-phpmysql Now in addition to 13 mì there are 26,000.. and all within select option...
– Daniel Omine
Hello @Guilhermeramalho, take a look at https://select2.github.io and look for "Loading remote data". You’ll need to import Bootstrap css as well. Or good old http://jqueryui.com/autocomplete/
– Euler01