Jump to content

Why doesn't this loop work?


Perad

Recommended Posts

[code]for ($i=0;$i<$pn;$i++) {
echo $pn1 . ' - ' . $pn2;
echo '<input type="text" name="player'.($i+1).'" size="30" maxlength="50" value="'.$pn.($i+1).'" />';
echo '<input type="text" name="playerscore'.($i+1).'" size="3" maxlength="3" value="'.$ps.($i+1).'" />';
}[/code]

I'm baffled, the $pn and $ps variables are defined in an external file, all variables are correct and work.

echo $pn1 . ' - ' . $pn2;

generates the correct values yet the form is returning incorrect values. No matter what edit page i flip to it comes up with exactly the same values and variables. Any ideas?
Link to comment
https://forums.phpfreaks.com/topic/28321-why-doesnt-this-loop-work/
Share on other sites

You really should use arrays here. They are much easier to use than trying to generate variables with the names like $x1, $x2, etc.

[code]<?php
for ($i=1;$i<=count($pn);$i++) {
echo '<input type="text" name="player['.$i.']" size="30" maxlength="50" value="'.$pn[$i].'" />';
echo '<input type="text" name="playerscore['.$i.']" size="3" maxlength="3" value="'.$ps[$i].'" />';
}?>[/code]

You will have to modify the rest of your script to use arrays instead of the generated variable names. This may take some time, but it will make your life much easier.

Ken
OK i will have a crack at turning it into an array, i think i know how to do it, could you just help with the first step please.

These are my variables which define where to get the data from.

[code]$ps1 = ($row['score1']);
$ps2 = ($row['score2']);
$ps3 = ($row['score3']);
$ps4 = ($row['score4']);
$ps5 = ($row['score5']);
$ps6 = ($row['score6']);
$ps7 = ($row['score7']);
$ps8 = ($row['score8']);
$ps9 = ($row['score9']);
$ps10 = ($row['score10']);
$ps11 = ($row['score11']);
$ps12 = ($row['score12']);
$ps13 = ($row['score13']);
$ps14 = ($row['score14']);
$ps15 = ($row['score15']);
$ps16 = ($row['score16']);
$ps17 = ($row['score17']);
$ps18 = ($row['score18']);
$ps19 = ($row['score19']);
$ps20 = ($row['score20']);
$ps21 = ($row['score21']);
$ps22 = ($row['score22']);
$ps23 = ($row['score23']);
$ps24 = ($row['score24']);[/code]

Is there a way to put this into an array than

$array = array(($row['score2']), ($row['score2']), etc etc
Where is the $row array coming from?

If you can't change that into a two dimentional array, you can use a for loop such as:
[code]<?php
$ps = array();
for ($i=1;$i<25;$i++)
    $ps[$i] = $row['score' . $i];
?>[/code]

If I were doing this, I would first change the $row array into another array:
[code]<?php
$row2 = array();
$ps = array();
for ($i=1;$i<25;$i++) {
    $row2['score'][$i] = $row['score' . $i];
    $ps[$i] = $row2['score'][$i]
}
?>[/code]

Ken

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.