number loop, removing some that has already been used

Asked

Viewed 50 times

0

Good morning, I’m having trouble solving a code here I have the following table in the database

inserir a descrição da imagem aqui

The field "total" is the total number that the raffle has

The field "paid" are the numbers that have already been sold.

I want to show the numbers in up to 150 that is the amount of numbers that the raffle has, but I want the numbers 5,8,9 to appear in red!! that the user will not be able to click... Thinking this I created the following code!

for($i = 1; $i < 150 $i++)
{
  if( in_array($i, array(1,3,4,5,6,7,8,9) ) ){
    continue;
  }
  echo "<li><a class='desativado' href='#'><i>$i</i></a></li>";
}

The above Code works, it does not appear the numbers 1,3,4,5,6,7 etc. more when I put the value of db so:

if( in_array($i, array($dados['total']) ) ){

It does not show the other numbers, it only takes out the number '1' the rest keeps appearing. and miss they 'no' appear too!!

When I take the value of the database he knows not what happens simply he keeps showing the other numbers

I wanted it to stay that way:

Somebody can help me, I’ve been sitting on this since yesterday!

inserir a descrição da imagem aqui

  • What you are doing is testing whether the number is in [150] and not in [1, 2, ... 150]. The best logic is to test if the number has already been drawn and use "else"

  • hi Castro good morning, I did not understand very well not! can explain me better?

  • Bernardo did just what I suggested.

2 answers

0


This is the result that comes from your database:

$dados['total'] = 150
$dados['num_pagos'] = '5,8,9'

You have to change $dados['total'] for $dados['num_pagos'] and can put a else also:

$num_pagos = explode(',', $dados['num_pago']);
for($i = 1; $i <= $dados['total']; $i++)
{
  if (in_array($i, $nums_pagos) ){
    echo "<li><a class='desativado' href='#'><i>$i</i></a></li>";
  } else {
    echo "<li><a class='ativado' href='#'><i>$i</i></a></li>";
  }
}
  • 1

    It works only with result placed in the hand, when you take the db it only works with the first number!

  • It’s really I did a test and it only works with the first number with results coming from DB.. why will it be? I’m looking for the same thing!

  • 1

    that part -> if (in_array($i, array($data['num_pagos']) ) )' if you take it and put it in your hand it works, if you put it from DB it only takes the first number!! help me!

  • It does not work because $data['num_pagos'] is string, not array. I updated the answer by turning $data['num_pagos'] into array and then checking.

0

I managed to solve @Wallace Maxters Thank you!!!

I saw that you already asked a similar question here on the site. Case a code similar, you would have to change the line of your in_array to in_array($i, explode(',', $given['num_pago']))

inserir a descrição da imagem aqui

Browser other questions tagged

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