2014-01-28 70 views
-1

我想循環這個函數「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 &copy; 2013 
</div> 
</body> 
</html> 
+2

你不會從'generatePassword()'返回任何東西,那麼你期望從循環中獲得什麼?或者爲此只運行一次。 – kba

+0

在循環結構中調用該函數。這是一個詭計嗎? – crush

+1

更嚴重的是,絕對不需要發佈代碼中的每一部分,包括與問題完全無關的部分。你真的認爲你的CSS阻止你調用PHP函數嗎?提出有效的問題會產生有效的答案! – crush

回答

1

做這件事時:

$func = generatePassword(); 
for ($x=0;$x<5;$x++) { 
    $func; 
} 

您執行generatePassword()一次,則返回值賦給變量$func。那麼你的循環基本上什麼都不做

你應該這樣做,而不是:

for ($x=0;$x<5;$x++) { 
    $password = generatePassword(); 
    // then do whatever you want to do with each password 
} 

如果你想改變所有的密碼在你的數據庫,你應該首先選擇要更新數據庫行,然後爲每個他們生成密碼並更新您的數據庫。

// generate n passwords 
for ($x=0 ; $x < 5 ; $x++) { 

    // generate one password 
    $password = generatePassword(); 

    // insert password in database 
    insertPassword($password); 
} 
+0

這將結束在一個無限循環,不是?你必須設置一個標誌來停止這個無限循環。或類似的東西。 –

+1

@ManuelRicharz'for($ x = 0; $ x <5; $ x ++)'不是無止境的... – Tchoupi

+1

@ManuelRicharz:循環的終止條件是$ x <5'。 (或者,相反,在for循環的情況下是持續狀態)。 – David

1

看看這個代碼:

$func = generatePassword(); 
for ($x=0;$x<5;$x++) { 
    $func; 
} 

似乎什麼,你希望發生的是,$func持有一些參考generatePassword功能,並通過其本身你會調用$func調用該函數。但是,事實並非如此。 $func實際上持有結果執行gneeratePassword()函數一次。之後它的值不會改變。

操作爲第一線的順序,實際上是由右至左操作,方法是:

  • 調用generatePassword功能
  • 獲取該功能
  • 存放在$func結果值的結果

爲了在循環的每次迭代中執行該函數,只需直接調用它:

for ($x=0;$x<5;$x++) { 
    generatePassword(); 
} 

注意:但是,我懷疑這不是您在此代碼中遇到的唯一問題。我不清楚自己在這裏要做什麼,或者通過多次或甚至一次執行該函數期望看到(或應該看到)的效果。

0

你有兩個問題:

  1. 至於其他的答案解釋,你只嘗試一次執行generatePassword,那麼你只是循環的結果,並沒有做任何事的。

  2. 您的循環實際上在generatePassword函數中。 (我編輯你的問題解決壓痕,所以你可以看到這一點。)

我想你想要的是:

function generatePassword($length = 15) 
{ 
    // ... all the stuff you have before your loop 

    // done! 
    return $password; 
} 

for ($x = 0; $x < 5; $x++) { 
    generatePassword(); 
} 

你可能要替換returnecho,所以你可以看到什麼這是。