我想循環這個函數「x」次。我對如何循環一個函數有點困惑。我設置了這個功能,當我只想運行一次這個功能時,一切都可以。但我想要做的事情是循環generatePassword()
函數。我怎樣才能做到這一點。在PHP中循環使用函數?
function generatePassword ($length = 15)
{
//start with a blank password
$password = "";
// define possible characters - any character in this string can be
// picked for use in the password, so if you want to put vowels back in
// or add special characters such as exclamation marks, this is where
// you should do it
$possible = "12346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";
// we refer to the length of $possible a few times, so let's grab it now
$maxlength = strlen($possible);
// check for length overflow and truncate if necessary
if ($length > $maxlength) {
$length = $maxlength;
}
// set up a counter for how many characters are in the password so far
$i = 0;
// add random characters to $password until $length is reached
while ($i < $length) {
// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, $maxlength-1), 1);
// have we already used this character in $password?
if (!strstr($password, $char)) {
// no, so it's OK to add it onto the end of whatever we've already got...
$password .= $char;
// ... and increase the counter by one
$i++;
}
}
//THIS IS THE PART I CANT GET
$func = generatePassword();
for ($x=0;$x<5;$x++) {
$func;
}
// done!
return $password;
}
//databse connection
include_once 'config.php';
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$votes = generatePassword (15); //whatever length
//insert votes
$sql=("INSERT INTO registered (voters) VALUES ('$votes')");
if (!mysql_query($sql,$link))
{
die('Error: ' . mysql_error());
}
header("location: administrator.php");
mysql_close($link);
我 這是我的第一頁:
<?php
require_once('admin-auth.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/>
<link rel="SHORTCUT ICON" href="images/log.png">
<title>Student Voting System</title>
<style>
#tbl
{
font-family:Tahoma, Geneva, sans-serif;
border-collapse:collapse;
margin-bottom:20px;
width:885px;
}
#tbl td, #tbl th
{
font-size:11px;
border:1px solid #094f4b;
padding:3px 7px 2px 7px;
background-color:#ffffff;
color:#4b4b4b;
font-family:Tahoma, Geneva, sans-serif;
}
#tbl th
{
font-size:14px;
text-align:left;
padding-top:5px;
padding-bottom:4px;
background-color:#116763;
color:#ffffff;
}
</style>
</head>
<body>
<div id="bar">
<div style="width:900px; margin:auto; padding-top:8px;">
<img src="images/aclc-logo.png" />
</div>
</div>
<div id="subbar">
<table style="padding-top:80px; width:890px; margin:auto; text-align:right;">
<tr>
<td id="bold" style="color:#FFF; padding-left:10px;">
<a href="administrator.php">Passcodes</a> | <a href="generating.php">Election Results</a> | <a href="print.php">Save/Print</a> | <a href="admin-logout.php">Logout</a>
</td>
</tr>
</table>
</div>
<div id="content">
<center>
<div id="scroll">
<table style="margin-top:0px;">
<tr>
<td colspan="2" style="font-family:Tahoma, Geneva, sans-serif; font-size:16px; padding-bottom:10px;"><b>ADMINISTRATOR</b></td>
</tr>
<tr>
<td colspan="2" style="font-family:Tahoma, Geneva, sans-serif; font-size:11px; color:#116763;"><a href="registered.php"><b style="color:#116763;">Generate New Passcode</b></a></td></tr>
<tr><td colspan="2" style="font-family:Tahoma, Geneva, sans-serif; font-size:11px; color:#116763;">
<form action="registerednumber.php" method="POST" /><b style="color:#116763;">Generate New Passcode </b></a><input type="text" maxlength="3" name="number" method="POST" /> times. (max:999)<input type="submit" value="GO!"/></form></td>
</tr>
<tr>
<td colspan="2">
<?php
//databse connection
include_once 'config.php';
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Create query
$row="SELECT * FROM registered";
$result=mysql_query($row);
echo "<table id='tbl'>
<tr>
<th width='60'>Number</th>
<th>Generated Password</th>
<th>Full Name</th>
<th>Signature</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><b>" . $row['id'] . "</b></td>";
echo "<td><b style='color:red;'>" . $row['voters'] . "</b></td>";
echo "<td><b style='color:red;'></b></td>";
echo "<td><b style='color:red;'></b></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($link);
?>
</td>
</tr>
</table>
</div>
</center>
</div>
<div id="footer">
ACLC Voting System © 2013
</div>
</body>
</html>
你不會從'generatePassword()'返回任何東西,那麼你期望從循環中獲得什麼?或者爲此只運行一次。 – kba
在循環結構中調用該函數。這是一個詭計嗎? – crush
更嚴重的是,絕對不需要發佈代碼中的每一部分,包括與問題完全無關的部分。你真的認爲你的CSS阻止你調用PHP函數嗎?提出有效的問題會產生有效的答案! – crush