Check if a value is present in an array

Asked

Viewed 606 times

2

I have a table in my database with several numbers. I take the numbers from the database, store them in a array, and do echo in a PHP table:

echo "<table class='CSSTableGenerator'>";

echo "<tr>";

echo "<th>array</th>";

echo "<th>1</th>";
echo "<th>2</th>";
echo "<th>3</th>";
echo "<th>4</th>";
echo "<th>5</th>";
echo "<th>6</th>";
echo "<th>7</th>";
echo "<th>8</th>";
echo "<th>9</th>";
echo "<th>10</th>";
echo "<th>11</th>";
echo "<th>12</th>";
echo "<th>13</th>";
echo "<th>14</th>";
echo "<th>15</th>";
echo "<th>16</th>";
echo "<th>17</th>";
echo "<th>18</th>";
echo "<th>19</th>";
echo "<th>20</th>";
echo "<th>21</th>";
echo "<th>22</th>";
echo "<th>23</th>";
echo "<th>24</th>";
echo "<th>25</th>";
echo "<th>26</th>";
echo "<th>27</th>";
echo "<th>28</th>";
echo "<th>29</th>";
echo "<th>30</th>";

echo "</tr>";

while ($produto = mysql_fetch_array($produtos)) {
    $vetor=array($produto['id'],$produto['n1'],$produto['n2'],$produto['n3'],$produto['n4'],$produto['n5'],$produto['e1'],$produto['e2']);

    echo "<tr>";

    echo "<td>".$vetor[1]."&nbsp";
    echo $vetor[2]."&nbsp";
    echo $vetor[3]."&nbsp";
    echo $vetor[4]."&nbsp";
    echo $vetor[5]."&nbsp";
    echo $vetor[6]."&nbsp";
    echo $vetor[7] . "</td>&nbsp";


    for ($i = 1; $i <= 30; $i++) {
        echo '<td class="'.(($vetor[1]==$i) ? 'paint_me_green' : 'paint_me_red').'"></td>';

    }

    echo "</tr>";

}

echo "</table>";

?>

The numbers (array) are written in the column <th>array<th> and the others <th>1 until 50 <th> I’d like to check if the numbers contained in one of my arrays ex: array 10 12 23 10 223. If number 10 exists in this array to <th>10</th> will be green in the line of this array, otherwise red.

So far as you can see in my code, inside the while and put a for

for ($i = 1; $i <= 50; $i++) {
    echo '<td class="'.(($vetor[1]==$i) ? 'paint_me_green' : 'paint_me_red').'"></td>';
}

but this way he’s just checking whether:

  • heading [1] == 1 he paints green if he does not paint red.
  • heading [1] == 2 he paints green if he does not paint red.
  • ...
  • heading [1] == 23 he paints green if he does not paint red.

And it works this way... that’s what I want. I just want to do the same for the other positions beyond the position [1].

The for which I am using, should not be the most correct way of analysing the remaining positions.

It would be something like this I would need:

for ($i = 1; $i <= 50; $i++) {
    echo '<td class="'.(($vetor[1]==$i) and ($vetor[2]==$i) and ($vetor[3]==$i) and ($vetor[4]==$i) and and ($vetor[5]==$i) and ($vetor[7]==$i) ? 'paint_me_green' : 'paint_me_red').'"></td>';
}

They told me about the foreach ($array_de_arrays ... I just can’t apply.

1 answer

4


  • It is working as I wanted according to your solution. But in this way, it increases an extra green field in the line, up to 50 arrays displayed in the table. then works normal, validates only the 7 elements of the array, without incrementing anything else in the line.

  • You need to explain better what you want, what result you want. Your texts are confusing and you can’t understand what you want.

  • 1

    You’re right, my explanation is a little confusing. Can you take a look at the image? It adds an extra green for 50 arrays in the table. Then run normal http://imageshack.com/a/img537/1525/dPdnVJ.png

  • 1

    The problem is elsewhere, this code is doing the right thing, maybe you don’t want to put the product ID in the vector as you are doing more above. Take the test, take it to see. Then you decide whether you want to keep it or not. Apparently it has no use. It is not only the text that is confusing, the code too. It is very difficult to analyze it.

  • I put 0 in the unnecessary id position of the vector coming from the database. Perfect! @bigown thanks!

Browser other questions tagged

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