Print result of an array in php list format

Asked

Viewed 176 times

0

I have two arrays in php, which I compare and create a third and want to print this difference on a list.

I’ve tried the foreach, but this method prints a huge list in string.

This is my code:

$sql = mysqli_query($cx, "SELECT IPS FROM ipvalidos ORDER BY IPS ASC ");

while($aux = mysqli_fetch_assoc($sql)) {
    $arrayip[ ] = $aux["IPS"];
        }

$diferenca = array_diff($arrayip, $arraycompara);

foreach ($diferenca as $diferencas)
{
        $stringArrayF = $stringArrayF.$diferencas;
}       
echo $stringArrayF;

The printed result comes out:

192.168.0.1192.168.0.2192.168.0.3192.168.0.4192.168.0.5...

I wish I could put that result into one select html

<select><option value="$stringArrayF">$stringArrayF</option>;

3 answers

0

<html>
$sql = mysqli_query($cx, "SELECT IPS FROM ipvalidos "); 






while($aux = mysqli_fetch_assoc($sql)) { 
	$arrayip[ ] = $aux["IPS"]; 
} 

$diferenca = array_diff($arrayip, $arraycompara); 

?> 




<body>
<select>
<?php	foreach ($diferenca as $diferencas) { 
	$stringArrayF = $stringArrayF.$diferencas; 
?> 

<option value="$diferencas"><?php echo $stringArrayF; ?></option> 

</body>


<?php } ?> 
</select>

  • Fixed kkk again , now go

  • It got better! But it stayed that way <select>&#xA;<option value="192.168.0.100">192.168.0.100</option> &#xA;<option value="192.168.0.100192.168.0.104">192.168.0.100192.168.0.104</option> &#xA;<option value="192.168.0.100192.168.0.104192.168.0.105">192.168.0.100192.168.0.104192.168.0.105</option> &#xA;</select> The problem is separating the results of _ array_ by position.

  • declare that i = 0 , in which case we could in value exchange for an i++, type option value = $difca for option value = $i; i++ understood ?

  • I believe it has more to do with printing each position separately, I think the iteration, with a for wouldn’t do that in the <select> It would have to be somewhere else :/. I just don’t know where

0

Try to exchange your foreach for this code:

echo "<select>";
foreach ($diferenca as $value)
{
    echo '<option value="'.$value.'">'.$value.'</option>';
}       
echo "</select>";
  • Very good @Guilherme Corrêa Peralta! I had managed to make the list, but without the value . <select>&#xA;<?php&#xA;$sql = mysqli_query($cx, "SELECT IPS FROM ipvalidos WHERE RORDER BY IPS ASC");&#xA; &#xA; while($aux = mysqli_fetch_assoc($sql)) {&#xA; $arrayip[ ] = $aux["IPS"];&#xA; }&#xA;&#xA;$diferenca = array_diff($arrayip, $arraycompara);&#xA;&#xA; &#xA; $ipdisponiveis = implode("</option>\n<option>" , $diferenca);&#xA; &#xA;?>&#xA;&#xA;<option><?php echo $ipdisponiveis;?></option></select> His solution was Genial!

  • Glad it worked out. Please mark my answer as accepted by clicking on the check on the left side of the answer. See how and why on this link: https://answall.com/help/someone-answers

-1

Make it fixed

$sql = mysqli_query($cx, "SELECT IPS FROM ipvalidos ORDER BY IPS ASC ");

while($aux = mysqli_fetch_assoc($sql)) {
    $arrayip[ ] = $aux["IPS"];
        }

$diferenca = array_diff($arrayip, $arraycompara);


?>

<?php


foreach ($diferenca as $diferencas)
{

$stringArrayF = $stringArrayF.$diferencas;
?>

<select><option value="<?php $stringArrayF ?>">$stringArrayF</option>;


<?php        
}    

