0

I have a PHP code (wordpress) thats working fine, I just feel there is a smarter way of shortening the whole syntax by some array of some sort, somebody help, code neatness comes first, here it is:

<?php
    $user = wp_get_current_user();
    // is there someone logged?
    if ($user->ID) {
        echo '<ul class="availableindex nav nav-pills nav-stacked">';
        $n1 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER1'));
        $n2 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER2'));
        $n3 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER3'));
        $n4 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER4'));
        $n5 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER5'));
        $n6 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER6'));
        $n7 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER7'));
        $n8 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER8'));
        $n9 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER9'));
        $n10 = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER10'));

        echo '<li><a href=# class=dir>' . $n1 . '</a></li>';
        echo '<li><a href=# class=dir>' . $n2 . '</a></li>';
        echo '<li><a href=# class=dir>' . $n3 . '</a></li>';
        echo '<li><a href=# class=dir>' . $n4 . '</a></li>';
        echo '<li><a href=# class=dir>' . $n5 . '</a></li>';
        echo '<li><a href=# class=dir>' . $n6 . '</a></li>';
        echo '<li><a href=# class=dir>' . $n7 . '</a></li>';
        echo '<li><a href=# class=dir>' . $n8 . '</a></li>';
        echo '<li><a href=# class=dir>' . $n9 . '</a></li>';
        echo '<li><a href=# class=dir>' . $n10 . '</a></li>';
        echo '</ul>';
    }

?>

4 Answers 4

3

The shorten way could be

$numberArray = array('NUMBER1', 'NUMBER2',...);

foreach ($numberArray as $number)
{
   echo '<li><a href=# class=dir>' . cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, $number)) . '</a></li>';

}
Sign up to request clarification or add additional context in comments.

Comments

1
for($i = 1; $i <= 10; $i++) {
    echo '<li><a href=# class=dir>' .
    cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER' . $i))
   '</a></li>';
}

Comments

0

You can use a loop to create the $n array and then another loop to print it.

$count = 10;
for($i = 0; $i < $count; $i++) {
    $n[$i] = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER' . $i+1));
}
for($i = 0; $i < $count; $i++) {
    echo '<li><a href=# class=dir>' . $n[$i] . '</a></li>';
}

Or, if you don't need to do them separately, you can do it all in one loop without the need of an array at all.

$count = 10;
for($i = 0; $i < $count; $i++) {
    echo '<li><a href=# class=dir>' . cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER' . $i)); . '</a></li>';
}

Comments

0

.Try this

$user = wp_get_current_user();
// is there someone logged?
if ($user->ID) {
    echo '<ul class="availableindex nav nav-pills nav-stacked">';

    for($i=1; $i<=10; $i+=1){
      $t = cimy_uef_sanitize_content(get_cimyFieldValue($user->ID, 'NUMBER' . $i));
      echo '<li><a href=# class=dir>' . $n[$i] . '</a></li>';
    }
    echo '</ul>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.