Page 1 of 1

looping sql query for form.

Posted: Tue Dec 11, 2007 5:00 pm
by farkewie
Hi i am trying o add a form that creates links on a site. the user can create up to 3 at a time, what i need is so they can fill out 1-3 links and when the form is proccessed it it adds them all to the database here is my code so far..

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

echo <<<FRM
<form id="form1" name="form1" method="get" action="index.php">
  <table>
    <tr>
      <td><label>
        <input type="text" name="text_1" id="text_1" />
      </label></td>
      <td><label>
        <input type="text" name="link_1" id="link_1" />
      </label></td>
      <td><label>
        <input type="text" name="location_1" id="location_1" />
      </label></td>
    </tr>
    <tr>
      <td><label>
        <input type="text" name="text_2" id="text_2" />
      </label></td>
      <td><label>
        <input type="text" name="link_2" id="link_2" />
      </label></td>
      <td><label>
        <input type="text" name="location_2" id="location_2" />
      </label></td>
    </tr>
    <tr>
      <td><label>
        <input type="text" name="text_3" id="text_3" />
      </label></td>
      <td><label>
        <input type="text" name="link_3" id="link_3" />
      </label></td>
      <td><label>
        <input type="text" name="location_3" id="location_3" />
      </label></td>
    </tr>
  </table>
    <label>
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
  </p>
</form>
FRM;

if(isset($_GET['Submit'])){
 for($i = 0; $i >= 3; $i++){
   $inputs[$i]['text'] = (isset($_GET['text_'.$i])) ? $_GET['text_'.$i] : '';
   $inputs[$i]['link'] = (isset($_GET['link_'.$i])) ? $_GET['link_'.$i] : '';
   $inputs[$i]['location'] = (isset($_GET['location_'.$i])) ? $_GET['location_'.$i] : '';
 }




foreach($inputs as $row){
echo 'New link '.$row['text'].' '.$row['link'].' '.$row['location'].'<br />';
}
}
?>

i get an error saying "inputs" is an undefined variable..
EDIT:

i am not doing the sql just yet, once the echo works ill add the sql

Posted: Tue Dec 11, 2007 5:15 pm
by nathanr

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

echo <<<FRM
<form id="form1" name="form1" method="get" action="index.php">
  <table>
    <tr>
      <td><label>
        <input type="text" name="text_1" id="text_1" />
      </label></td>
      <td><label>
        <input type="text" name="link_1" id="link_1" />
      </label></td>
      <td><label>
        <input type="text" name="location_1" id="location_1" />
      </label></td>
    </tr>
    <tr>
      <td><label>
        <input type="text" name="text_2" id="text_2" />
      </label></td>
      <td><label>
        <input type="text" name="link_2" id="link_2" />
      </label></td>
      <td><label>
        <input type="text" name="location_2" id="location_2" />
      </label></td>
    </tr>
    <tr>
      <td><label>
        <input type="text" name="text_3" id="text_3" />
      </label></td>
      <td><label>
        <input type="text" name="link_3" id="link_3" />
      </label></td>
      <td><label>
        <input type="text" name="location_3" id="location_3" />
      </label></td>
    </tr>
  </table>
    <label>
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
  </p>
</form>
FRM;

if(isset($_GET['Submit'])){
$inputs = array();
 for($i = 0; $i >= 3; $i++){
   $inputs[$i]['text'] = (isset($_GET['text_'.$i])) ? $_GET['text_'.$i] : '';
   $inputs[$i]['link'] = (isset($_GET['link_'.$i])) ? $_GET['link_'.$i] : '';
   $inputs[$i]['location'] = (isset($_GET['location_'.$i])) ? $_GET['location_'.$i] : '';
 }




foreach($inputs as $row){
echo 'New link '.$row['text'].' '.$row['link'].' '.$row['location'].'<br />';
}
}
?> 
try that.. ($inputs was an undefined variable.. so i defined it)

Posted: Tue Dec 11, 2007 6:38 pm
by farkewie
Thanks for the reply,

I now dont get any errors but it will not echo anything.

Posted: Tue Dec 11, 2007 6:53 pm
by califdon
Looks like that loop will never run:

Code: Select all

for($i = 0; $i >= 3; $i++){
The middle term should be the condition for it to run, so it needs to be:

Code: Select all

for($i = 0; $i <= 3; $i++){

Posted: Tue Dec 11, 2007 7:46 pm
by farkewie
Thank you for the reply,

it is almost working now, it is echoing data out..

only problem it always echo'es 4 "newlink" lines out even though there are only 3 form fields and the "for" statment is set to 3.
also if i only fill the form out once it still echo'es 4 times

here is my code

Code: Select all

<?php

if ( isset($_GET['Submit']) )
{

    $inputs = array();
    for ( $i = 0; $i <= 3; $i++ )
    {
// i have changed the code from ( isset($_GET['text_' . $i]) ) to the below to try and resolve 
        $inputs[$i]['text'] = ( !empty($_GET['text_' . $i]) ) ? $_GET['text_' . $i] : '';
        $inputs[$i]['link'] = ( !empty($_GET['link_' . $i]) ) ? $_GET['link_' . $i] : '';
        $inputs[$i]['location'] = ( !empty($_GET['location_' . $i]) ) ? $_GET['location_' .
            $i] : '';
    }





        foreach ( $inputs as $row )
        {
            echo 'New link ' . $row['text'] . '   --    ' . $row['link'] . '    --    ' . $row['location'] .
                '<br />';
        }
    }

?>
result from filling all 3 form fields out.

New link -- --
New link Link 1 -- http://link1.com -- Location 1
New link Link 2 -- http://link2.com -- Location 2
New link Link 3 -- http://link3.com -- Location 3

result from filling one of the three forms out.
New link -- --
New link Link 1 -- http://link1.com -- Location 1
New link -- --
New link -- --