Jump to content

how to use for... loop in array


boon_chong

Recommended Posts

I got the coding as below, I wana make the auto numbering for each record, how to do that?

<?
include("8_storeAdminMenu5.php");

$outlet = $_POST['outlet'];
$month = $_POST['month'];
 
include("8_dbconnect.php");
print("$month"); 
print("<br><b>Oulet Code: </b>");
print("$outlet");       
include("monthlycolorsize.php");
print("<table height=\"0\" width=\"970\" border=\"1\"

cellpadding=\"0\">\n");

print("<tr><td><H4>No</H4></td>");

print("<td width=\"180\"><H4>Item No</H4></td>");

print("<td width=\"180\"><H4>Item Description</H4></td>");

print("<td width=\"90\"><H4>Product Group</H4></td>");

print("<td width=\"90\"><H4>Sales Type</H4></td>");

print("<td width=\"90\"><H4>Color Code</H4></td>");

print("<td width=\"90\"><H4>Color Description</H4></td>");

print("<td width=\"90\"><H4>Size Code</H4></td>");

print("<td width=\"90\"><H4>Size Description</H4></td>");

print("<td width=\"90\"><H4>Original Price(RM)</H4></td>");

print("<td><H4>Discount(%)</H4></td>");

print("<td width=\"110\"><H4>Net Price(RM)</H4></td>");

print("<td><H4>Quantity</H4></td>");

print("<td width=\"60\"><H4>Total(RM)</H4></td>");



$count = "select employee.itemNo from employee where

employee.date='$date' and employee.outlet='$outlet'";

$count2 = strlen($count);



$query = "select * from employee where month(date)='$month' and

employee.outlet='$outlet'";
   
$mysql_result = mysql_query($query,$mysql_link);

while($row = mysql_fetch_array($mysql_result))
  {
  print("<br>");
 
  print("<form action=\"8_storeAdminMenu.php\">");
  print("<table width=\"970\" border=\"0\" cellpadding=\"3\">\n");

[color=red]for ($i=1;$i <= $mysql_result;$i++) [/color] {
  printf("<tr><td width=\"15\">%s</td>",$i);
}

printf("<td width=\"70\">%s</td>", $row[1]);

printf("<td width=\"90\">%s</td>", $row[2]);

printf("<td width=\"50\">%s</td>",$row[3]);

printf("<td width=\"45\">%s</td>",$row[4]);

$a= substr("$row[1]",-2,-1);


printf("<td width=\"45\">%s</td>",$a);

if ($a == '0')
{
printf("<td width=\"45\">%s</td>",NA);
}
else
{
printf("<td width=\"45\">%s</td>",$a);
}

$b= substr("$row[1]",-1);
printf("<td width=\"45\">%s</td>",$b);

if ($b == '0'){
printf("<td width=\"45\">%s</td>",NA);
}
elseif ($b == '1') {
printf("<td width=\"45\">%s</td>",S);
}
elseif ($b == '2') {
printf("<td width=\"45\">%s</td>",M);
}
elseif ($b == '3') {
printf("<td width=\"45\">%s</td>",L);
}
elseif ($b == '4') {
printf("<td width=\"45\">%s</td>",XL);
}
elseif ($b == '5') {
printf("<td width=\"45\">%s</td>",XXL);
}
elseif ($b == '6') {
printf("<td width=\"45\">%s</td>",XXXL);
}
elseif ($b == '7') {
printf("<td width=\"45\">%s</td>",S/M);
}
elseif ($b == '8') {
printf("<td width=\"45\">%s</td>",L/XL);
}
elseif ($b == '9') {
printf("<td width=\"45\">%s</td>",XS);
}
elseif ($b == 'A') {
printf("<td width=\"45\">%s</td>",26);
}
elseif ($b == 'B') {
printf("<td width=\"45\">%s</td>",27);
}
elseif ($b == 'C') {
printf("<td width=\"45\">%s</td>",28);
}
elseif ($b == 'D') {
printf("<td width=\"45\">%s</td>",29);
}
elseif ($b == 'E') {
printf("<td width=\"45\">%s</td>",30);
}
elseif ($b == 'F') {
printf("<td width=\"45\">%s</td>",31);
}
elseif ($b == 'G') {
printf("<td width=\"45\">%s</td>",32);
}
elseif ($b == 'H') {
printf("<td width=\"45\">%s</td>",33);
}
elseif ($b == 'I') {
printf("<td width=\"45\">%s</td>",34);
}
elseif ($b == 'J') {
printf("<td width=\"45\">%s</td>",35);
}
elseif ($b == 'K') {
printf("<td width=\"45\">%s</td>",36);
}
elseif ($b == 'L') {
printf("<td width=\"45\">%s</td>",37);
}
elseif ($b == 'M') {
printf("<td width=\"45\">%s</td>",38);
}
elseif ($b == 'N') {
printf("<td width=\"45\">%s</td>",39);
}
elseif ($b == 'O') {
printf("<td width=\"45\">%s</td>",40);
}
elseif ($b == 'P') {
printf("<td width=\"45\">%s</td>",41);
}
elseif ($b == 'Q') {
printf("<td width=\"45\">%s</td>",42);
}
elseif ($b == 'R') {
printf("<td width=\"45\">%s</td>",43);
}
elseif ($b == 'S') {
printf("<td width=\"45\">%s</td>",44);
}
elseif ($b == 'T') {
printf("<td width=\"45\">%s</td>",45);
}
elseif ($b == 'U') {
printf("<td width=\"45\">%s</td>",46);
}
elseif ($b == 'V') {
printf("<td width=\"45\">%s</td>",47);
}
elseif ($b == 'W') {
printf("<td width=\"45\">%s</td>",48);
}
elseif ($b == 'X') {
printf("<td width=\"45\">%s</td>",XXXXL);
}
elseif ($b == 'Y') {
printf("<td width=\"45\">%s</td>",F);
}
else {
printf("<td width=\"45\">%s</td>",NA);
}

printf("<td width=\"50\" align=right>%s</td>",$row[5]);

printf("<td width=\"40\" align=right>%s</td>",$row[6]);

printf("<td width=\"43\" align=right>%s</td>", $row[7]);

printf("<td width=\"35\" align=right>%s</td>",$row[8]);

printf("<td width=\"35\" align=right>%s</td>",$row[9]);   


printf("</h6>");

print("</table>");
 
      }


