Jump to content

Shuffle Table rows and column


vignesht5

Recommended Posts

Hi,

 

I am using this table to generate dynamic password when each time user logs in the website. The password will be generated once generate button password is clicked. The password is generated based on picking alternate rows and columns from the table.

a b c d e
a y9
2j t1 r3 f8
b g5 k4
39 i2 j9
c w3 e2 t9 w3 r2
d oo h3 h5
96 45
e f6 k7 i3 a1 e3

 

My Code:

<!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=iso-8859-1" />
<title>Password</title>
</head>

<body><br /><br /><table width="100%" border="0"><tr> <td height="38" bgcolor="#FA8072" > <center>
<strong><em><h1><font color="#F5FFFA" >LOGIN PROCESS</font></h1></em></strong></center></td></tr> </table><br /><br/><br />



<?php
$k
=0;
do
{
$matrix[3][0]="h5";$matrix[3][1]="h3";$matrix[3][2]="96";$matrix[3][3]="45";$matrix[3][4]="oo";
$matrix[1][0]="39";$matrix[1][1]="k4";$matrix[1][2]="i2";$matrix[1][3]="j9";$matrix[1][4]="g5";
$matrix[0][0]="t1";$matrix[0][1]="2j";$matrix[0][2]="r3";$matrix[0][3]="f8";$matrix[0][4]="y9";
$matrix[4][0]="i3";$matrix[4][1]="k7";$matrix[4][2]="a1";$matrix[4][3]="e3";$matrix[4][4]="f6";
$matrix[2][0]="t9";$matrix[2][1]="e2";$matrix[2][2]="w3";$matrix[2][3]="r2";$matrix[2][4]="w3";

}
while($k>0);

$key[0]=4;
$key[1]=2;
$key[2]=1;
$key[3]=5;
$key[4]=3;
for($i=0;$i<5;$i++)
{
for($j=$i+1;$j<5;$j++)
{
if($key[$i]>$key[$j])
{
for($y=0;$y<5;$y++)
{
$temp[$y]=$matrix[$y][$i];
$temp1[$y]=$matrix[$y][$j];
}
for($y=0;$y<5;$y++)
{
$matrix[$y][$j]=$temp[$y];
$matrix[$y][$i]=$temp1[$y];
}
}
}
}

?>

<table align="center" width = "45%" height="65%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td align="center"><strong>    </strong></td>
<td align="center"><strong>a</strong></td>
<td align="center"><strong>b</strong></td>
<td align="center"><strong>c</strong></td>
<td align="center"><strong>d</strong></td>
<td align="center"><strong>e</strong></td></tr>
<tr>
<td align="center"><strong>a</strong></td>
<td align="center"><strong><?php echo $matrix[0][0]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[0][1]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[0][2]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[0][3]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[0][4]; ?></strong></td></tr>

<tr><td align="center"><strong>b</strong></td>
<td align="center"><strong><?php echo $matrix[1][0]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[1][1]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[1][2]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[1][3]; ?></strong></td>
<td align="center"><strong><?php echo$matrix[1][4]; ?></strong></td></tr>

<tr><td align="center"><strong>c</strong></td>
<td align="center"><strong><?php echo $matrix[2][0]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[2][1]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[2][2]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[2][3]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[2][4]; ?></strong></td></tr>

<tr><td align="center"><strong>d</strong></td>
<td align="center"><strong><?php echo $matrix[3][0]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[3][1]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[3][2]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[3][3]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[3][4]; ?></strong></td></tr>

<tr><td align="center"><strong>e</strong></td>
<td align="center"><strong><?php echo $matrix[4][0]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[4][1]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[4][2]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[4][3]; ?></strong></td>
<td align="center"><strong><?php echo $matrix[4][4]; ?></strong></td></tr>

</table>

 

But question is how could I possibly shuffle these cells values and show user in a table when they log in each time. Really appreciate any help on this.

Link to comment
https://forums.phpfreaks.com/topic/286437-shuffle-table-rows-and-column/
Share on other sites

Here's an easier way, if you don't mind my complete rewrite of your way of donig it:

 

<?php
function RandomPassword($len){
    $char_array = array();
    for ($i =48; $i<=126;$i++){
        $char_array[] = char($i);
    }
    $count = 0;
    $pass = "";
    while ($count < $len){
        $pass .= $char_array[array_rand($char_array)];
        $count++;
    }
    return $pass;
}
//Usage example:

$pass = RandomPassword(rand(8,15));
echo $pass;
?>

 

The function will generate an array of characters based on the ASCII table, then select random characters until it reaches the length you select.  In my sample call, it selects a random password with a random length between 8 and 15 characters long.

 

EDIT: forgot a couple things

 

EDIT 2: Man, I need sleep. Forgot that if I start with "0", the proper number will be one less than your required lenght.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.