shaung Posted December 24, 2013 Share Posted December 24, 2013 Hello, I have these form fields in HEREDOC syntax but I need to add more PHP which isn't possible in heredoc. I wish to remove the heredoc for this section, but I suck at echoing form fields. Could someone help me with this? foreach ($arrValues as $row) { $id = $row['employee_id']; echo <<<FORM_FIELDS <tr> <td><input type="text" name="record[$id][firstName]" value="{$row['first_name']}" readonly /></td> <td><input type="text" name="record[$id][lastName]" value="{$row['last_name']}" readonly /></td> <td><input type="text" name="record[$id][height]" value="{$row['height']}" readonly /></td> <td><input type="text" name="record[$id][cap]" value="{$row['cap']}" readonly /></ td> <td><input type="text" name="record[$id][color]" value="{$row['colors']}" readonly /></td> <td><select name="record[$id][degree]" </td> <option> "Filler" </option> </select> <td><select name="record[$id][school]" </td> <option>{$row['name']}</option> </select> <td><input type="submit" name="update" value="Update" /></td> </tr> FORM_FIELDS; } What I need to do is populate the dropdown option boxes from the DB but I cannot use a loop within the heredoc blocks. Apparently PHP loops are not possible within heredoc. I would prefer to just echo everything out, but with my limited PHP skills, I keep making a mess of it. Can anyone help me? Thanks! Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 24, 2013 Share Posted December 24, 2013 if i remember correctly, you want to pre-select the option for the currently displayed record. seems that is what this previous post did - http://forums.phpfreaks.com/topic/284692-populate-an-arrayed-dropdown-when-clicked/ you would form the option list in a php variable before the start of the heredoc statement and just put the php variable in at the correct point. Quote Link to comment Share on other sites More sharing options...
Solution AJinNYC Posted December 24, 2013 Solution Share Posted December 24, 2013 (edited) One issue I see is it looks like you're missing your closing bracket on your select elements. It's not too hard to echo out. Just use single quotes, around the HTML block, and then you have to "breakout" of the single quotes because variables inside single quotes are treated like strings/plain-text. For example: echo 'Some Text '.$variable.' Some more text'; You can think of the quotes as thread and the periods (or more technically concatenating) as needle holes... you're stitching together the variable, and the text after the variable, to the first bit of text. So using the above method on your code, you end up with this. foreach ($arrValues as $row) { $id = $row['employee_id']; echo '<tr> <td><input type="text" name="record['.$id.'][firstName]" value="'.$row['first_name'].'" readonly /></td> <td><input type="text" name="record['.$id.'][lastName]" value="'.$row['last_name'].'" readonly /></td> <td><input type="text" name="record['.$id.'][height]" value="'.$row['height'].'" readonly /></td> <td><input type="text" name="record['.$id.'][cap]" value="'.$row['cap'].'" readonly /></ td> <td><input type="text" name="record['.$id.'][color]" value="'.$row['colors'].'" readonly /></td> <td><select name="record['.$id.'][degree]"> </td> <option> "Filler" </option> </select> <td><select name="record['.$id.'][school]"> </td> <option>'.$row['name'].'</option> </select> <td><input type="submit" name="update" value="Update" /></td> </tr>'; } If you don't know it, the definition of concatenating is to: link (things) together in a chain or series. Edited December 24, 2013 by AJinNYC Quote Link to comment Share on other sites More sharing options...
shaung Posted December 24, 2013 Author Share Posted December 24, 2013 (edited) One issue I see is it looks like you're missing your closing bracket on your select elements. It's not too hard to echo out. Just use single quotes, around the HTML block, and then you have to "breakout" of the single quotes because variables inside single quotes are treated like strings/plain-text. For example: echo 'Some Text '.$variable.' Some more text'; You can think of the quotes as thread and the periods (or more technically concatenating) as needle holes... you're stitching together the variable, and the text after the variable, to the first bit of text. So using the above method on your code, you end up with this. foreach ($arrValues as $row) { $id = $row['employee_id']; echo '<tr> <td><input type="text" name="record['.$id.'][firstName]" value="'.$row['first_name'].'" readonly /></td> <td><input type="text" name="record['.$id.'][lastName]" value="'.$row['last_name'].'" readonly /></td> <td><input type="text" name="record['.$id.'][height]" value="'.$row['height'].'" readonly /></td> <td><input type="text" name="record['.$id.'][cap]" value="'.$row['cap'].'" readonly /></ td> <td><input type="text" name="record['.$id.'][color]" value="'.$row['colors'].'" readonly /></td> <td><select name="record['.$id.'][degree]"> </td> <option> "Filler" </option> </select> <td><select name="record['.$id.'][school]"> </td> <option>'.$row['name'].'</option> </select> <td><input type="submit" name="update" value="Update" /></td> </tr>'; } If you don't know it, the definition of concatenating is to: link (things) together in a chain or series. OK, I'm starting to see what is going on now. It's just like one long string. So I should be able to flip the 'readonly' to write by putting it in a variable and just setting it to an empty string as needed... thanks a lot. I have programmed for years off and on, just haven't done it in about 3 years and have never done web programming/scripting before. Appreciate everyone's help. Edited December 24, 2013 by shaung Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.