Soy nuevo en jquery y php ambos. Tengo una página html y necesito usar algunas funciones en un archivo php a través de jquery y luego imprimir los resultados en html.
Lo que estoy tratando de hacer es contar la cantidad de archivos en 3 directorios diferentes y luego imprimir sus resultados en 3 divs diferentes. Cuando escribo solo una función en el archivo php y la llamo a través de la load en jquery, funciona bien. Pero no sé cómo usar este método para 3 funciones diferentes en el mismo archivo php. ¿Me podría ayudar?
HTML:
<div class="result1"></div> <div class="result2"></div> <div class="result3"></div> <script>window.onload = stats()</script>JS:
function stats() { $('.result1').load("counter.php"); //I don't know what to add here to get the result of only the function a1 in counter.php $('.result2').load("counter.php"); //and for a2 $('.result3').load("counter.php"); //and for a3 }PHP:
<?php function a1() { $i = 0; $dir = '../folder1/'; if ($handle = opendir($dir)) {while (($file = readdir($handle)) !== false){if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++;}} echo "$i"; } function a2() { $i = 0; $dir = '../folder2/'; if ($handle = opendir($dir)) {while (($file = readdir($handle)) !== false){if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++;}} echo "$i"; } function a3() { $i = 0; $dir = '../folder3/'; if ($handle = opendir($dir)) {while (($file = readdir($handle)) !== false){if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++;}} echo "$i"; } ?>Agregue un parámetro de URL que indique a qué función llamar. Luego verifique eso en el PHP.
JS:
function stats() { $('.result1').load("counter.php?action=a1"); $('.result2').load("counter.php?action=a2"); $('.result3').load("counter.php?action=a3"); }PHP:
<?php switch ($_GET['action']) { case 'a1': a1(); break; case 'a2': a2(); break; case 'a3': a3(); break; } function a1() { $i = 0; $dir = '../folder1/'; if ($handle = opendir($dir)) {while (($file = readdir($handle)) !== false){if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++;}} echo "$i"; } function a2() { $i = 0; $dir = '../folder2/'; if ($handle = opendir($dir)) {while (($file = readdir($handle)) !== false){if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++;}} echo "$i"; } function a3() { $i = 0; $dir = '../folder3/'; if ($handle = opendir($dir)) {while (($file = readdir($handle)) !== false){if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++;}} echo "$i"; } ?>