TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,970
  • Latest: bynw
Stats
  • Total Posts: 196,049
  • Total Topics: 21,337
  • Online today: 230
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 1
  • Guests: 132
  • Total: 133
  • @rjen

How about a FORM? PHP Article or Block Code

Started by rumboogy, April 02, 2008, 08:50:29 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Omniverse

Great work on this little gem.

I have been playing with it a little bit, testing to see if I could use it for something. :)

I ran into an issue though. I set it up, left it mostly default how I found it. After entering the info and hitting submit, it kicks me to the smf arcade hehe. No idea why.

JPDeni

You've not given me enough information to be able to debug your problem.

Omniverse

#32
Well, here is the code I used, I really didn't change a lot.

What other information do you need? Sig has versions used.

Can see here: http://ksguild.org/index.php?page=36



global $user_info, $db_prefix, $scripturl, $ID_MEMBER;

$page_title = "<center><h1>Page Title</h1></center>"; // The title of your form. Use any html code you want to here.

$intro_table = "This is where you give information about the table in general -- explaining to the viewers what they're seeing."; // Can include html
$intro_form = "This is where you give any instructions for filling out the form.";                                                // Can include html

// Define your fields. All of these values need to be defined, even if they are empty.
// The fields will be displayed in the order in which they are listed in the array.
// $fielddef = array(
//                   array( 'type' =>         "", // text, radio, select, checkbox, textarea
//                          'caption' =>      "", // caption to be displayed on the form. Can include symbols and spaces.
//                          'name' =>         "", // a unique name for the field. No symbols or spaces
//                          'options' =>      "", // for radio and select fields. List in order you wish them to appear, separated by commas; for checkboxes, it's the value to be saved and displayed next to the box
//                          'defaultvalue' => "", // the default value for the field. Can be a variable or text. Be sure to enclose text in quotation marks
//                          'label' =>        "", // Label for the table column on the results display
//                          'required' =>     0   // 0 or 1 -- use 1 if the field must be filled out. use 0 if it's optional; never set a checkbox to be required
//                        ),
//                 );

$fielddef = array(
                   array( 'type' =>         "text",
                          'caption' =>      "Name",
                          'name' =>         "name",
                          'options' =>      "",
                          'defaultvalue' => "",
                          'label' =>        "Name",
                          'required' =>     1
                        ),
                   array( 'type' =>         "radio",
                          'caption' =>      "Gender",
                          'name' =>         "gender",
                          'options' =>      "Male,Female",
                          'defaultvalue' => "",
                          'label' =>        "Gender",
                          'required' =>     0
                        ),
                   array( 'type' =>         "radio",
                          'caption' =>      "Main or Alt?",
                          'name' =>         "mainalt",
                          'options' =>      "Main,Alt",
                          'defaultvalue' => "",
                          'label' =>        "MainAlt",
                          'required' =>     0
                        ),
                   array( 'type' =>         "select",
                          'caption' =>      "Choose Game",
                          'name' =>         "game",
                          'options' =>      "-----,AoC,DDO,LotRO,WoW",
                          'defaultvalue' => "-----",
                          'label' =>        "Game",
                          'required' =>     0
                        ),
                   array( 'type' =>         "textarea",
                          'caption' =>      "Class?",
                          'name' =>         "class",
                          'options' =>      "",
                          'defaultvalue' => "",
                          'label' =>        "class",
                          'required' =>     0
                        ),
             );

///////////////////////////////////////////////////////////////////////////////////////////////////////
db_query("CREATE TABLE IF NOT EXISTS `{$db_prefix}db_data`(
`dbid` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`ID_MEMBER` INT( 11 ) NOT NULL DEFAULT '0',
`field` TINYTEXT NOT NULL ,
`value` TEXT NOT NULL)", __FILE__, __LINE__);
///////////////////////////////////////////////////

$show_form = 'false';
$errors=array();
$fieldvalue = array();

