Jump to content


Photo

Repeating array... Not supposed to


  • Please log in to reply
1 reply to this topic

#1 DBookatay

DBookatay

    Advanced Member

  • Members
  • PipPipPip
  • 333 posts
  • LocationCT

Posted 27 January 2013 - 07:42 PM

For some reason this array keeps repeating the 1st line onto the 2nd line and the 1st and 2nd line onto the 3rd line, and so on and so on.

unset ($grade); $grade = array();
   foreach ($row as $grade => $value) {
	  if (substr($grade, 0,1)== "y") {
	  if ($grade == "y1") {if ($value == "x") {$grades[] = "1";}}
	  if ($grade == "y2") {if ($value == "x") {$grades[] = "2";}}
	  if ($grade == "y3") {if ($value == "x") {$grades[] = "3";}}
	  if ($grade == "y4") {if ($value == "x") {$grades[] = "4";}}
	  if ($grade == "y5") {if ($value == "x") {$grades[] = "5";}}
	  if ($grade == "y6") {if ($value == "x") {$grades[] = "6";}}
	  if ($grade == "y7") {if ($value == "x") {$grades[] = "7";}}
   }
}
$grades_string = implode(", ", $grades);

The dB rows are titled y1, y2 etc.

I can show someone a link with the example of what I mean the error is if need be.

$result mysql_query("SELECT finger FROM hand WHERE id=3"); 
echo 
$result


#2 DBookatay

DBookatay

    Advanced Member

  • Members
  • PipPipPip
  • 333 posts
  • LocationCT

Posted 27 January 2013 - 08:10 PM

For some reason this array keeps repeating the 1st line onto the 2nd line and the 1st and 2nd line onto the 3rd line, and so on and so on.

unset ($grade); $grade = array();
foreach ($row as $grade => $value) {
	 if (substr($grade, 0,1)== "y") {
	 if ($grade == "y1") {if ($value == "x") {$grades[] = "1";}}
	 if ($grade == "y2") {if ($value == "x") {$grades[] = "2";}}
	 if ($grade == "y3") {if ($value == "x") {$grades[] = "3";}}
	 if ($grade == "y4") {if ($value == "x") {$grades[] = "4";}}
	 if ($grade == "y5") {if ($value == "x") {$grades[] = "5";}}
	 if ($grade == "y6") {if ($value == "x") {$grades[] = "6";}}
	 if ($grade == "y7") {if ($value == "x") {$grades[] = "7";}}
}
}
$grades_string = implode(", ", $grades);

The dB rows are titled y1, y2 etc.

I can show someone a link with the example of what I mean the error is if need be.



Rewrote it, problem was it should of been unset ($grade); $grades = array();
unset ($grade); $grades = array();
   foreach ($row as $grade => $value) {
   if (substr($grade, 0,1)== "y" && $value == "x") {
	  if ($grade == "y1") {$grades[] = "1";}
	  if ($grade == "y2") {$grades[] = "2";}
	  if ($grade == "y3") {$grades[] = "3";}
	  if ($grade == "y4") {$grades[] = "4";}
	  if ($grade == "y5") {$grades[] = "5";}
	  if ($grade == "y6") {$grades[] = "6";}
	  if ($grade == "y7") {$grades[] = "7";}
   }
}
$grades_string = implode(", ", $grades);

$result mysql_query("SELECT finger FROM hand WHERE id=3"); 
echo 
$result





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com