?>   

  • It’s coming up like this <html>&#xA;<form>&#xA;&#xA;<select><option value="192.168.0.100">192.168.0.100</option>&#xA;<select><option value="192.168.0.100192.168.0.104">192.168.0.100192.168.0.104</option>&#xA;<select><option value="192.168.0.100192.168.0.104192.168.0.105">192.168.0.100192.168.0.104192.168.0.105</option>&#xA;<select><option value="192.168.0.100192.168.0.104192.168.0.105192.168.0.106">192.168.0.100192.168.0.104192.168.0.105192.168.0.106</option>&#xA;&#xA;</form>

  • send me the code you updated, so I can take a look.

  • >$sql = mysqli_query($cx, "SELECT IPS FROM ipvalidos ");&#xA; &#xA; while($aux = mysqli_fetch_assoc($sql)) {&#xA; $arrayip[ ] = $aux["IPS"];&#xA; }&#xA; &#xA;$diferenca = array_diff($arrayip, $arraycompara);&#xA;&#xA;?>&#xA;<?php &#xA;foreach ($diferenca as $diferencas)&#xA; {&#xA; $stringArrayF = $stringArrayF.$diferencas;&#xA;?>&#xA;&#xA;&#xA;<select><option value="<?php $stringArrayF ?>"><?php $stringArrayF ?></option>';&#xA;&#xA;<?php &#xA;} &#xA;?> &#xA;&#xA;</select>

  • Fixed, just see the quotes ai, $sql = mysqli_query($Cx, "SELECT IPS FROM ipvalidos "); &#Xa while($aux = mysqli_fetch_assoc($sql)) { $arrayip[ ] = $aux["IPS"]; } $difca = array_diff($arrayip, $arraycompara); ? > <? php foreach ($differs from $differentials) { $stringArrayF = $stringArrayF. $differentials; ? > <select><option value="<? php echo $stringArrayF; ? >"></option></select> <? php } ?>

  • Unfortunately, it hasn’t changed much: <select><option value="192.168.0.100">192.168.0.100</option></select> &#xA;&#xA;&#xA;&#xA;<select><option value="192.168.0.100192.168.0.104">192.168.0.100192.168.0.104</option></select> &#xA;&#xA;&#xA;&#xA;<select><option value="192.168.0.100192.168.0.104192.168.0.105">192.168.0.100192.168.0.104192.168.0.105</option></select> &#xA;

  • now go, tested here,

  • What I think the problem is in separating each position of the array into one <option> . I put the <select > at the beginning of everything and appeared only a combobox, but the result is not as it should be.

  • see la, fixed 2 kkk, pq actually, select has to this before for each, pq actually it was, multiplying the select

  • That we had already corrected. The foreach is doing an arithmetic progression look at Position 1; Position 1 + Position 2, Position 1 + Position 2 + Position 3 and so on! <select>&#xA;<option value="192.168.0.100">192.168.0.100</option>&#xA;<option value="192.168.0.100192.168.0.104">192.168.0.100192.168.0.104</option>&#xA;<option value="192.168.0.100192.168.0.104192.168.0.105">192.168.0.100192.168.0.104192.168.0.105</option>&#xA;</select>

  • You know @Victor, I don’t know if I made it clear, but there are 3 arrays . $diff = diff array_f($arrayip, $arraycompara). The $difference I turn into string with the foreach.

  • got that result <select>&#xA;<option value="192.168.0.10&#xA;192.168.0.100 &#xA;192.168.0.101 &#xA;192.168.0.102 &#xA;192.168.0.103 &#xA;192.168.0.104 &#xA;192.168.0.106 &#xA;192.168.0.107 &#xA;192.168.0.108 ..</option>&#xA;</select>&#xA;</form> with that code: while($aux = mysqli_fetch_assoc($sql)) {&#xA; $arrayip[ ] = $aux["IPS"];&#xA; }&#xA; &#xA;&#xA;&#xA;&#xA;$diferenca = array_diff($arrayip, $arraycompara);&#xA;&#xA;&#xA; $ipdisponiveis = implode("\n", $diferenca);&#xA; &#xA;&#xA;&#xA;&#xA;&#xA;?>&#xA;<select>&#xA;&#xA;&#xA;<option value="<?php echo $ipdisponiveis; ?>"><?php echo $ipdisponiveis; ?></option>&#xA;&#xA;&#xA;</select>

  • needs help yet ?

  • Thank you for your attention Vitor, but the reply of @Guilherme Corrêa Peralta, solved my problem! Very grateful for your help, I learned a lot.

Show 8 more comments

Browser other questions tagged

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