1

I would like to get the id of a specific value in my phpmyadmin table. So, I have got a table with 'id_cal' as an A.I. id, 'mois' representing the month with numbers (e.g 1 for january) and 'annee' representing the year. (see calendar table)

I am trying to set php variable for the month and the year and if they match the current month and year, I want to get this specific id.

I commented the php code where I am having my trouble, here it is :

<?php

include_once('config.php');

$m = idate('n');
$y = idate('Y');

echo $m; echo "\t"; echo $y; echo "<br>"; echo "<br>";  // The result of this is 7 2019

$reponse = $bdd->query('SELECT * FROM calendrier');

while($donnees= $reponse->fetch()){ 
    $mois = $donnees['mois'];
    $year = $donnees['annee'];
    $id_cal = $donnees['id_cal'];
    echo $id_cal;
    echo "\t";
    echo $mois;
    echo "\t";
    echo $year;
    echo "<br>";
}

// What I am trying to do :

if (($m = $mois) && ($y = $year)){  // If the month and the year are the current month/year
    $i = $id_cal;                   // I want to put the id refering to the current month/year (in my phpmyadmin table) into a new variable
    echo "<br>";                    // and echo this variable (or use it in other ways)
    echo $i;                        // BUT what I am echoing is 24 representing the number of values in my array
}                                   // How can I only get 7 ? (in this exemple, since we are the 7/2019)

Here is what I am getting in my localhost : echo

I really don't understand why am I not having 7.

Also, I tried this instead of my while:

$donnees= $reponse->fetch();    
$mois = $donnees['mois'];
$year = $donnees['annee'];
$id_cal = $donnees['id_cal'];

// But in this cas I am having $i = 1, so it's the same problem.

Many thanks in advance for your response I am quite struggling with this.

3
  • Did you maybe want to put the if inside the loop? The last time thru the loop $id_cal will equal the last one, which from your image is 24. Commented Jul 29, 2019 at 15:45
  • You also probably want $m == $mois instead of $m = $mois. Commented Jul 29, 2019 at 16:07
  • The == was indeed my main problem here and also putting the if statement in the loop helped me. Thank you very much to both of you ! Commented Jul 30, 2019 at 10:17

1 Answer 1

1

It is because id_cal gets overwritten with the new value of id_cal in each iteration of your while statement.

To get the result you want, you could put the if inside the while statement...

while($donnees= $reponse->fetch()){ 
    $mois = $donnees['mois'];
    $year = $donnees['annee'];
    $id_cal = $donnees['id_cal'];
    echo $id_cal;
    echo "\t";
    echo $mois;
    echo "\t";
    echo $year;
    echo "<br>";
    if (($m == $mois) && ($y == $year)){  
        $my_var_to_use_elsewhere = $id_cal;                 
    }   
}

echo "<br>";                   
echo $my_var_to_use_elsewhere;
Sign up to request clarification or add additional context in comments.

2 Comments

This (with the add of Paul Spiegel solution, i.e the "==") worked perfectly, many thanks !
Ah yes of course! You may also want to consider break; inside the if statement if you're not concerned with the values after your if statement is met. php.net/manual/en/control-structures.break.php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.