2

I'm displaying a form on admin url :admin/settings/custom. In the page argument I pass array('custom_admin_form'), to the menu item. In the form function I'm writing form elements to display folder and file names present in the public folder. I get those values through the code below:

$dir_path = variable_get('file_public_path', conf_path(). '/files') ;
$fileslist = glob($dir_path . "*.*"); 
$dirs = glob($dir_path . '/*' , GLOB_ONLYDIR);
foreach($dirs as $directs){
     $names = explode('/', $directs);
     $fnames[] = end($names);
}

How can I display the results in the form function? I tried it using print, echo, drupal_render but nothing is working. I've also used #markup element, but it was displaying array(), not the values of array.

2

You can try something like this:

 $form['sample'] = array(
     '#type' => 'item',
     '#markup' => print_r($sample_values, TRUE),        
 ); 

or simply just

print_r($sample_values);

or install Devel module and use dsm function.

dsm($sample_values);

UPDATE:

Try this:

$form['sample'] = array(
    '#type' => 'item',
    '#markup' => print_array_values($sample_values),        
); 

Then declare the function below:

function print_array_values($sample_values){
    $content = '';
    foreach($sample_values as $val){
            $content .= '<div>' . $val . '</div>';
    }

    return $content;
}
  • Thankyou sir, but print_r($sample_values); will not display the values where i want to display. is there any way to display values by using foreach loop for form element.So that for each value it will print value. – Suraj Aug 4 '16 at 8:35
  • I edit the answer to add the second argument in the print_r function (TRUE), to return the sting instead of printing it. – TytooF Aug 4 '16 at 15:02
  • 1
    @Suraj -> I updated my answer. Anyways, call me Ms. :) – Eliyah Aug 5 '16 at 8:17
1

I would suggest dpm or dsm or kpr, which is very common among Drupal developer for printing variables to debug in Drupal 7. But you need to enable Devel module in your site first as @AlyssaGono suggested.

If you want to use something not depends on other module, you can use var_dump().

Example:

var_dump($variable);
drupal_exit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.