Jump to content

Recommended Posts

I have a script im building and I am explorering the loop option for the first time. now I have 3 differnt fields that will need to loop such as $a1 -$b1 -$c1 that I want to loop to $a2 $b2 $c2 and so on down the rows. Now I can do the

 

for ( $counter = 1; $counter <= 40; $counter += 1)

 

to get the numbers from 1-40 or what ever I set there but how to I get the echo to add $a to the 1,2,3.....?

 

I though I could do

 

echo "<input type=\"text\" value='$a$counter' name=\"a$counter\" size=\"10\">";

 

but the $a is not a valid optoin so it skips over it. So how do I get those to wright as $a1 and pull the value for $a1 and keep looping?

Link to comment
https://forums.phpfreaks.com/topic/99073-loop-help/
Share on other sites

Cause ive never used arrays before Im still learning here...lol but Im not sure how that dose the job... here is the full coding I have now with your [] in there and its erroring...

 

<?php

include ("inc/config.php");

include ("inc/data_djnames.php");

 

print_r($_POST['djnamefc']);

 

echo "<table border=\"2\" align=\"center\" width=\"383\" bordercolor=\"#000000\" >";

echo "<tr><th>DJ Name</th>";

echo "<th>DJ#</th>";

echo "<th>DJ Color</th></tr>";

for ( $counter = 1; $counter <= 40; $counter += 1) {

echo "<tr><td>";

echo $counter;

echo "</td><td width=78>";

    echo "<font class=\"djname2\">DJ #$counter</font>";

echo "</td><td width=78 >";

echo "<input type=\"text\" value='$djnamefc[]' name=\"djnamefc$counter\" size=\"10\">";

echo "</td></tr>";

}

echo "</table>";

 

?>

 

and im getting

Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/hookah17/public_html/timesheet/admin/test.php on line 17

Link to comment
https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506953
Share on other sites

your need to escape the brackets (square brackets)

 

try this (untested)

<?php
include ("inc/config.php");
include ("inc/data_djnames.php");

print_r($_POST['djnamefc']);

echo "<table border=\"2\" align=\"center\" width=\"383\" bordercolor=\"#000000\" >";
echo "<tr><th>DJ Name</th>";
echo "<th>DJ#</th>";
echo "<th>DJ Color</th></tr>";
for ( $counter = 1; $counter <= 40; $counter += 1) {
   echo "<tr><td>";
   echo $counter;
   echo "</td><td width=78>";
    echo "<font class=\"djname2\">DJ #$counter</font>";
   echo "</td><td width=78 >";
   echo '<input type="text" value="'.$_POST['djnamefc'][$counter].'" name="djnamefc['.$counter.']" size="10">';
   echo "</td></tr>";
}
echo "</table>";

?>

Link to comment
https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506963
Share on other sites

do you mean

<?php
$arr = array('fname','lname','email');

for ($row=1; $row<=9; $row++)
{
    for ($col=0; $col < 3; $col++)
    {
        echo "<input type='text' name='{$arr[$col]}[$row]' value='{$arr[$col]}$row'> \n";
    }
    echo "\n<br/>\n";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506968
Share on other sites

ok if I go back and make all my values to $djnamefc[1] then I can do

 

   echo "<input type=text value='$djnamefc[$counter]' name='djnamefc$counter' size=10>";

 

And its pulls up the info. is this the only way? cause if so I hve to go back and change all the coding for every one of those var's

Link to comment
https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506983
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.