I am making a photo gallery and with every photo I can see how many comments there are on that photo. The code that I have is working like it should except that if there are 0 comments it will give the 'Undefined Offset error: 2'
'Undefined Offset error: 3'
'Undefined Offset error: 4'
'Undefined Offset error: 5'
etc.
This is the code to check how many comments there are: (reacties = comments)
// check how many comments every photo has.
$reacties;
$query = "select foto from fotosreacties where boek = '" . $_GET['boek'] . "'";
if($result = mysql_query($query)){
while ($r = mysql_fetch_array($result)){
while ($foto = current($fotoArray)) {
if ($foto==$r["foto"]){
$key = key($fotoArray);
} // end if
next($fotoArray);
} // end while
if(!isset($reacties[$key])){
$reacties[$key] = 1;
} // end if
else {
$reacties[$key]++;
} // end else
reset($fotoArray);
} // end while
} // end if
And this is the code to show the picture and the number of comments:
for($i=$begin; $i < $eind; $i++){
$thumb = str_replace($path2, $thumbPath, $fotoArray[$i]);
echo "<td align='center'><a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&fotoID=" . $i . "'><img border='0' src='" . $thumb . "' height='100'><br>";
echo "<small>reacties (";
if($reacties[$i]==0){
echo "0";
} // end if
else {
echo $reacties[$i];
} // end else
echo ")</small>";
echo "</a></td>";
$fotonr++;
if($fotonr == ($clm + 1)){
echo "</tr>
<tr>";
$fotonr = 1;
} // end if
If anyone knows what I have done wrong and/or knows how to solve this it would be great!
You can embed the counter in your query:
select foto, count(*) AS counter_comments from fotosreacties where boek = '" . $_GET['boek'] . "'
. In the result, you can use $r['counter_comments']
and check whether its 0 or more.
About your code:
MySQL
-functions in new applications since they will be deprecated soon. Use MySQLi or PDO instead!$_GET
, you at least sanitize it by (for example) mysql_real_escape_string()
.