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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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>';

9 Comments

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.
I editted the above... Now you can pair them with the row, you can even give the td an id... like, <tr><td id="left'.$i.'"></td><td id="right'.$i.'"></td></tr>
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?
Yes!haha, sorry about that! yes, the else needs a closing bracket before it.
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!
|
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)
{
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.