if (isset($_POST['submitted']) && $ID_MEMBER > 0) {
  if (isset($_POST['deleteentry']) && ($_POST['deleteentry'] == 1))
    db_query("DELETE FROM `{$db_prefix}db_data`
              WHERE ID_MEMBER = $ID_MEMBER", __FILE__, __LINE__); 
  else {
// Check that all required fields have values
    foreach ($fielddef as $field)
      if (empty($_POST[$field['name']]) && ($field['required'] == 1)){ $errors[] = $field['name']; }

// There's at least one field missing
    if (isset($errors[0])) {
      $show_form = 'true';
      foreach ($_POST as $key => $value)
        $fieldvalue[$key] = $value;
    }

// All is well
    else {
// Is there already a record for this person?
      $query = db_query(
          "SELECT *
           FROM {$db_prefix}db_data
           WHERE ID_MEMBER LIKE $ID_MEMBER", __FILE__, __LINE__);

// There's a record for this person so we'll modify the record
      if(db_affected_rows() != 0) {                             
        db_query("DELETE FROM `{$db_prefix}db_data`
                WHERE ID_MEMBER = $ID_MEMBER", __FILE__, __LINE__); 
      }
      foreach ($fielddef as $field) {
        if (isset($_POST[$field['name']]))
          $value = addslashes(trim($_POST[$field['name']]));
        else
          $value = "";
        $fieldname = $field['name'];
        db_query("INSERT INTO `{$db_prefix}db_data`
               ( `dbid` , `ID_MEMBER` , `field` , `value` )
               VALUES ('', '$ID_MEMBER', '$fieldname', '$value')", __FILE__, __LINE__);

      }
    }
  }
}
elseif (isset($_GET['cleardb']) && ($_GET['cleardb']==1) && ($user_info['is_admin'])) {
  echo '<center>';
  echo 'Are you sure you want to clear the database?<br>';
  echo '<a href="' . $scripturl . '?page='  . $_GET['page'] . '&cleardb=2">Yes, I want to clear it.</a> -- <a href="' . $scripturl . '?page='  . $_GET['page'] . '">No, I changed my mind</a>';
  echo '</center>';
}

elseif (isset($_GET['cleardb']) && ($_GET['cleardb']==2) && ($user_info['is_admin'])) {
  db_query("TRUNCATE TABLE `{$db_prefix}db_data`", __FILE__, __LINE__);
  echo '<center>';
  echo 'Database cleared';
  echo '</center>';
}

else {                                                             
  if (isset($_GET['form']) && ($_GET['form']==1) && ($ID_MEMBER > 0)) {
// They've asked for the form
    $show_form = 'true';
    $query = db_query(
          "SELECT *
           FROM {$db_prefix}db_data
           WHERE ID_MEMBER LIKE $ID_MEMBER", __FILE__, __LINE__);

// There's already a record for this person, so fill in the form with previous details
    if(db_affected_rows() != 0)                                     
    {
      while ($row = mysql_fetch_assoc($query))
        $fieldvalue[$row['field']] = $row['value'];
      $previousrecord = 1;
    }

// No record, so we're adding a new one with the default values
    else {                                                       
      foreach ($fielddef as $field)
        $fieldvalue[$field['name']] = $field['defaultvalue'];
      $previousrecord = 0;
    }
  }
}

echo $page_title . "<br>";

// Looks like you want the form, but only if you're a registered member
if ($show_form == 'true' && $ID_MEMBER > 0) {
  echo $intro_form . "<br>";
  if (isset($errors[0])) { echo '<div style:"color: red;">Please fill in all fields with a *.</div>';  }
  echo '<form action="' . $scripturl . '?page='  . $_GET['page'] . '" method="post">
        <INPUT id="submitted" name="submitted" type="hidden" value="TRUE" />
        <center><table>';
  $bg = 'windowbg2';
  foreach ($fielddef as $field) {
    if ($field['type'] == 'text') {
      echo '<TR class ="' . $bg . '">
              <TD width = "200px" align="right">';
              if ($field['required'] == 1) { echo '* '; }
              echo $field['caption'] . ':</TD>
              <TD align="left"><INPUT id="' . $field['name'] . '" name="' . $field['name'] . '" type="text" value ="' . $fieldvalue[$field['name']] . '" /></TD>
            </TR>';
    }
    elseif ($field['type'] == 'radio') {
      echo '<TR class ="' . $bg . '">
              <TD width = "200px" align="right">';
              if ($field['required'] == 1) { echo '* '; }
              echo $field['caption'] . ':</TD>
              <TD align="left">';
              $options = explode(',',$field['options']);
              foreach ($options as $option) {
                echo '<INPUT TYPE="RADIO" NAME="' . $field['name'] . '" VALUE="'. $option . '"';
                if ((isset($fieldvalue[$field['name']])) && ($option == $fieldvalue[$field['name']])) { echo ' CHECKED'; }
                echo '>' . $option . ' ';
              }
              echo '</TD>
            </TR>';   
    }
    elseif ($field['type'] == 'checkbox') {
      echo '<TR class ="' . $bg . '">
              <TD width = "200px" align="right">';
              echo $field['caption'] . ':</TD>
              <TD align="left">';
                echo '<INPUT TYPE="CHECKBOX" NAME="' . $field['name'] . '" VALUE="'. $field['options'] . '"';
                if (isset($fieldvalue[$field['name']]) && ($fieldvalue[$field['name']]==$field['options'])) { echo ' CHECKED'; }
                echo '>' . $field['options'] . '
            </TR>';   
    }
    elseif ($field['type'] == 'select') {
      echo '<TR class ="' . $bg . '">
              <TD width = "200px" align="right">';
              if ($field['required'] == 1) { echo '* '; }
              echo $field['caption'] . ':</TD>
              <TD align="left"><SELECT id="' . $field['name'] . '" name="' . $field['name'] . '" style="WIDTH: 152px" value ="';
              echo '" />';
              $options = explode(',',$field['options']);
              foreach ($options as $option) {
                echo '<OPTION value="' . $option . '"';
                if ((isset($fieldvalue[$field['name']])) && ($option == $fieldvalue[$field['name']])) { echo ' selected'; }
                echo '>' . $option . '</OPTION>';
            }
            echo '</SELECT></TD>
            </TR>';
    }
    elseif ($field['type'] == 'textarea') {
      echo '<TR class ="' . $bg . '">
            <TD width = "200px" align="right">';
            if ($field['required'] == 1) { echo '* '; }
            echo $field['caption'] . ':</TD>
            <TD align="left"><TEXTAREA id="' . $field['name'] . '" name="' . $field['name'] . '" rows="4" cols="40">';
            echo $fieldvalue[$field['name']];
            echo '</' . 'TEXTAREA></TD>
  </TR>';
    }
    if ($bg == 'windowbg2') { $bg = 'windowbg'; }
    else { $bg = 'windowbg2'; }
  }
  if ($previousrecord == 1) {
    echo '<tr class ="' . $bg . '"><td colspan="2"><INPUT TYPE="CHECKBOX" NAME="deleteentry" VALUE="1"> Delete this entry</td></tr>';
    if ($bg == 'windowbg2') { $bg = 'windowbg'; }
    else { $bg = 'windowbg2'; }
  }
  echo '
    <TR class ="' . $bg . '">
      <TD colspan="2" align="center">
        <INPUT type="submit" value="Submit">
        <INPUT type="reset" value="Reset"></TD>
    </TR></TABLE></center></form>';
}

// If you don't want the form, you want the table -- when first coming to the page or after submitting a record
else {
  echo $intro_table . "<br>";
  $members = array();
  $query = db_query(
          "SELECT *
           FROM {$db_prefix}db_data", __FILE__, __LINE__);
  if(db_affected_rows() != 0)                                     
  {
    while ($row = mysql_fetch_assoc($query)) {
      if ($row['value'] == '') { $row['value'] = "&nb" . "sp;"; }
      $displayvalue[$row['ID_MEMBER']][$row['field']] = $row['value'];
      if (!in_array($row['ID_MEMBER'],$members))
        $members[] = $row['ID_MEMBER'];
    }
    $memberlist = implode(" OR ID_MEMBER = ",$members);
    $query = db_query(
          "SELECT ID_MEMBER, realName
           FROM {$db_prefix}members
           WHERE ID_MEMBER = $memberlist
           ORDER BY realName", __FILE__, __LINE__);
    while ($row = mysql_fetch_assoc($query))
      $displaymember[$row['ID_MEMBER']] = $row['realName'];
    echo '<center><table border="1"><tr><td>&nb' . 'sp;</td>';
    foreach ($fielddef as $field)
      echo '<td>'. $field['label'] . '</td>';
    echo '</tr>';
    foreach ($displaymember as $number => $name) {
      echo '<tr><td><a href="' . $scripturl . '?action=profile;u=' . $number . '">' . $name . '</a></td>';
      foreach ($fielddef as $field)
        echo '<td>'. $displayvalue[$number][$field['name']] . '</td>'; 
      echo '</tr>';   
    }
    echo '</table></center>';
  }
  else { echo "No records in the database yet."; }
  echo '<br><br>';
  if ($ID_MEMBER > 0)
    echo '<a href="' . $scripturl . '?page='  . $_GET['page'] . '&form=1">Add or modify your information</a>';
  if ($user_info['is_admin'])
    echo '<br><a href="' . $scripturl . '?page='  . $_GET['page'] . '&cleardb=1">Empty the database</a>';
}



JPDeni

I don't know. I signed up for an account in order to check it out and you're right. It does send you to the arcade. I've never worked with a site that has an arcade installed, so I don't know what the issue is and why it would do this.

There's something coming up that you might be able to use instead. Just hang on for a bit.

Omniverse

Yea, it's quite the baffling little thing... it seems everything works as intended... until you try it hehe. Not a clue why the arcade bugs it out.

But something new ya say? Interesting....

I definitely could use something like this though, look forward to whats up your sleeve. :)

JPDeni

It's not me that's got it. Someone else is working on it. I don't know when it'll be ready, though.

Heero

Sorry for the revive,
But Deni I think this might help me out with my clanwar script if you remember :).
I have a few questions about it :
1) can I delete specific selected submissions? (like, deleting a submission from one member only).
2) Can I display the 3 latest submissions in any way?

Well, that are they.
Thanks in advance :)

JPDeni

Quotemy clanwar script if you remember
Sorry. :) I have a hard enough time remembering my name these days!!! But I trust you.

At the moment, the answers would be "No" and "No". I think. I'll have to look over it a bit. Just about anything can be done. It's just a matter of working it out. I'll take a look at the code and see what I can whip up for you.

Heero

Thank you Deni.
It doesn't has to be simple though (if that makes it easier for you),
like : deleting it via the database is fine for me :-).

JPDeni

Deleting via the database is a piece of cake. You would just search for the member's ID and delete the associated records. There is a record in the database for each field on your form for each member.

What's becoming more difficult is trying to work out the last three entries. Seems like it should be easy, but not so much. I'll work on it.

This website is proudly hosted on Crocweb Cloud Website Hosting.