Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   help for (for) (http://www.programmingforums.org/showthread.php?t=15500)

dark06 Mar 28th, 2008 3:53 AM

help for (for)
 
<?
$c = $_POST['col'];
$r = $_POST['rw'];
?>

<?
for($cvchk = 1; $cchk<= $c; $cchk ++)
{
echo $cchk."<br>";

for($rchk = 1; $rchk<= $r; $rchk ++)
{
echo $rchk." ";
}
}

?>

i don't know what is wrong with this one
i want the output to become this one

1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3

i really kinda new with this thanks a lot guys

guys can you help me out with this one.. :icon_redface:

Freaky Chris Mar 28th, 2008 6:15 AM

Re: help for (for)
 
Couple of things, firtsly could you use code tags to place code in a post.

Secondly welcome to the forums.

OK one of your first problems was your initial for statement had seom variables named wrongly

:

  1. for($cvchk = 1; $cchk <= $c; $cchk ++)


then there were a few other problems to such as at no point were you specifying to start from any number other than 1, so it would always count from 1-4.

you were also echoing the value of cchk for no reason.

here is the code i come up with not the best for many reasons, such as im not a great programmer/scripter plus i don't know the PHP language but here it is anyway

:

  1. <?
  2. $c = $_POST['col'];
  3. $r = $_POST['rw'];
  4. ?>
  5.  
  6. <?
  7. for($cvchk = 1; $cvchk <= $c; $cvchk ++)
  8. {
  9.   $x = $cvchk;
  10.   for($rchk = 1; $rchk <= $r; $rchk ++)
  11.   { 
  12.       if ($x > 4)
  13.       {
  14.         $x = 1
  15.       }
  16.       echo $x." ";
  17.       $x += 1;
  18.   }
  19.   echo "<br />";
  20. }
  21. ?>


As you can see i also check to see if x goes above 4 if so it returns it to 1, this ensure that it doesn't produce this outcome

1234
2345
3456
4567

Also this is unchecked but to make this work for any number of rows, so the highest number = the number of ros simple change this

:

  1. if ($x > 4)
  2. {
  3.     $x = 1
  4. }
  5. .....
  6. if ($x > $r)
  7. {
  8.       $x = 1
  9. }



Hope this helped if you need anything else just ask.

grimpirate Mar 28th, 2008 12:07 PM

Re: help for (for)
 
This is a bit more memory intensive and slower as well, but I like how it works:
:

  1. <?php
  2. // Get matrix size
  3. $rowSize = $_POST['rw'];
  4. $colSize = $_POST['col'];
  5. ?>
  6. <?php
  7. // Construct matrix
  8. $shiftable = array();
  9. for($i = 0; $i < $rowSize; $i++){
  10.         $temp = array();
  11.         for($j = 0; $j < $colSize; $j++){
  12.                 array_push($temp, $j);
  13.         }
  14.         array_push($shiftable, $temp);
  15.         shiftRow($shiftable, $i, $i);        // Shift matrix accordingly
  16. }
  17. ?>
  18. <pre>
  19. <?php
  20. // Print matrix
  21. foreach($shiftable as $row){
  22.         foreach($row as $j => $value){
  23.                 echo str_pad($value, 2, ' ', STR_PAD_LEFT);
  24.         }
  25.         echo "\n";
  26. }
  27. ?>
  28. </pre>
  29. <?php
  30. // Row shifting function
  31. function shiftRow(&$dArray, $shiftRow, $shiftAmount){
  32.         for($i = 0; $i < $shiftAmount; $i++){
  33.                 array_push($dArray[$shiftRow], array_shift($dArray[$shiftRow]));
  34.         }
  35. }
  36. ?>



All times are GMT -5. The time now is 5:01 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC