Jump to content

How can I get $_POST to squentially number from $counter?


PHP_Idiot

Recommended Posts

I have a FOR Loop that sequentially names form fields based on a user input in the PlayerNo field.

 

The below code snippets should give you the idea.

 

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

<input name=\"Position$counter\"
<input name=\"Points$counter\"
<input name=\"MembershipNo$counter\"

 

This works perfectly, but the problem is when I try to get the $_POST data for each field

How can I get all the data and have the following also sequentially numbered?

  if ($_POST['MembershipNo1'] == ' '){
  $MembershipNo = '<span style="color:red;">MembershipNo omitted.</span>';
}else{
  $MembershipNo = $_POST['MembershipNo1'];
}

 

I have tried a few things but I just keep getting errors!

Anyone got any ideas?

 

Thanks a lot

Link to comment
Share on other sites

and why are u comparing against a space?

if (empty(trim($_POST['MembershipNo1']))){

 

but these are dynamic fields, why not have them in a loop instead as yer form does?

for ( $counter = 1; isset($_POST[($mc='Membership'.$counter)]); $counter += 1)
if (empty(trim($_POST[$mc]))){

 

we want to check for an empty response, trim just removes any whitespaces :)

Link to comment
Share on other sites

<?php
for ($i = 1; $i <= $PlayerNo; $i++) {
   if ($_POST["MembershipNo$i"] == ' ') {
      ${"MembershipNo$i"} = "<span style=\"color:red;\">MembershipNo$i omitted.</span>";
   } else {
      ${"MembershipNo$i"} = $_POST["MembershipNo$i"];
   }
}
?>

 

Will create variables $MembershipNo1, $MembershipNo2 etc. If that's not what you want, just tell me.

Link to comment
Share on other sites

Thank you both for your suggestions.

 

Laffin, When I tried your code I got a fata error on the If line, I probably used it in the wrong way!

Sorry i couldn't get it to work!

 

thebadlad

your code runs without error, and you correct that I am trying to create a variable for each field name as I need to load the contents of these variables into my database.

However, I couldn't echo out the contents of any of the variables!

Bascially on the form that is created (MembershipNo1, MembershipNo2 etc) I want to create variables of the same name that stores the contents of those fields.

 

I suspect the code you have given does this, but I don't seem to be able to 'see' the contents to check it is storing what I need.

 

I confess to being a total noob this is my first ever dip into php so please be patient with me, I appreciate the help enourmously!

Cheers

Link to comment
Share on other sites

Just a side note, the easier way of doing this is using an array for $PlayerNo

 

this can be achieved by using brackets in your form fields

for ( $counter = 1; $counter <= $PlayerNo; $counter += 1)
echo "
<input name=\"Position[$counter]\">
<input name=\"Points[$counter]\">
<input name=\"MembershipNo[$counter]\">
";
}

 

Now since the variables are in an array

you can use foreach or count :)

 

Link to comment
Share on other sites

Woohoo.........you guys rock!!  ;D

 

Thebadbad - correctly assumed I hadn't defined $PlayerNo (it should actually have been $Players)

 

laffin now got your original code running  and the below:

for ($i = 1; $i <= $Players; $i++) {
   if ($_POST["MembershipNo$i"] == '') {
      ${"MembershipNo$i"} = "<span style=\"color:red;\">MembershipNo$i omitted.</span>";
   } else {
      ${"MembershipNo$i"} = $_POST["MembershipNo$i"];
   }
   echo $i;
   echo "<br>";
   echo ${"MembershipNo$i"};
   echo "<br>";
}

gives me the output I was looking for  :D

 

However... I really like the idea of using the array you posted your last comment, but couldn't get it working! I presume this would do away with having to repeat the above code for each field I wanted to place into variables!

 

Link to comment
Share on other sites

Correct :)

 

take a simple form like

for($i=1;$i<10;$i++)
{
   $val=$i*10;
   echo "<input type=\"hidden\" name=\"box[{$i}]" value=\"{$val}\"";
}
echo "<input type=\"hidden\" name=\"box[{$i}]" value=\"\"";

 

very simple form, for readability.

okay in processing u can now treat $box as an array

<?php

$boxes=$_POST['box']);
$count=count($boxes);
echo "Found {$boxes} boxes<BR />";
$removed=0;
foreach($boxes as $idx=>$box)
{
   if(empty($box))
   {
     unset($boxes[$idx]);
     $removed++;
   }
}
echo "Removed {$removed} empty boxes<br />";

 

Just to give u an idea :)

 

Link to comment
Share on other sites

:o

One day I'll make it a mission to understand all this!! But for now I'm happy just to have it working!! I really appreciate you taking the time to explain it, and I kinda get it, but I need to play with it some more to fully understand.

 

Can I ask you one more question though please...

  if ($_POST["Venue$i"] == '') {
      ${"Venue$i"} = "<span style=\"color:red;\">Venue$i omitted.</span>";
   } else {
      ${"Venue$i"} = ereg_replace("[A-Za-z)(.-]","",$_POST["Venue$i"];
   } 

This code doesn't work, if I remove the -- ereg_replace("[A-Za-z)(.-]","", -- bit it does.

Where am I going wrong?

The reason I'm doing this is that the VenueName stores the ID as well, this is the format:

Venue Name(12)

 

I want to strip off the text and brackets and spaces -'s and anything else and just be left with the number. This is the VenueID in myDB Table and is the primary key.

 

Any ideas? (Promise this is the last question..probably...for now at least  :-[)

Link to comment
Share on other sites

Oh ok, never heard of preg_match before!

 

I tried to use it like this:

if ($_POST["Venue$i"] == '') {
      ${"Venue$i"} = "<span style=\"color:red;\">Venue$i omitted.</span>";
   } else {
      ${"Venue$i"} = preg_match('@.*?\((\d*)\)@',$_POST["Venue{$i}"],$match);
  ${"Venue$i"}=$match[1];
  
   } 

but got no results.

 

Then went and read up on it on the manual website, but I can't see how this will work in place of ereg-replace!

Link to comment
Share on other sites

Try this then:

 

if ($_POST["Venue$i"] == '') {
      ${"Venue$i"} = "<span style=\"color:red;\">Venue$i omitted.</span>";
   } else {
      preg_match('@.*?\((\d*)\)@', $_POST["Venue$i"], $match);
     ${"Venue$i"}=$match[1];
    
   } 

 

Or you can use preg_replace() there's very similar to ereg_replace():

 

if ($_POST["Venue$i"] == '') {
      ${"Venue$i"} = "<span style=\"color:red;\">Venue$i omitted.</span>";
   } else {
      ${"Venue$i"} = preg_replace('~[^\d]~', '', $_POST["Venue$i"]);
   } 

 

The simple RegEx [^\d] matches any character NOT a digit, and replaces those matches with nothing (second parameter ''). So if the venue names don't contain any digits, that should leave you with the ID.

 

Basically the preg_ functions are recommended over the ereg_ functions because they are faster.

Link to comment
Share on other sites

Thanks so much for explaining in more detail, I tried it with the preg_replace (just because it makes more sense to my simple brain!)

But when I ran the following at the end to check the output, it still had the Venue name as well as the ID!

foreach($_POST as $field => $value)
{
	echo "$field, $value<br>";
}

 

Unfortunately my server seems to have gone to bed, as I have no connection at the moment so can't test anything else out, I think I'll give up for tonight and try again in the morning.

 

Thanks so much for all the help I really appreciate it, I'll drop a note in tomorrow if I manage to get it going :)

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.