Php generate a progressive number

to fill dynamically (automatically) the position (structured data) metadata I need to generate a progressive number with php.
You can see my code in this thread. I highlight here the rows affected

$breadcrumbs[] = "<li itemprop='itemListElement' itemscope itemtype='https://schema.org/ListItem'>
            <a href=\"$base$crumbs$crumb\" itemprop='item' href='https://schema.org/ListItem'>
            <span itemprop='name'>$title</span>
            <meta itemprop='position'  content='$num' />
            </a> »</li>";

What I need is a php code that can generate as $num a progressive series (1,2,3,4) of numbers, so that structured data be correct.

What code?
Thank you!

EDIT

this code works (in a new page)

<?php
 
// creating array with elements from 0 to 6
// using range function
$arr = range(0,6);
 
// printing elements of array
foreach ($arr as $a) {
     
    echo "$a ";
}
 
?>

but how insert it in my whole code? I have already if and foreach instructions.

you’re close.

foreach($arrayname as $key => $value)

the first value in a numeric array will have key 0, the second key 1, etc.

1 Like

Thank you!
But I don’t understand how should I modify my code. I report it fully:

function breadcrumbs($separator = ' &raquo; ', $home = 'Home')
  {
    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = array("<li><a href=\"$base\">$home</a></li>");

    // Initialize crumbs to track path for proper link
    $crumbs = '';

    // Find out the index for the last value in our path array
    $last = end(array_keys($path));

    // Build the rest of the breadcrumbs
    foreach ($path as $x => $crumb) {
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(array('.php', '_', '%20'), array('', ' ', ' '), $crumb));
        
        // If we are not on the last index, then display an <a> tag
        if ($x != $last) {
            $breadcrumbs[] = "<li itemprop='itemListElement' itemscope itemtype='https://schema.org/ListItem'>
            <a href=\"$base$crumbs$crumb\" itemprop='item' href='https://schema.org/ListItem'>
            <span itemprop='name'>$title</span>
            <meta itemprop='position' content='$num'  />
            </a> »</li>";
            $crumbs .= $crumb . '/';
        }
        // Otherwise, just display the title (minus)
        else {
            $breadcrumbs[] = $title;
        }

    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

Where $num is not definied, so far.

$num = $x.

1 Like

Perfect!
Thank you.

I re-add the whole new code, so that it could be useful for others:

function breadcrumbs($separator = ' &raquo; ', $home = 'Home')
  {
    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = array("<li><a href=\"$base\">$home</a></li>");

    // Initialize crumbs to track path for proper link
    $crumbs = '';

    // Find out the index for the last value in our path array
    $last = end(array_keys($path));

    // Build the rest of the breadcrumbs
    foreach ($path as $x => $crumb) {
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(array('.php', '_', '%20'), array('', ' ', ' '), $crumb));
        
        // If we are not on the last index, then display an <a> tag
        if ($x != $last) {
            $breadcrumbs[] = "<li itemprop='itemListElement' itemscope itemtype='https://schema.org/ListItem'>
            <a href=\"$base$crumbs$crumb\" itemprop='item' href='https://schema.org/ListItem'>
            <span itemprop='name'>$title</span>
            <meta itemprop='position' content='$x'  />
            </a> »</li>";
            $crumbs .= $crumb . '/';
        }
        // Otherwise, just display the title (minus)
        else {
            $breadcrumbs[] = $title;
        }

    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);

Thank you very, very much!!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.