user1681975
user1681975

Reputation: 1

Variable variables syntax and for loop issues

I'm using variables as keys in an array. I understand that won't work with the superglobals. The following code snippet will run the correct number of times, but only picks up data on the first iteration. Do I have a variable variable syntax problem, or a loop problem? Can anyone clarify this for a newbie?

$php_postData = $_POST; 

$php_totalPanels = $_SESSION['totalPanels']; 

for ($php_count=1;$php_count<$php_totalPanels; $php_count++){
echo "<div class='individualQuote'>";

$php_currentItemWidth = 'width_' . $php_count; 
$php_currentItemHeight = 'height_' . $php_count;        
$php_currentItemDescription = 'description_' . $php_count;
$php_currentItemPartNumber = 'partNumber_' . $php_count;
$php_currentItemLexan = 'lexan_' . $php_count;
$php_currentItemVinyl = 'vinyl_' . $php_count;
$php_currentItemPolyester = 'polyester_' . $php_count;

if ( isset($php_postData[$php_currentItemDescription]) ){
echo "<p><span class='em'>Label Name: </span>" . $php_postData[$php_currentItemDescription] . "</p>";   
}

if ( isset($php_postData[$php_currentItemPartNumber]) ){
echo "<p><span class='em'>Part Number: </span>" . $php_postData[$php_currentItemPartNumber] . "</p>";   
}


if ( isset($php_postData[$php_currentItemLexan]) && $php_postData[$php_currentItemLexan] != '0' ) {
echo "<p><span class='em'>Material: </span>" . $php_postData[$php_currentItemLexan] . "</p>";
} 

if ( isset($php_postData[$php_currentItemVinyl]) && $php_postData[$php_currentItemVinyl] != '0'){
echo "<p><span class='em'>Material: </span>" . $php_postData[$php_currentItemVinyl] . "</p>";
} 

if ( isset($php_postData[$php_currentItemPolyester]) && $php_postData[$php_currentItemPolyester] != '0'){
echo "<p><span class='em'>Material: </span>" . $php_postData[$php_currentItemPolyester] . "</p>";
}

if ( isset($php_postData[$php_currentItemWidth]) && isset($php_postData[$php_currentItemHeight]) ){
echo "<p><span class='em'>Size: </span>" . $php_postData[$php_currentItemWidth] . " x " . $php_postData[$php_currentItemHeight] . "</p>";
 }  

echo "</div>";
}   

Upvotes: 0

Views: 45

Answers (1)

benfes
benfes

Reputation: 215

Could you possibly give an example of the data you are posting in?

Looking at the code your loop starts at 1 and continues while it is lower than the totalPanels value. If you are only posting in an array that is has a 2 sets of data then only one will get printed out. Perhaps your loop should run until the count is lower-than-or-equal-to the totalPanels E.g:

for ($php_count=1;$php_count<=$php_totalPanels; $php_count++){...}

Or perhaps you mean to have the loop count start at zero E.g.:

for ($php_count=0;$php_count<$php_totalPanels; $php_count++){...}

I think either of these should make your code run on all values being posted in.

Upvotes: 1

Related Questions