#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Counting array pieces


    I just converted a query that displays "bread crumbs" style navigation links to PDO:

    Code:
     
    function get_path($dbh,$node,$TopnavTable, $TopnavName) {
    $stmt = $dbh->prepare("SELECT name FROM $TopnavTable WHERE $TopnavName = ?");
    $stmt->bindValue(1,$node);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $row = $stmt->fetch();
    $path = array_merge(get_path($pdo,$row['Parent'], $TopnavTable, $TopnavName), $path);
    return $path ;
    }
    Now I'm trying to figure out how to display the results. I want an arrow (&gt following each segment but the last, and I'd like every segment linked except the last one, which I'm also going to put inside a span.

    I came up with this scheme, which works pretty well:

    Code:
    $Path2 = explode("/", $path);
    $Path2[1] = '<a href="/Topics">'.$Path2[1].'</a>';
    $Path2[2] = '<a href="/Topics/'.$Path2[2].'">'.$Path2[2].'</a>';
    $Path2[3] = '<span class="navHere"><b>'.$Path2[3].'</b></span>';
    				
    echo join( $Path2, ' &gt; ' );
    However, it only works only if I'm working with an array consisting of three segments. For example, I'm viewing the URL MySite/Topics/Washington/Governor, which displays the following bread crumbs trail:

    Topics > Washington > Governor

    If I view MySite/Washington, it should display...

    Topics > Washington

    But I get an error message: Undefined offset: 3, plus there's a trailing arrow (&gt after Washington.

    So I guess I need to figure out a way to count the number of pieces or segments in an array, so I can make a PHP switch that lets me display whatever's available, like this:

    Code:
    switch($Number_Pieces)
    {
     case 1:
    $Path2[1] = '<a href="/Topics">'.$Path2[1].'</a>';
     break;
    
     case 2:
    $Path2[1] = '<a href="/Topics">'.$Path2[1].'</a>';
    $Path2[2] = '<span class="navHere"><b>'.$Path2[3].'</b></span>';
     break;
    
     case 3:
    $Path2[1] = '<a href="/Topics">'.$Path2[1].'</a>';
    $Path2[2] = '<a href="/Topics/'.$Path2[2].'">'.$Path2[2].'</a>';
    $Path2[3] = '<span class="navHere"><b>'.$Path2[3].'</b></span>';
     break;
    
     case 6:
    $Path2[1] = '<a href="/Topics">'.$Path2[1].'</a>';
    $Path2[2] = '<a href="/Topics/'.$Path2[2].'">'.$Path2[2].'</a>';
    // $Path2[3, 4 and 5]
    $Path2[6] = '<span class="navHere"><b>'.$Path2[3].'</b></span>';
     break;
    
     default:
     break;
    }
    Can anyone tell me how to count the pieces in this array? Or is there a way to make it automatically do what I want it to do without counting the array pieces?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    first of all, congratulations for switching to PDO and prepared statements. That was a good decision.

    However, you need to escape the values coming from the database before you can insert them into the HTML document: Never insert raw values into an HTML context.

    Regarding the navigation, you can simple loop through the elements and check the index. For the first index, you display a span element. Otherwise, you display a "&gt;" followed by a link. This automatically gives you the layout you want.

    Do you know how to use for loops?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    Regarding the navigation, you can simple loop through the elements and check the index. For the first index, you display a span element. Otherwise, you display a "&gt;" followed by a link. This automatically gives you the layout you want.

    Do you know how to use for loops?
    Sorry, I'm a little new to this. Could you show me an example? Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo