Php : récupérer nombre de commentaires

Steph94 -  
 Steph94 -
Bonjour,

Je cherche à récupérer le nombre de commentaires dans un script de news, mais j'ai une erreur sur une variable :

Notice: Undefined variable: nb in /homez.504/.../index.php on line 129

Voilà le code php concerné :

if(isset($_GET["id"]))
{

$sql2 = "SELECT id FROM commentaires WHERE idnews=".intval(mysql_real_escape_string($_GET["id"]))."";
$req = mysql_query($sql2) or die('Erreur SQL !'.$sql2.'<br>'.mysql_error());
$nb = mysql_num_rows($req);

// On converti la date au format EU
$datefr="$date";
list($a,$m,$j)=explode("-",$datefr);
$date="$j-$m-$a";
}

Et ligne 129 :

<?php echo ''.$nb.'';?>

4 réponses

  1. Nanyick
     
    Bonjour,

    Tes lignes :

    if(isset($_GET["id"]))
    {
    $sql2 = "SELECT id FROM commentaires WHERE idnews=".intval(mysql_real_escape_string($_GET["id"]))."";
    $req = mysql_query($sql2) or die('Erreur SQL !'.$sql2.'<br>'.mysql_error());
    $nb = mysql_num_rows($req);
    // On converti la date au format EU
    $datefr="$date";
    list($a,$m,$j)=explode("-",$datefr);
    $date="$j-$m-$a";
    }

    ne seraient-elles pas dans une fonction alors que ta ligne 129 ne le serait pas ?

    D'où le undefined variable du à la portée des variables ?
    0
  2. Steph94
     
    Salut,

    Merci de te pencher sur ma question.

    Je te donne le code jusqu'à la ligne 129, ce sera plus clair :

    if(isset($_GET["id"]))
    {

    $sql2 = "SELECT id FROM 1980_comments WHERE idNews=".intval(mysql_real_escape_string($_GET["id"]))."";
    $req = mysql_query($sql2) or die('Erreur SQL !'.$sql2.'<br>'.mysql_error());
    $nb = mysql_num_rows($req);

    // On converti la date au format EU
    $datefr="$date";
    list($a,$m,$j)=explode("-",$datefr);
    $date="$j-$m-$a";
    }

    echo' </span></span><br>
    <table width="408" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="60"><img src="img/menu.gif" width="60" height="20"></td>
    <td width="144" bgcolor="#E7CA88"><span class="Style13">';
    echo ''.$date.'';
    ?>
    </span></td>
    <td width="60" bgcolor="#E7CA88"><img src="img/menu2.gif" width="60" height="20"></td>
    <td width="144" bgcolor="#E7CA88"><span class="Style13">
    <?php echo ''.$titre.'';
    ?>
    </span></td>
    </tr>
    <tr align="left">
    <td colspan="4">
    <div align="left"> <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td><?php echo ''.$texte.'';?></td>
    </tr>
    <tr>
    <td><div align="right" class="Style16"><strong>Auteur</strong> : <? echo ''.$auteur.'';?> </div></td>
    </tr>
    </table>
    </div></td>
    </tr>
    <tr>
    <td colspan="2" valign="top" bgcolor="#ECE2BE"><div align="left"><img src="img/bas2.gif" width="204" height="10"></div></td>
    <td colspan="2" valign="top"><div align="left"><img src="img/bas3.gif" width="204" height="10"></div></td>
    </tr>
    <tr>
    <td height="18" colspan="2" align="left" valign="top" bgcolor="#ECE2BE"><div align="left"><?php

    echo '<A HREF="#" onClick="window.open(\'ajoutcom.php?id='. $id .'/\',\'_blank\',\'toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=1, copyhistory=0, menuBar=0, width=500, height=600, left=0, top=0\');return(false)">Ajouter commentaire</a>';?></div></td>
    <td colspan="2" align="left" valign="top"><div align="left"><? echo '<A HREF="#" onClick="window.open(\'lirecom.php?id='. $id .'/\',\'_blank\',\'toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=1, copyhistory=0, menuBar=0, width=500, height=600, left=0, top=0\');return(false)">Lire les commentaires</a>';?>
    <?php echo ''.$nb.'';
    var_dump ('$nb');
    ?></div>
    0
  3. Nanyick
     
    En fait, ce n'est qu'un avertissement (NOTICE).

    Comme tu calcules $nb dans un if, il est possible que ce dernier ne soit initialisé lors de son affichage (ligne 129).

    test pour confirmation :
    Ajoute sur la ligne avant le if (par exemple) : $nb ="";

    test ta page.
    Le NOTICE devrait disparaitre.
    0
  4. Steph94
     
    En effet, le NOTICE a disparu. Si j'enlève le if, la variable nb est valide, mais pas la variable id. Savez-vous ce que je dois faire pour ne plus avoir de message d'erreur ?
    0