Jump to content

Problem With Arrays And My Logic


supertrucker

Recommended Posts

Here's what I want to do.

  1. Retrieve a bunch of rows from a table in a database and store them in an array.
  2. Take one part of those rows, say field 'Title' and prepend the title with a particular string.
  3. 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 67
I 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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...