2017-02-01 65 views
0

在PHP文件中,任何人都可以帮助我直接创建一个for循环,我不知道要选择的行数。从PHP发送数组到javascript

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "mydb";//database details 
    $conn = new mysqli($servername, $username, $password, $dbname); 

    if ($conn->connect_error) 
    { 
     die("Connection failed: " . $conn->connect_error); 
    } 

    $sql = "SELECT * FROM mytable"; 
    $result = $conn->query($sql); 
    $myarray = array(); 
    $index = 0; 

    if ($result->num_rows > 0) 
    { 
     // output data of each row 
     while($row = $result->fetch_assoc()) 
     { 
      $myarray[$index] = $row["firstname"]; 
      $index++; 
      $myarray[$index] = $row["lastname"]; 
      $index++; 
      $myarray[$index] = $row["email"]; 
      $index++; 
     } 
    } 
    else 
    { 
     echo "0 results"; 
    } 
    $conn->close(); 
?> 

的JavaScript代码包含一个搜索栏建议编写JavaScript代码,在这里我当你在阅读没有提到的JavaScript文件

var substringMatcher = function(strs) { 
    return function findMatches(q, cb) { 
     var matches, substringRegex; 

     // an array that will be populated with substring matches 
     matches = []; 

     // regex used to determine if a string contains the substring `q` 
     substrRegex = new RegExp(q, 'i'); 

     // iterate through the pool of strings and for any string that 
     // contains the substring `q`, add it to the `matches` array 
     $.each(strs, function(i, str) { 
      if (substrRegex.test(str)) { 
       matches.push(str); 
      } 
     }); 

     cb(matches); 
    }; 
}; 

$('#the-basics .typeahead').typeahead({ 
    hint: true, //here i used typeahead js code though i didnt mentioned here 
    highlight: true, 
    minLength: 1 
}, { 
    name: 'states', 
    source: substringMatcher(states) 
}); 
+0

如果您不知道行,你必须做一个while循环 –

回答

0

您可以一点点在这里简化了PHP的预输入值你不需要增加索引:

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "mydb";//database details 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname);//making a connection 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT * FROM mytable"; 
$result = $conn->query($sql); 
$myarray = array(); 
if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     $myarray[] =$row["firstname"]; 
     $myarray[] =$row["lastname"];//storing in the array 
     $myarray[] =$row["email"]; 
    } 
} else { 
    echo "0 results"; 
} 
$conn->close(); 
?> 

你可以在结果给Javascript,你需要它们,echo($myarray[0])例如回声。

你的JavaScript可能会是这个样子:

var matches = <?php echo($myarray[0]); ?> 
+0

谢谢,可以üplz帮助我如何连接的PHP文件的HTML或JS文件,我需要PHP文件运行加载页面,并提前致谢 –

+0

你可以把PHP中的HTML代码,只是确保该文件是.php –

+0

谢谢,但你可以告诉我如何做,因为我必须做在不同的文件,我如何通过数组,如果我需要在同一个文件上写,应该怎么做,非常感谢 –

-1

有4种方式,你可以做插入动态内容到您的JavaScript代码。

  1. 将JavaScript内联到您的PHP页面。也就是说,把你的js代码放在里面<script>标签

  2. 使动态js变量变为全局变量并将它们填充到你的php页面中。你单独的JS文件将能够读取这些全局js变量。

  3. 如果你想为JS一个单独的文件,你必须每次生成它(因为你不能在JavaScript中的PHP代码。浏览器无法解释它,并且服务器不能解释它)
  4. 如果你想强制服务器解释JS文件中的PHP代码,添加一个处理程序,以便通过php解释器处理.js处理程序(无论是php模块还是php-cgi)请参阅Clean links to PHP-generated JavaScript and CSS