Compare multiple foreach results

Asked

Viewed 197 times

0

I need to compare between two tables the values saved in the bank and bring in an option the results (tb_portfolios and tb_portfolios), if more than one record should be inserted the Selected.

In the table tb_wallets I have 10 records with id and name In table tb_portfolios I have 2 records with id and wallet

I need to select it to give the Selected="Selected" when the id.tb_wallets and wallet.tb_wallets are equal

In the script below only happens in the first comparison record and does not pass to the second.

    <select class="selectpicker" data-style="select-with-transition" multiple title="Selecione a Carteira:<code>*</code>" required="true" aria-required="true" aria-invalid="true" title="Carteira" name="carteira[]" id="carteira">
<?php
$readSes = new Read;
$readSes->FullRead("
SELECT
tb_carteiras.nome,
tb_carteiras.id,
tb_carteirasclientes.carteira
FROM `tb_carteiras` 
LEFT JOIN `tb_carteirasclientes` 
ON `tb_carteirasclientes`.carteira = `tb_carteiras`.id
ORDER BY
tb_carteiras.id ASC
");
if (!$readSes->getResult()):
    echo '<option disabled="disabled" value="null"> Nenhuma Carteira cadastrado. </option>';
else:
    foreach ($readSes->getResult() as $ses):
        echo "<option value=\"{$ses['id']}\" ";
        if ($ses['id'] == $ClienteData['carteira']):
            echo ' selected="selected" ';
        endif;
        echo "> {$ses['nome']} </option>";
    endforeach;
endif;
?>
</select>
  • 1

    Okay... pardon my innocence, but what’s going on? Isn’t it working or is it bringing something wrong as a result? Going through the code I didn’t see anything that could be wrong from my point of view...

  • As @Rodrigotognin said, try to be more complete... is SQL returning the right values? If not, what is the database structure? If yes, what is the resulting HTML? And the result it should have?

  • Maybe I didn’t explain it right, in this auxiliary table I can have 2 records that are in the main table, and I need this data to be checked and only 1 is coming

  • You do the main select, read on foreach, while doing another query and comparing the results. Wouldn’t that be?

  • Currently I only do this foreach, I wanted to know how to compare both so that in select stay more than one field

1 answer

0

I was able to find the error, I don’t know if it is the best practice but it worked, I used the $ses['wallet'] comparative variable to compare the $ses['id'] inside the foreach, and it came right. (below)

foreach ($readSes->getResult() as $ses):
 echo "<option value=\"{$ses['id']}\" ";
 if ($ses['id'] == $ses['carteira']):
  echo ' selected="selected" ';
 endif;
 echo "> {$ses['nome']} </option>";
endforeach;

Thanks to those who helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.