Jump to content


Photo

Displaying the Array using for()


  • Please log in to reply
7 replies to this topic

#1 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 15 April 2006 - 02:46 PM

Hi. If I could just draw your attention to this snippet of 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>

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.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 15 April 2006 - 03:09 PM

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

Ken

#3 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 15 April 2006 - 03:28 PM

That's awesome, thanks.

#4 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 15 April 2006 - 04:13 PM

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:

<?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 { ?>

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.

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 15 April 2006 - 04:39 PM

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

There are two ways of inserting them all together.
  • Insert them as CSV (comma seperated values) in a string:
    <?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';
    ?>
  • Insert a serialized array containing the values:
    <?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';
    ?>
Ken

#6 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 15 April 2006 - 05:41 PM

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

<?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 />'); 
} ?>

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.

#7 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 16 April 2006 - 11:22 AM

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.

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 April 2006 - 03:14 PM

Remove the foreach(). It is not necessary.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users