supertrucker Posted May 14, 2009 Share Posted May 14, 2009 Here's what I want to do. Retrieve a bunch of rows from a table in a database and store them in an array. Take one part of those rows, say field 'Title' and prepend the title with a particular string. Sort the array alphabetically using the 'Title' field. However, part of my logic isn't right, and I'm getting unexpected results, just at step one.Here's my code so far function ShowDirectoryList($sel){ include("global_vars.php"); $sql="SELECT * FROM wapdirectory ORDER BY ParentId"; $line=0; echo "<br/>Current category: $sel<br/>"; $con = mysql_connect($dbhost,$dbu,$dbp); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $con); $result = mysql_query($sql); echo '<select name="ParentId">'; echo '<option value="">No Category</option> '; while($row = mysql_fetch_array($result)) { $psql="SELECT Title FROM wapdirectory WHERE DirId='$row[ParentId]' ORDER BY ParentId, Title"; $parent=ExecuteSQL($psql, TRUE); $parent=$parent['0']; if ($sel != $row['DirId']) $selected=""; else $selected='selected="selected" '; echo '<option value="'.$row['DirId'].'" '.$selected.'>'.$parent."/".$row['Title'].'</option>'; // TEST if (isset($sort)) { $sort = array_merge($sort, $row); } else { $sort = $row; } // TEST } echo "</select>"; foreach($sort as $row[]) { echo $row['DirId']; }} The part that I am confused about is... foreach($sort as $row[]) { echo $row['DirId']; } ...which generates this error: Notice: Undefined index: DirId in /hermes/bosweb/web237/b2379/sl.supertru/public_html/zerorubbish/dir_funcs.php on line 67I expect it to just spit out every 'DirId' in the $sort array. Am I confusing my logic somehow? I appreciate any help! Link to comment Share on other sites More sharing options...
justsomeguy Posted May 14, 2009 Share Posted May 14, 2009 It sounds like the wapdirectory table doesn't have a field called "DirId". You can use print_r on $row to see what's in the array. Link to comment Share on other sites More sharing options...
supertrucker Posted May 16, 2009 Author Share Posted May 16, 2009 It sounds like the wapdirectory table doesn't have a field called "DirId". You can use print_r on $row to see what's in the array.It's most definitely there. I use that table pretty heavily throughout my scripts, the two fields in it are "DirId", which is the index, and "Title", which is just a descriptive name. In anycase, I think it may have something to do with the fact that this is a multi-dimensional array, and that I'm just referencing it incorrectly. I found an example online somewhere that shows an example of sorting/referencing a multidimensional array, but I couldn't get it to apply to my situation. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 17, 2009 Share Posted May 17, 2009 I think I was looking inside the while loop, not after it.I don't think that array_merge is doing what you expect it to. After the while loop use print_r on $sort and see what you made it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.