• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2059
  • Last Modified:

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
0
tdillon80
Asked:
tdillon80
1 Solution
 
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
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
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
 
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

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now