Undefined variable quand pas de isset
Bonjour,
Voici le script qui me retourne une erreur sur EasyPHP:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
<?php
//query the database
$myResult = mysql_query('SELECT ref_links.*, ref_categories.category_name FROM ref_links, ref_categories WHERE ref_links.ref_categories_id=ref_categories.id', $connectID)
or die ("Unable to select from database");
//loop through the returned records
print '<table border="1">'."\n";
while ($row = mysql_fetch_array($myResult, MYSQL_ASSOC)) {
//category heading only written out if a listing is in diff cat from previous listing
//$thisCat is used to test if a new heading needs to be written out
$thisCat= $row['category_name'];
if ($lastCat<>$thisCat) { // true first time($lastCat not set), and each time a new category is found
//then write out a table heading with the category name
print '<th colspan="6" align="left">'; //heading spans all columns of table
print "<h3>".$row['category_name']."</h3>"; // print the next category heading
print "</th>\n";
}
print '<tr>';
print '<tr>'."\n";
print '<td>'.$row['topic_name'].'</td>'."\n";
print '<td class="description">'.$row['topic_desc'].'</td>'."\n";
print '<td><a href="'.$row['topic_url'].'" target="_blank">'.$row['topic_url'];
print'</a></td>';
print '</tr>'."\n";
// finally, we store the current category so next time the loop repeats,
// we can tell if the next item is in a new category or not
// if so, we write out a new heading - see if ($lastCat<>$thisCat) above
$lastCat = $row['category_name'];
}
print '</table>'."\n";
?> |
La deuxième partie du code semble fonctionner, mais je reçois l'erreur suivante en haut:
« Notice: Undefined variable: lastCat in C:\Program Files\EasyPHP 2.0b1\www....php on line 23 »
Je crois que le problème vient que je n'ai pas utilisé isset, mais je ne sais pas comment l'utiliser dans ce cas précis...
Au premier passage de
Code:
if ($lastCat<>$thisCat)
ça devrait donner true, mais ça fait juste une erreur parce que $lastCat n'a pas encore été initialisé.
Quelqu'un peut m'aider? Merci!
Il faut initialiser la variable
Bonjour,
c'est normal que ça te donne une erreur puisque la variable n'existe pas. Essai de l'initialiser avec une valeur.
Code:
1 2 3 4 5 6 7 8
|
$lastCat='';
while ($row = mysql_fetch_array($myResult, MYSQL_ASSOC)) {
//category heading only written out if a listing is in diff cat from previous listing
//$thisCat is used to test if a new heading needs to be written out
$thisCat= $row['category_name'];
if ($lastCat<>$thisCat) {
... |