PHP last element of multidimensional array

I'm using PHP / MySQL.

The do / while loop displays a link for each category horizontally across the page. After each link a "pipe" | is displayed. After the last link I do not want the "pipe" displayed.

<?php
mysql_select_db($database_conn, $conn);
$query_categories = "SELECT id_ctg, name_ctg FROM categories_ctg WHERE idctg_ctg = 1 ORDER BY order_ctg ASC";
$categories = mysql_query($query_categories, $conn) or die(mysql_error());
$row_categories = mysql_fetch_assoc($categories);
$totalRows_categories = mysql_num_rows($categories);
?>



<?php do { ?>
      <a href="section.php?category=<?php echo $row_categories['id_ctg']; ?>" class="toyslink"><?php echo $row_categories['name_ctg']; ?></a> |
<?php } while ($row_categories = mysql_fetch_assoc($categories)); ?>

I've researched array functions on this exchange as well as other php sites. I've tried
array_pop()  
end()
count()
without success.

Could somebody please shed light on this subject?

Thanks
tdillon80Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Oddsen84Commented:
You can use:
$length = count($array);
foreach($array as $array_node=>$array_element)
{
    echo $array_element;
    if($array_node < $length)
    {
        echo '|';
    }
}

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
let's see:

<?php
mysql_select_db($database_conn, $conn);
$query_categories = "SELECT id_ctg, name_ctg FROM categories_ctg WHERE idctg_ctg = 1 ORDER BY order_ctg ASC";
$categories = mysql_query($query_categories, $conn) or die(mysql_error());
$totalRows_categories = mysql_num_rows($categories);  
$menu = array(); 
while ($row_categories = mysql_fetch_assoc($categories))
{
  $menu[] = "<a href=\"section.php?category={$row_categories['id_ctg']}" class=\"toyslink\">{$row_categories['name_ctg']}</a>"; 
}
print join("|", $menu); 
?>

Open in new window

0
Oddsen84Commented:
count(); counts the elements in an array.
the foreach can consist of three items:
the array, an id(this must be numerical I think) and the looping element
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

nplibCommented:
Or using the same logic, which would make more sense you can do this.

<?php
$rows_total = mysql_num_rows($categories);
$counter = 0;
  do {
     echo '<a href="section.php?category='.$row_categories['id_ctg'].' class="toyslink">'.$row_categories['name_ctg'].'</a>';
     if ($counter < $rows_total) {////try <= if that doesn't work
        echo '|';
     }
  } while ($row_categories = mysql_fetch_assoc($categories));
?>

Open in new window

0
Oddsen84Commented:
Oops found a mistake in my code. You need to subtrack 1 from the length of the array since the array starts on 0 but counts it as the first element :)
0
john chambersweb designerCommented:
What you want to do is join the array elements together using the implode() function. This takes in a delimiter and an array and does exactly what you are looking for...
<?php
mysql_select_db($database_conn, $conn);
$query_categories = "SELECT id_ctg, name_ctg FROM categories_ctg WHERE idctg_ctg = 1 ORDER BY order_ctg ASC";
$categories = mysql_query($query_categories, $conn) or die(mysql_error());
$row_categories = mysql_fetch_assoc($categories);
$totalRows_categories = mysql_num_rows($categories); 
$menu=array();
while($row_categories = mysql_fetch_assoc($categories)) {
  //push each category link into the menu
  array_push($menu,"<a href=\"section.php?category={$row_categories['id_ctg']}" class=\"toyslink\">{$row_categories['name_ctg']}</a>");
}
$menu_links = implode(" | ",$menu);
echo $menu_links;
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Oddsen84Commented:
I would say hemjesti is completly correct.. Why didn't I just think of that ;) It saves on proccessing power to and is shorter than any other method here.
0
Richard QuadlingSenior Software DeveloperCommented:
There are 2 ways I can quickly think of ...

1 - Build a string and rtrim($s_String, ' |');
2 - Add | before the category except for the first category.

<?php
$s_String = '';
while(...) {
$s_String .=( ('' !== $s_String) ? ' | ' : '') . $category;


sort of thing.

0
tdillon80Author Commented:
I got the answer. Thanks for your help though.

<?php
mysql_select_db($database_conn, $conn);
$query_categories = "SELECT id_ctg, name_ctg FROM categories_ctg WHERE idctg_ctg = 1 ORDER BY order_ctg ASC";
$categories = mysql_query($query_categories, $conn) or die(mysql_error());

$j = 1;
$row_categories = array();
while($row = mysql_fetch_array($categories, MYSQL_ASSOC)) {
        foreach($row as $colname => $value) {
                $row_categories[$j][$colname] = $value;
        }
        $j++;
}

?>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.