0

this is my first question here. I'm facing a problem that is literally driving me crazy. I'm new to PHP and I feel that it is something really simple that I just can't figure out.

I have this code:

$busca_alimento = mysql_query("SELECT * FROM alimentos");
if (empty($busca_alimento)) { 
    echo 'Nothing found';
}

$count = 0;

while ($alimento_informacoes = mysql_fetch_array($busca_alimento)) {

    $alimento_id[$count++] = $alimento_informacoes['id'];
    $alimento_categoria[$count++] = $alimento_informacoes['categoria'];
    $alimento_nome[$count++] = $alimento_informacoes['nome'];
    $alimento_quantidade[$count++] = $alimento_informacoes['quantidade'];

}

I want to fetch the ID, Categoria, Nome and Quantidade rows and store the data in those vars like this:

$alimento_id[1]
$alimento_categoria[1]
$alimento_nome[1]
$alimento_quantidade[1]

but what is happening is this:

$alimento_id[1]
$alimento_categoria[2]
$alimento_nome[3]
$alimento_quantidade[4]

Does anyone know what's happening and how can I solve this? Thank you

5 Answers 5

1

I like this better, and no increment needed:

while ($alimento_informacoes = mysql_fetch_array($busca_alimento)) {
  $alimento_id[]         = $alimento_informacoes['id'];
  $alimento_categoria[]  = $alimento_informacoes['categoria'];
  $alimento_nome[]       = $alimento_informacoes['nome'];
  $alimento_quantidade[] = $alimento_informacoes['quantidade'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Increment $count one time only, then use it as a subscript.

while ($alimento_informacoes = mysql_fetch_array($busca_alimento)) {
    $count++;
    $alimento_id[$count] = $alimento_informacoes['id'];
    $alimento_categoria[$count] = $alimento_informacoes['categoria'];
    $alimento_nome[$count] = $alimento_informacoes['nome'];
    $alimento_quantidade[$count] = $alimento_informacoes['quantidade'];

}

Depending on the initial value of $count and what value you want to use, you may need to move $count++ to the end of the loop.

1 Comment

I moved the $count to the end of the loop and worked. Thanks!
0

Well, you're incrementing it everytime. Increment it just once:

while ($alimento_informacoes = mysql_fetch_array($busca_alimento)) {
    // so first loop, its just zero, 0
    $alimento_id[$count] = $alimento_informacoes['id'];
    $alimento_categoria[$count] = $alimento_informacoes['categoria'];
    $alimento_nome[$count] = $alimento_informacoes['nome'];
    $alimento_quantidade[$count] = $alimento_informacoes['quantidade'];
    // so before this line, its all zero
    $count++; // then increment
    // increment it after cycle is complete
}

Comments

0
$busca_alimento = mysql_query("SELECT * FROM alimentos");
if (empty($busca_alimento)) { 
  echo 'Nothing found';
}

$count = 0;

while ($alimento_informacoes = mysql_fetch_array($busca_alimento)) {

$alimento_id[$count] = $alimento_informacoes['id'];
$alimento_categoria[$count] = $alimento_informacoes['categoria'];
$alimento_nome[$count] = $alimento_informacoes['nome'];
$alimento_quantidade[$count] = $alimento_informacoes['quantidade'];

$count++;
}

Comments

0

increment $count only one time in the loop:

$alimento_id[++$count] = $alimento_informacoes['id'];
$alimento_categoria[$count] = $alimento_informacoes['categoria'];
$alimento_nome[$count] = $alimento_informacoes['nome'];
$alimento_quantidade[$count] = $alimento_informacoes['quantidade'];

1 Comment

In this way, the other arrays will have different $count value than first, maybe you mean ++$count

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.