Page 1 of 1

Having trouble with shuffle code !!!

Posted: Fri Jan 14, 2005 9:46 am
by hiewall
I am working with code to shuffle numbers. I am able to use it for my horizontal (top row) and it works fine.

My problem is getting it to work for the vertical ( red) side row.

The problem is that when I apply this same code to the vertical ---- the numbers inside the square get all messed up -- they have to remain 0 thru 99.

Thanks for your help !!!!

The section of code that applies to this problem is :


Code: Select all

<table border=1 width=800 cellspacing=0 cellpadding=5>
<tr>
     <td align=center width="9%" height=25 valign=top><font color=black><b></b></font><br><font color=red><b></b></font></td>
     <td width="1%"></td>
<?
// THIS IS WHERE ALL OF THE MAGIC HAPPENS.  FIRST WE PRINT OUT THE TOP ROW OF SQUARES...
$randarray=array();
 for($k=0; $k<=9; $k++) &#123;
    $randarray&#1111;] = $k;
&#125;
shuffle($randarray);
foreach ($randarray as $k) &#123;
     echo "\t<td width="9%" height=25 align=center><b>"; if ($mystery) &#123; echo "<name=col$k>?"; &#125; else &#123; echo "$k"; &#125; echo "</b></td>\n";
&#125; ?>
<tr>
     <td height=10></td>
</tr>
<?


// NOW THE GRID IS BEING PRINTED
for($i=0; $i<=9; $i++) &#123;
     $rownew = "row"."$i";
     echo "<tr>\n\t<td align=center><font color=red><b>"; if ($mystery) &#123; echo "<name=$rownew>?"; &#125; else &#123; echo "$i"; &#125; echo "</b></font></td><td width="1%"></td>";
     for($j=0; $j<=9; $j++) &#123;
          $colnew = "col"."$j";
          $both_loop = $rownew.$colnew;
          $formatted_selection = $rownew.':'.$colnew;
          $number = $i.$j;
          $digits = sprintf('%2d', $number);
          if ($&#123;$rownew.$colnew&#125;) &#123;
               $pick = "$rownew:$colnew";
               
                    echo "\n\t<td width="9%" align=center>"; if ($mystery) &#123; echo "<font color=red><b><name=$rownew>?</b></font> : <b><name=$colnew>?</b>"; &#125; else &#123; echo "<b>$digits</b>"; &#125; echo "<br><font size=1><b>$name</b></font></td>";
               &#125; // endwhile
           else &#123;
               echo "\n\t<td width="9%" align=center>"; if ($mystery) &#123; echo "<font color=red><b><name=$rownew>?</b></font> : <b><name=$colnew>?</b>"; &#125; else &#123; echo "<b>$digits</b>"; &#125; echo "<br><input type=radio name="selection" value="$rownew:$colnew""; if ($selection == $formatted_selection) echo " checked"; echo "></td>";
          &#125;
     &#125; //for($j=0; $j<=9; $j++) &#123;
     echo "\n</tr>\n";
&#125; //for($i=0; $i<=9; $i++) &#123;
?>
</table>
<br>
<? if ($squares_add) &#123; ?>
<input class=button type="submit" value="Submit" name="submit">
<input class=button type="reset" value = "Clear">
<? &#125; ?>
<br><br><a href="http://<?=$website?>">Return to <?=$website?></a>
</center>
</body>
</html>
My entire code is in http://poolsforfun.com/squares2.php

Posted: Fri Jan 14, 2005 9:58 am
by pickle
It's difficult to read your code, please wrap it in [syntax=php][/syntax] or [syntax=php][/syntax] (preferably) tags.

Posted: Fri Jan 14, 2005 10:09 am
by hiewall
I'm new at this and am not sure what you mean by "wrap your code".

I thought that the code is pretty obvious.

Posted: Fri Jan 14, 2005 10:15 am
by feyd
after it's been placed in formatting, like it is now.. (which I did for you) it becomes easier to read. Posting the code without some form of formatting makes it difficult for us to help..

As for your problem.. where do the variable variables come from? "${$rownew.$colnew}"

Can you post an example of what gets output?

Posted: Fri Jan 14, 2005 10:22 am
by hiewall
If you just click on the url at the bottom of my original post --- you will see what the code produces.

Thanks for your help

Posted: Fri Jan 14, 2005 10:29 am
by feyd
that still doesn't answer my first question of where the variable variables come from...



html note: the first table row isn't terminated & should use a span in the second (spacer) row..

Posted: Fri Jan 14, 2005 10:37 am
by hiewall
Please forgive my ignorance --- but what do you mean --- where do they come from???? They are ceated in the code.

Like I said --- I am kind of new at this --- so please be patient with my ignorance.

Posted: Fri Jan 14, 2005 10:53 am
by feyd
${$rownew.$colnew} isn't "$rownew:$colnew"

this seems to be what you are after more, you'll have to add the magic stuff.. but the randomization is there.

Code: Select all

<?php

$cols = $rows = array();
for($i = 0; $i < 10; $i++)
	$rows&#1111;] = $cols&#1111;] = $i;

shuffle($rows);
shuffle($cols);

echo '<table width="800" border="1" cellpadding="0" cellspacing="0"><tr><td width="9%">&nbsp;</td><td width="1%">&nbsp;</td>';
for($i = 0; $i < 10; $i++)
&#123;
	echo '<td width="10%" style="color:black;text-align:center">' . $cols&#1111;$i] . '</td>';
&#125;

echo '</tr>'."\n".'<tr><td colspan="12" height="10">&nbsp;</td></tr>'."\n";

for($i = 0; $i < 10; $i++)
&#123;
	echo '<tr><td width="9%" style="color:red;text-align:center">' . $rows&#1111;$i] . '</td><td width="1%">&nbsp;</td>';
	for($j = 0; $j < 10; $j++)
	&#123;
		echo '<td style="text-align:center" width="10%">' . ($i!=0?$i:'') . $j . '<br /><input type="radio" name="selection" value="row' . $i . ':col' . $j . '" /></td>';
	&#125;
	echo '</tr>' . "\n";
&#125;

echo '</table>';

?>

Posted: Sat Jan 15, 2005 9:58 am
by hiewall
Thanks Feyd ----- this is exactly what I needed. I really appreciate your help.