I assume that you haven't changed the code that calls your function. That's part of the problem.
Your code is currently saying something like this:
// while you can fetch a row
while ($sql = mysql_fetch_array($query)) {
// print category name
echo("$cat_name: ");
// while you can fetch a row
while ($sql = mysql_fetch_array($query)) {
// if the link is different from the last one
if (!old_link == $link) { // your syntax is wrong here*
// print the link
echo(\"<A href=$url>$link</A> . \");
}
}
}
- "if (!old_link == $link)" doesn't mean what you think it does. You need to either use parentheses, if (!($old_link == $link)) or the "not equal" operator if ($old_link != $link). And check your code to make sure your variables always start with '$'.
Now to the program logic.
First, you should only have one "mysql_fetch_array($query)" statement in your code. So instead of this:
while ($sql = mysql_fetch_array($query)) {
$cat_name = $sql[\"category_name\"];
echo(\"$cat_name: \");
linklist2();
}
you should just have this:
linklist2();
and print cat name from linklist2().
Or you could just take the code from the function and use it directly. The function should only be called once anyway.
Second, you're on the right track for determining whether a field changes, but you're testing the wrong field. Remember, you want to display a link for every record, but only display category names when the category changes, so you need to test and print $cat_name, not $link.
Also, don't forget to add an "order by category_name" (or "order by category_id") to your SQL. Otherwise your results could look like this:
Search: link link ...
Web: link ..
Search: link ...
Web: link link ..
HTH,
Beth