$result = mysql_query("SELECT sum(total) FROM employee where

month(date)='$month' and outlet='$outlet'");
echo "<table border=0 width=960 cellpadding=0>";echo "<tr>"; echo "<td

align=right>";
print("<br>");
echo "<b>Grand Total (RM): ";
echo mysql_result($result,0);
echo "<td>"; echo "<tr>"; echo "<table>";

      include("8_dbdisconnect.php");
?>
Link to comment
https://forums.phpfreaks.com/topic/29060-how-to-use-for-loop-in-array/
Share on other sites

To number each row, use

[code=php:0]$row = 0;
while ($row = mysql_fetch_array($mysql_result)) {
  print "Row number $row\n";
  $row++
}[/code]


About that big "if", you can do like this:

[code=php:0]$table = array(
  '0' => 'NA',
  '1' => 'S',
  ... # and so on for each possibility
  'Y' => 'F',
);
echo $table[$b];[/code]


That is more flexible and allows for easier editing.
First, use <?php instead of <?

Second, $mysql_result = mysql_query($query,$mysql_link); is going to give $mysql_result basically a value of "Resource id #x", which when you put it into "for ($i=1;$i <= $mysql_result;$i++)", the int form of a string is always zero, so your for loop is not going to run.  You will want to change $mysql_result to @mysql_numrows($mysql_result) and it should be good.
After i change the coding like below, the result is like that:

------------------------------------------------------------------------
NO ItemNO
1
2  ABC123

1
2  XYZ123
------------------------------------------------------------------------

<?php
include("8_storeAdminMenu5.php");

$outlet = $_POST['outlet'];
$month = $_POST['month'];
 
include("8_dbconnect.php");
print("$month"); 
print("<br><b>Oulet Code: </b>");
print("$outlet");       
include("monthlycolorsize.php");
print("<table height=\"0\" width=\"970\" border=\"1\"

cellpadding=\"0\">\n");

print("<tr><td><H4>No</H4></td>");

print("<td width=\"180\"><H4>Item No</H4></td>");

print("<td width=\"180\"><H4>Item Description</H4></td>");

print("<td width=\"90\"><H4>Product Group</H4></td>");

print("<td width=\"90\"><H4>Sales Type</H4></td>");

print("<td width=\"90\"><H4>Color Code</H4></td>");

print("<td width=\"90\"><H4>Color Description</H4></td>");

print("<td width=\"90\"><H4>Size Code</H4></td>");

print("<td width=\"90\"><H4>Size Description</H4></td>");

print("<td width=\"90\"><H4>Original Price(RM)</H4></td>");

print("<td><H4>Discount(%)</H4></td>");

print("<td width=\"110\"><H4>Net Price(RM)</H4></td>");

print("<td><H4>Quantity</H4></td>");

print("<td width=\"60\"><H4>Total(RM)</H4></td>");

$query = "select * from employee where month(date)='$month' and

employee.outlet='$outlet'";
   
$mysql_result = mysql_query($query,$mysql_link);


while($row = mysql_fetch_array($mysql_result))
  {
  print("<br>");
 
  print("<form action=\"8_storeAdminMenu.php\">");
  print("<table width=\"970\" border=\"0\" cellpadding=\"3\">\n");

for ($i=1; $i<= mysql_numrows($mysql_result);$i++){


printf("<tr><td width=\"15\">%s</td>",$i);

}

printf("<td width=\"70\">%s</td>", $row[1]);

printf("<td width=\"90\">%s</td>", $row[2]);

printf("<td width=\"50\">%s</td>",$row[3]);

printf("<td width=\"45\">%s</td>",$row[4]);

$a= substr("$row[1]",-2,-1);


printf("<td width=\"45\">%s</td>",$a);

if ($a == '0')
{
printf("<td width=\"45\">%s</td>",NA);
}
else
{
printf("<td width=\"45\">%s</td>",$a);
}

$b= substr("$row[1]",-1);
printf("<td width=\"45\">%s</td>",$b);

if ($b == '0'){
printf("<td width=\"45\">%s</td>",NA);
}
elseif ($b == '1') {
printf("<td width=\"45\">%s</td>",S);
}
elseif ($b == '2') {
printf("<td width=\"45\">%s</td>",M);
}
elseif ($b == '3') {
printf("<td width=\"45\">%s</td>",L);
}
elseif ($b == '4') {
printf("<td width=\"45\">%s</td>",XL);
}
elseif ($b == '5') {
printf("<td width=\"45\">%s</td>",XXL);
}
elseif ($b == '6') {
printf("<td width=\"45\">%s</td>",XXXL);
}
elseif ($b == '7') {
printf("<td width=\"45\">%s</td>",S/M);
}
elseif ($b == '8') {
printf("<td width=\"45\">%s</td>",L/XL);
}
elseif ($b == '9') {
printf("<td width=\"45\">%s</td>",XS);
}
elseif ($b == 'A') {
printf("<td width=\"45\">%s</td>",26);
}
elseif ($b == 'B') {
printf("<td width=\"45\">%s</td>",27);
}
elseif ($b == 'C') {
printf("<td width=\"45\">%s</td>",28);
}
elseif ($b == 'D') {
printf("<td width=\"45\">%s</td>",29);
}
elseif ($b == 'E') {
printf("<td width=\"45\">%s</td>",30);
}
elseif ($b == 'F') {
printf("<td width=\"45\">%s</td>",31);
}
elseif ($b == 'G') {
printf("<td width=\"45\">%s</td>",32);
}
elseif ($b == 'H') {
printf("<td width=\"45\">%s</td>",33);
}
elseif ($b == 'I') {
printf("<td width=\"45\">%s</td>",34);
}
elseif ($b == 'J') {
printf("<td width=\"45\">%s</td>",35);
}
elseif ($b == 'K') {
printf("<td width=\"45\">%s</td>",36);
}
elseif ($b == 'L') {
printf("<td width=\"45\">%s</td>",37);
}
elseif ($b == 'M') {
printf("<td width=\"45\">%s</td>",38);
}
elseif ($b == 'N') {
printf("<td width=\"45\">%s</td>",39);
}
elseif ($b == 'O') {
printf("<td width=\"45\">%s</td>",40);
}
elseif ($b == 'P') {
printf("<td width=\"45\">%s</td>",41);
}
elseif ($b == 'Q') {
printf("<td width=\"45\">%s</td>",42);
}
elseif ($b == 'R') {
printf("<td width=\"45\">%s</td>",43);
}
elseif ($b == 'S') {
printf("<td width=\"45\">%s</td>",44);
}
elseif ($b == 'T') {
printf("<td width=\"45\">%s</td>",45);
}
elseif ($b == 'U') {
printf("<td width=\"45\">%s</td>",46);
}
elseif ($b == 'V') {
printf("<td width=\"45\">%s</td>",47);
}
elseif ($b == 'W') {
printf("<td width=\"45\">%s</td>",48);
}
elseif ($b == 'X') {
printf("<td width=\"45\">%s</td>",XXXXL);
}
elseif ($b == 'Y') {
printf("<td width=\"45\">%s</td>",F);
}
else {
printf("<td width=\"45\">%s</td>",NA);
}



printf("<td width=\"50\" align=right>%s</td>",$row[5]);

printf("<td width=\"40\" align=right>%s</td>",$row[6]);

printf("<td width=\"43\" align=right>%s</td>", $row[7]);

printf("<td width=\"35\" align=right>%s</td>",$row[8]);

printf("<td width=\"35\" align=right>%s</td>",$row[9]);   


printf("</h6>");

print("</table>");
 
      }


$result = mysql_query("SELECT sum(total) FROM employee where

month(date)='$month' and outlet='$outlet'");
echo "<table border=0 width=960 cellpadding=0>";echo "<tr>"; echo "<td

align=right>";
print("<br>");
echo "<b>Grand Total (RM): ";
echo mysql_result($result,0);
echo "<td>"; echo "<tr>"; echo "<table>";

      include("8_dbdisconnect.php");
?>

why?
[code]
<?php
$query = "select * from employee where month(date)='$month' and
employee.outlet='$outlet'";
   
$mysql_result = mysql_query($query,$mysql_link);

while($row = mysql_fetch_array($mysql_result))
  {
  print("<br>");
  print("<form action=\"8_storeAdminMenu.php\">");
  print("<table width=\"970\" border=\"0\" cellpadding=\"3\">\n");

for ($i=1; $i<= mysql_numrows($mysql_result);$i++)
{
printf("<tr><td width=\"15\">%s</td>",$i);
}
printf("<td width=\"70\">%s</td>", $row[1]);

printf("<td width=\"90\">%s</td>", $row[2]);

printf("<td width=\"50\">%s</td>",$row[3]);
print("</table>");
      }
?>
[/code]
[quote author=boon_chong link=topic=116933.msg476807#msg476807 date=1164954133]
[code]
<?php
$query = "select * from employee where month(date)='$month' and
employee.outlet='$outlet'";
   
$mysql_result = mysql_query($query,$mysql_link);

while($row = mysql_fetch_array($mysql_result))
  {
  print("<br>");
  print("<form action=\"8_storeAdminMenu.php\">");
  print("<table width=\"970\" border=\"0\" cellpadding=\"3\">\n");

for ($i=1; $i<= mysql_numrows($mysql_result);$i++)
{
printf("<tr><td width=\"15\">%s</td>",$i);
}
printf("<td width=\"70\">%s</td>", $row[1]);

printf("<td width=\"90\">%s</td>", $row[2]);

printf("<td width=\"50\">%s</td>",$row[3]);
print("</table>");
      }
?>
[/code]
[/quote]
This code seems fairly redundant.  You're running a while loop to to to go through all of the results and store it in $row, and then you're basically going through the loop again with a for loop but offsetting it by 1 ($i=1 start means it's ignoring the 1st index)??  Add $i=0; before the while loop, and at the bottom of the while loop put $i++; and then you can just take everything in that for loop and just put it in the main body of the while loop.  Also try to avoid using SELECT * and instead of referring to it as $row[2], use like $row['name'].

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.