Jump to content

Displaying the Array using for()


CMellor

Recommended Posts

Hi. If I could just draw your attention to this snippet of code:

[code]<tr>
<td class="preres" style="background: #CCC">
<fieldset><legend>Who's in your stable?</legend>
<form action="" method="post">
Select a name for your stable.<br />
<input type="text" name="team_name" /><br />
Your selections must be on the same brand show as you.<br />
<?php for($i = 0; $i < $_POST['number']; $i++) { echo('<input type="text" name="stable_mate[$i]" /><br />'); } ?>
<input type="submit" name="submit_stable" value="Request"/>
</form>
</fieldset>
</td>
</tr>
<?php } } elseif($_POST['submit_stable']) { ?>
<tr>
<td class="preres" style="background: #CCC"><span class="tag_error"><?=$_POST['stable_mate']?></span></td>
</tr>[/code]

Before this part of the code works, you select a number between 3 and 5 and depending on the number chosen, that's how many input boxes are displayed. I used the for() function to do that. To do this though, I did find it from a PHP book I own, that's why in the input's name, it says [$i] after the name.

What I can't figure out is how to display the text I input on the x input boxes, so if I selected 3 input boxes, filled them in, and clicked Submit, I'd want it to display those 3 input values.

If anyone knows how I can do this, I'd be grateful. Thanks for your time. I look forward to your responces.

Chris.
Link to comment
Share on other sites

First, change this:
[code]<?php
for($i = 0; $i < $_POST['number']; $i++) { echo('<input type="text" name="stable_mate[$i]" /><br />'); }
?>[/code]
to:
[code]<?php
for($i = 0; $i < $_POST['number']; $i++)
    echo '<input type="text" name="stable_mate[' . $i . ']" /><br />';
?>[/code]
Then in your processing piece:
[code]<?php
foreach ($_POST['stable_mate'] as $val) if (trim(stripslashes($val)) != '')
      echo htmlentities(trim(stripslashes($val))) . '<br />';
?>[/code]

Ken
Link to comment
Share on other sites

Follwing on from this...

I want to add the values of the inputs into the database, but when I execute it, it add's each input value as a row, and doesn't list them in the same row. Here's the code:

[code]<?php } } elseif($_POST['submit_stable']) { ?>
<tr>
<td class="preres" style="background: #CCC">
<p>Thanks. Your chosen stable name is <strong><?=$_POST['team_name']?></strong>.</p>
<p>And you have chosen:</p>
<?php foreach($_POST['stable_mate'] as $members) {
mysql_query("INSERT INTO $dbname.request_stable(leader, members, group_name)
VALUES('".$_SESSION['name']."', '$members', '".$_POST['team_name']."')");
echo('- ' . $members . '<br />');
} ?>
<br />to be in your stable.
</td>
</tr>
<?php } else { ?>[/code]

I thought it would of listed the values in the same row, but no, it lists each value as a row. Can anyone tell me what I might be doing wrong?

Thanks.
Link to comment
Share on other sites

That's because you're doing one insert for each value.

There are two ways of inserting them all together.
[list][*]Insert them as CSV (comma seperated values) in a string:
[code]<?php
$members = implode(', ',$_POST['stable_mates'];
mysql_query("INSERT INTO $dbname.request_stable(leader, members, group_name)
VALUES('".$_SESSION['name']."', '$members', '".$_POST['team_name']."')");
echo 'You have chosen ' . $members . ' to be in your stable';
?>[/code][*]Insert a serialized array containing the values:
[code]<?php
$members = $_POST['stable_mates'];
$tmp = serialize($members);
mysql_query("INSERT INTO $dbname.request_stable(leader, members, group_name)
VALUES('".$_SESSION['name']."', '$members', '".$_POST['team_name']."')");
echo 'You have chosen ' . implode(', '.$members) . ' to be in your stable';
?>[/code][/list]
Ken
Link to comment
Share on other sites

Is any of the code you provided their a bit wrong, 'cause it looks a bit wrong, mostly just the implode() function, I'm getting Bad Argument errors when doing it with the code above.

Do I add the implode() function inbetween the foreach() function? like so...

[code]<?php foreach($_POST['stable_mate'] as $members) {
/* INSERT IT IN HERE ??? */
mysql_query("INSERT INTO $dbname.request_stable(leader, members, group_name)
VALUES('".$_SESSION['name']."', '$members', '".$_POST['team_name']."')");
echo('- ' . $members . '<br />');
} ?>[/code]

Sorry, just don't seem to understand it, maybe I need to take a break, lol.

Thanks for your help though, I really appriciate it.
Link to comment
Share on other sites

Hey, sorry to bump this, but I really can't figure it out, even with the example code, am just getting errors. It's probably gonna be something really simple, and i'll end up kicking myself for it, lol, usually the case.
Link to comment
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.