EternalSorrow Posted September 26, 2011 Share Posted September 26, 2011 For some reason I keep getting a recurring error every time someone uses a page on my site. Here is the error: [Date] PHP Warning: array_key_exists() [<a href='function.array-key-exists'>function.array-key-exists</a>]: The second argument should be either an array or an object in /home/page.php on line 52 The offending line is here: echo array_key_exists($i, $alpha) ? '<li><a href="?a=show&letter='.$i.'&type='.$type.'">'.$i.'</a></li> ' : '<li class="no_highlight">'.$i.'</li> The code works, but the error message must be showing that I am performing the echo incorrectly. Anyone have any ideas on where I could be going wrong (other than the exact line, that is )? Here's the rest of the code for full reference (the offending line is near the top). Thanks for any help! <?php $sql = "SELECT *, UPPER(SUBSTRING(series,1,1)) AS letter FROM design WHERE type = '$type' ORDER BY series"; $query = mysql_query($sql) or die(mysql_error()); // Display a range of letters. echo '<ul class="alphabet">'; while ($records = mysql_fetch_assoc($query)) { $alpha[$records['letter']] += 1; ${$records['letter']}[$records['series']] = $records['series']; } foreach(range('A','Z') AS $i) { echo array_key_exists($i, $alpha) ? '<li><a href="?a=show&letter='.$i.'&type='.$type.'">'.$i.'</a></li> ' : '<li class="no_highlight">'.$i.'</li> '; echo $i != 'Z' ? ' ':'</ul>'; } // Display records of the letter selected. if (isset($_GET['letter']) && !empty($_GET['letter']) && !is_numeric($_GET['letter'])) { $letter = substr($_GET['letter'], 0, 1); } else { $letter = ''; } $a = (isset($_GET['a'])) ? strip_tags(trim(htmlentities($_GET['a'], ENT_QUOTES))) : 'index'; switch ($a) { case 'index': $select_series = mysql_query("SELECT *, UPPER(SUBSTRING(series,1,1)) AS letter FROM design WHERE `type` = '$type' ORDER BY `id` DESC LIMIT 8") or die (mysql_error()); $i=3; echo '<h3>Newest Additions</h3>'; while($row2 = mysql_fetch_array($select_series)) { extract ($row2); $contents_here = '<div class="envelope"><a href="?a=show&letter='.$letter.'&type='.$type.'#'.$title.'"><img src="previews/'.$id.'.jpg" class="tips" title="'.$title.'|'.$series.'"><br>'.$title.'</a></div>'; if ($i==0) { echo ''.$contents_here.''; } else if ($i==3) { echo ''.$contents_here.''; } else { echo ''.$contents_here.''; } $i++; $i=$i%3; } break; case 'show': $letter2 = (isset($_GET['letter'])) ? strip_tags(trim(htmlentities($_GET['letter'], ENT_QUOTES))) : ''; $sql = "SELECT * ,UPPER(SUBSTRING(series,1,1)) AS letter, date_format(design.datetime, '%M %d, %Y') AS datetime FROM design WHERE series LIKE '$letter2%' AND type = '$type' ORDER BY series ASC, id ASC"; $query = mysql_query($sql) or die (mysql_error()); echo '<h3><a name="'.$i.'"><b>'.$letter.'</b></a></h3> <table align="center" width="100%"><tr><td>'; $series = ''; while ($row = mysql_fetch_assoc($query)) { $image = $row[image]; $datetime = $row[datetime]; $id = $row[id]; $type = $row[type]; $title = $row[title]; $count_display = $row[count_display]; if ($series != $row['series']) { //Assign category to holding var $series = $row['series']; //echo Category heading echo '<h3><a href="#" class="top"></a>'.$series.'</h3> '; } echo '<table align="center" class="format" cellpadding="0" cellspacing="0"> <tr><td> <img src="previews/'.$id.'.jpg" align="right"> <td class="design"><ul class="design_info"> <li class="special"><b>Title:</b> <a name="'.$title.'">'.$title.'</a> <ul> <li><b>Series:</b> '.$series.' <li><b>Type:</b> '.$type.' <li><b>Uploaded:</b> '.$datetime.' <li><b>Downloads:</b> '.$count_display.' </ul></ul> <ul class="design_dl"> <li class="preview"><a href="graphics/preview.php?id='.$id.'" target="_blank">preview</a> <li class="download"><a href="graphics/download.php?id='.$id.'">download</a> </ul> </td></tr> </table>'; } } echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/247888-second-argument-error/ Share on other sites More sharing options...
WebStyles Posted September 26, 2011 Share Posted September 26, 2011 well, you're incrementing $alpha on this line: $alpha[$records['letter']] += 1; but before that, where is it initialized? How are you even incrementing something that does not exist? It's bound to throw a warning. You need to check for existence first. try something like this: (mind you, I only read the first 3 lines of your code, but this seems to be the way to go) if(isset($alpha[$records['letter']])){ $alpha[$records['letter']] += 1; }else{ $alpha[$records['letter']] = 1; } Quote Link to comment https://forums.phpfreaks.com/topic/247888-second-argument-error/#findComment-1272890 Share on other sites More sharing options...
EternalSorrow Posted September 26, 2011 Author Share Posted September 26, 2011 If I read your post correctly, the code you gave me is to replace this piece of code? if (isset($_GET['letter']) && !empty($_GET['letter']) && !is_numeric($_GET['letter'])) { $letter = substr($_GET['letter'], 0, 1); } else { $letter = ''; } And sorry for the lack of inclusion, the information is fetched in a separate code that I've presented below to avoid further confusion: <?php connect to database here if (!is_numeric($_GET["type"]) && !empty($_GET["type"]) && $_GET["type"]!="") { $type = $_GET["type"]; } $query = "SELECT * FROM design WHERE type = '$type' LIMIT 1 "; $result = mysql_query($query) or die(mysql_error()); echo '<h1>Designs: '.$type.'</h1>'; while ($row3 = mysql_fetch_array($result)) { extract($row3); if ($type == 'Divider') { echo 'Divider designs are based on css positioning and focus on a table-less layout driven by the style-sheet. All designs are tested in the newest editions of both Internet Explorer and Firefox.'; } else if ($type == 'Table') { echo 'Table designs are positioned primarily through the use of html tables. All designs are tested in the newest editions of both Internet Explorer and Firefox.'; } else if ($type == 'Iframe') { echo 'Iframe designs use the inline framing technique to absolutely position items in the design. All designs are tested in the newest editions of both Internet Explorer and Firefox.'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/247888-second-argument-error/#findComment-1273007 Share on other sites More sharing options...
WebStyles Posted September 26, 2011 Share Posted September 26, 2011 ok, back to your first post, since this new code you psted does not even contain the variable $alpha, so it's not relevant right now... this is were the problem is: array_key_exists($i, $alpha) and this is the error you're getting: The second argument should be either an array or an object Since the error says 'the second argument', we know the problem is with $alpha. the rest of the error tells us everything: 'should be either an array or an object'. Since you expect it to be an array, we can rule out the object part. The only thing that is left it to figure out why $alpha is NOT an array... and here is were my first answer comes in. In my first answer, you'll find some code that belonged to you and some code I wrote to try and solve the problem. Replace your code (just the line I mentioned) with my code, and test again, see what happens, and get back to me please. good luck and I hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/247888-second-argument-error/#findComment-1273010 Share on other sites More sharing options...
jcbones Posted September 26, 2011 Share Posted September 26, 2011 WebStyles is right on target, but I will point out that his code doesn't go deep enough. Consider this: a snippet from your code while ($records = mysql_fetch_assoc($query)) { $alpha[$records['letter']] += 1; ${$records['letter']}[$records['series']] = $records['series']; } This is the first time that $alpha is in your script. You will notice that $alpha resides inside of a query loop. This query loop will never be called, if the database does NOT return any rows. Setting alpa to an empty array before the query loop will solve your problem, as $alpha will then be an array, albeit empty, but it will be an array. PS. I just tried it, you can increment an array that isn't set, but it will throw 2 notices. 1 for undefined variable, the other for undefined offset. In other words (BAD FORM!). My Suggestion in effect $alpha = array(); while ($records = mysql_fetch_assoc($query)) { $alpha[$records['letter']] += 1; ${$records['letter']}[$records['series']] = $records['series']; } Quote Link to comment https://forums.phpfreaks.com/topic/247888-second-argument-error/#findComment-1273021 Share on other sites More sharing options...
EternalSorrow Posted September 27, 2011 Author Share Posted September 27, 2011 Thanks to both of you for your help. I learned more about my code and I'll see if that slice worked. Again, thanks for the lesson! Quote Link to comment https://forums.phpfreaks.com/topic/247888-second-argument-error/#findComment-1273100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.