0

Total beginner with PHP:

I have an form based on table data that the user can add any number of rows too, currently I'm calling the data in my .php like:

$para01 = $_POST["1_0"];
$url01 = $_POST["1_1"];
$para02 = $_POST["2_0"];
$url02 = $_POST["2_1"];

etc.

I'd like a way call the table data, so I can cater for x amount of rows

So, my pseudo code would be:

  • for each row, apply cell id 0 to $url and id 1 to $para
  • then echo url and para in some html
  • repeat until all rows have been expressed.
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

2

You should make the url and the para a (two) dimensional array. Then, loop through the _POST[] variable, with a two dimensional for loop. Add the values to the array, and print them if necessary.

Hidde
  • 11,493
  • 8
  • 43
  • 68
0

You should use arrays! a simple way to do it would be to call all post variables, and then sort them with php...

I did it for you really quick, assuming the form input fields look like:

formstart url1 para1 url2 para2 url3 para3 and so on...

submit endform

$i=1;
$urls = array();
$paras = array();
foreach($_POST as $variable){
    if($i==1){
        array_push($urls,$variable);
        $i++;
    }else{
        array_push($paras,$variable);
        $i=1;
    }
}
echo'<table><tr><td>';
foreach($urls as $url){
    echo $url.'<br />';
}
echo'</td><td>';
foreach($paras as $para){
    echo $para.'<br />';
}
echo'</td></tr></table>';

Edit

You would pair them like this...

 $_POST = array('url1','paragraph1','url2','paragraph2','url3','paragraph3');
$urls = array();
$paras = array();
$i=1;
$c=0;
foreach($_POST as $variable){
    if($i==1){
        array_push($urls,$variable);
        $i++;
    }else{
        array_push($paras,$variable);
        $i=1;
     $c++;
    }
}

echo'<table>';
for($x=0;$x<$c;$x++){
   echo'<tr><td>'.$urls[$x].'</td><td>'.$paras[$x].'</td></tr>';
}
echo'</table>';
Nickolas Tuttle
  • 208
  • 2
  • 10
  • thanks for this- I have it working, but how would I get the para in the first foreach loop, instead of separate to it? Each indexed url and para needs to be paired in the loop. – Joshua Crowley May 13 '12 at 10:14
  • I editted the above... Now you can pair them with the row, you can even give the td an id... like, – Nickolas Tuttle May 13 '12 at 11:41
  • thanks for the edit- but doesn't seem to work for me? Does the else need a } infront of it? I added it, but I got another parser error on ; line 20? – Joshua Crowley May 13 '12 at 11:57
  • Yes!haha, sorry about that! yes, the else needs a closing bracket before it. – Nickolas Tuttle May 13 '12 at 23:50
  • I really should check my code before i send it, i just wrote it really quick so you could get the idea. let me know if you have any other questions! – Nickolas Tuttle May 13 '12 at 23:51
  • Hey Nickolas, I've just noticed a funny thing- the result of the above paired example seem to loop twice- once with all the variables/arrays and then a second time with empty fields. So three sets of urls and paras produces 6 , 3 with the correct data in order and then 3 empty. Also, any PHP that I echo inside the FOR function is duplicated as well, a basic preg_replace gives me two copies of an reg url so my links read src="hello.jpghello.jpg". Any idea why? Perhaps something to do with the FOR conditions? – Joshua Crowley May 16 '12 at 12:34
  • Images no longer duplicating- that was something I messed up. Other stuff (double loop) still stands. – Joshua Crowley May 16 '12 at 12:55
  • http://stackoverflow.com/questions/2433727/submitting-a-multidimensional-array-via-post-with-php this solves all my problems! – Joshua Crowley May 16 '12 at 14:42
  • Ok, sorry about the confusion, I think the problem there was that I put the $c++ in the worng place. The edit above works, try it as is first, then get rid of the $_POST array at the top so you can utilize your form post instead. – Nickolas Tuttle May 16 '12 at 17:58
0

Ya you can use it as arrays. That is like,

<input type="text" name="para[]" />
<input type="text" name="url[]" />

<input type="text" name="para[]" />
<input type="text" name="url[]" />

<input type="text" name="para[]" />
<input type="text" name="url[]" />

And in php, you can use like

<?php
foreach($_POST[para] as $key=>$val)
{
}

foreach($_POST[url] as $key=>$val)
{
}
?>
Stranger
  • 10,332
  • 18
  • 78
  • 115