Fix undefined offset notice

I need to solve this undefined offset notice. It seems to be caused by the last line in the reported code


// First get ads per cat and subcat
$subcatadcounts = array();
$catadcounts = array();
$sql = "SELECT scat.subcatid, scat.catid, COUNT(*) as adcnt
		FROM $t_ads a
			INNER JOIN $t_subcats scat ON scat.subcatid = a.subcatid AND ($visibility_condn)
			INNER JOIN $t_cats cat ON cat.catid = scat.catid
			INNER JOIN $t_cities ct ON a.cityid = ct.cityid
		WHERE scat.enabled = '1'
			$loc_condn
		GROUP BY a.subcatid";
 
$res = mysql_query($sql) or die(mysql_error().$sql);
 
while($row=mysql_fetch_array($res))
{
	$subcatadcounts[$row['subcatid']] = $row['adcnt'];
	$catadcounts[isset($row['catid'])] += $row['adcnt'];
}

Open in new window

jimgordonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
hieloConnect With a Mentor Commented:
OK. Well that clear things more, although it does not help at all. At any rate, see if the code below helps. Thats my best guess.

while($row=mysql_fetch_assoc($res))
{
	$subcatadcounts[$row['subcatid']] = $row['adcnt'];
	if(isset($row['catid'] ) )
	{
		if(!isset($catadcounts[ 1 ]) )
		{
			$catadcounts[ 1 ]=0;
		}
		$catadcounts[ 1 ] += $row['adcnt'];
	}
	else
	{
		if(!isset($catadcounts[ 0 ]) )
		{
			$catadcounts[ 0 ]=0;
		}
		$catadcounts[ 0 ] += $row['adcnt'];
	}
}

Open in new window

0
 
hieloCommented:
>>$catadcounts[isset($row['catid'])] += $row['adcnt'];
Are you trying to add or concatenate?


0
 
hieloCommented:
Perhaps you meant:
while($row=mysql_fetch_array($res))
{
	$subcatadcounts[$row['subcatid']] = $row['adcnt'];
	if(!isset($catadcounts[ $row['catid'] ]) )
	{
		$catadcounts[ $row['catid'] ]=0;
	}
	
	$catadcounts[ $row['catid'] ] += $row['adcnt'];
}

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jimgordonAuthor Commented:
What I am trying to do is displaying the categories in a classified ads page (the home page), which you can see at: www.annunci.cc
0
 
hieloCommented:
I meant "what are trying to do in technical terms?", since this
>>$catadcounts[isset($row['catid'])] += $row['adcnt'];
looks fishy! Refer to my last post.
BTW: after re-examining my last post, you should consider changing:
while($row=mysql_fetch_array($res))

to:
while($row=mysql_fetch_assoc($res))
since you are using only associative arrays.
0
 
jimgordonAuthor Commented:
I've tried, but nothing has changed. BTW, after this error, I get a long list of undefined index for "3", "4"... "80" and so on (I guess they are number for sub categories. Should I post you more code?
0
 
hieloCommented:
You need to tell me what it is that you are trying to do "program-wise". Are you trying to add quantities?
So far I have just been guessing because your line of code can be interpreted in different  ways. So posting more code may not help if you cannot tell me the "algorithm" you are trying to implement.
Here is another possibility:
while($row=mysql_fetch_assoc($res))
{
      $subcatadcounts[$row['subcatid']] = $row['adcnt'];
      $catadcounts[$row['catid']] = $row['adcnt'];
}
0
 
jimgordonAuthor Commented:
I attach a screenshot of my errors
0
 
jimgordonAuthor Commented:
Here is the notices
notices.gif
0
 
jimgordonAuthor Commented:
I cannot answer technically. I purchased this classified script and I started to remove any xhtml and php error in it. I ask you because I don't know. I can give you any other detail about the script, but I don't know what developer would implement. I can tell you what the script does, and you can see it live at the web address above.
0
 
jimgordonAuthor Commented:
Well, it seems that undefined offset has disappeared! But I still have a long list of undefined index for "3", "4"... and so on as you can see in my posted image above. Can you help?
0
 
jimgordonAuthor Commented:
Here is the code for the undefined index error message:
The problem is in this line: $adcount = 0+$subcatadcounts[$rowsubcat['subcatid']];
<table border="0" cellspacing="0" cellpadding="0" width="90%" class="dir_cat">
	<tr>
	<th width="25" valign="top"><img src="images/category.gif" border="0" style="vertical-align:middle" alt="Annunci" /></th>
	<th><a href="<?php echo $catlink; ?>"><?php echo $rowcat['catname']; ?></a>
	<?php if($show_cat_adcount) { ?><span class="count">(<?php echo $adcount; ?>)</span><?php } ?>
	</th>
	</tr>
 
<?php
 
	$sql = "SELECT scat.subcatid, scat.subcatname AS subcatname
	FROM $t_subcats scat
	WHERE scat.catid = $rowcat[catid]
		AND scat.enabled = '1'
	$sortsubcatsql";
 
	$ressubcats = mysql_query($sql) or die(mysql_error()."<br />$sql");
 
	while ($rowsubcat = mysql_fetch_array($ressubcats))
	{
		$adcount = 0+$subcatadcounts[$rowsubcat['subcatid']];
 
		if($sef_urls) $subcat_url = "{$vbasedir}$xcityid/annunci/$rowcat[catid]_{$catname_inurl}/$rowsubcat[subcatid]_". RemoveBadURLChars($rowsubcat['subcatname'])."/";
		else $subcat_url = "?view=ads&catid=$rowcat[catid]&subcatid=$rowsubcat[subcatid]&cityid=$xcityid&lang=$xlang";
 
?>
		<tr>
		<td>&nbsp;</td>
		<td>
		<a href="<?php echo $subcat_url; ?>"><?php echo $rowsubcat['subcatname']; ?></a>
		<?php if($show_subcat_adcount) { ?><span class="count">(<?php echo $adcount; ?>)</span><?php } ?>
		<br />
		</td>
		</tr>
 
<?php
 
	}
 
?>
	
	</table>

Open in new window

0
 
jimgordonAuthor Commented:
I solve changing: $adcount = 0+$subcatadcounts[$rowsubcat['subcatid']];
with: $adcount = 0+isset($subcatadcounts[$rowsubcat['subcatid']]);

I am satisfied with your help. This is my first time i ask for help. What should I do now? Click on accept and award points?
0
 
hieloConnect With a Mentor Commented:
replace this:
$adcount = 0+$subcatadcounts[$rowsubcat['subcatid']];

with this:
if( isset($subcatadcounts[ $rowsubcat['subcatid'] ]) )
      $adcount = (int)$subcatadcounts[ $rowsubcat['subcatid'] ];
else
      $adcount=0;
0
 
jimgordonAuthor Commented:
Thanks. What I have to do now to accept solutions and award points?
0
 
jimgordonAuthor Commented:
Is it possible to remove the link to the website and the attached pic?
0
 
hieloCommented:
I don't know. I have never asked a question. You will need to look around. (And please, take the time to look around and close it because as long as it is not closed, it distracts me from helping others).
0
 
hieloCommented:
You would need to contact community support or any of the the zone advisors.
0
 
jimgordonAuthor Commented:
Thank a lot hielo. You really helped me.
0
All Courses

From novice to tech pro — start learning today.