Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts

    Exclamation Shorter please!!!?

    Is there an easy way I can make this smaller using php?

    Code:
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <table class="editor" border="0" cellspacing="0" cellpadding="5">
      <tr>
        <td>*Type:</td>
        <td><select name="type" class="INPUT_type">
          <option value="anime" selected>Anime</option>
          <option value="yaoi">Yaoi</option>
          <option value="yuri">Yuri</option>
          <option value="movie">Movie</option>
        </select></td>
      </tr>
      <tr>
        <td>*Title:</td>
        <td><input class="INPUT_title" type="text" name="title"></td>
      </tr>
      <tr>
        <td>Japanese Title</td>
        <td><input class="INPUT_japtitle" type="text" name="japtitle"></td>
      </tr>
      <tr>
        <td>Image:</td>
        <td><input class="INPUT_image" type="text" name="image"></td>
      </tr>
      <tr>
        <td>Genres:</td>
        <td><input class="INPUT_genres" type="text" name="genres"></td>
      </tr>
      <tr>
        <td>Formats:</td>
        <td><input class="INPUT_formats" type="text" name="formats"></td>
      </tr>
      <tr>
        <td>Number of Episodes:</td>
        <td><input class="INPUT_episodes" type="text" name="episode_number"></td>
      </tr>
      <tr>
        <td>Length:</td>
        <td><input class="INPUT_length" type="text" name="length"></td>
      </tr>
      <tr>
        <td>Year(s) Published:</td>
        <td><input class="INPUT_year" type="text" name="year"></td>
      </tr>
      <tr>
        <td>Opening Theme Song(s):</td>
        <td><textarea class="INPUT_opening" type="text" name="opening"></textarea></td>
      </tr>
      <tr>
        <td>Ending Theme Song(s):</td>
        <td><textarea class="INPUT_ending" type="text" name="ending"></textarea></td>
      </tr>
      <tr>
        <td>*Summary:</td>
        <td><textarea class="INPUT_summary" type="text" name="summary"></textarea></td>
      </tr>
      <tr>
        <td>*Summary's author:</td>
        <td><input class="INPUT_s_author" type="text" name="s_author"></td>
      </tr>
      <tr>
        <td>Review:</td>
        <td><textarea class="INPUT_review" type="text" name="review"></textarea></td>
      </tr>
      <tr>
        <td>Review's author:</td>
        <td><input class="INPUT_s_review" type="text" name="s_review"></td>
      </tr>
      <tr>
        <td>Episodes titles:</td>
        <td><textarea class="INPUT_episode" type="text" name="episode"></textarea></td>
      </tr>
      <tr>
        <td colspan="2"><input class="INPUT_submit" type="submit" name="submit" value="Submit!"></td>
        </tr>
    </table>
    </form>

    My code continues with

    PHP Code:
    foreach($_POST as $name => $v) {
                
    $data[$name] = escape_data($v);
            }
            
    $last_updated date("Y-m-d");
            
    $date_created date("Y-m-d");
        
                if( 
    mysql_query"INSERT INTO `anime_reviews` SET title = '$title', title_jap = '$japtitle', image = '$image', genres = '$genres', formats = '$formats', episode_number = '$episode_number', length = '$length', released = '$year', opening_t = '$opening', ending_t = '$ending', sites = '$site', summary = '$summary', episodes = '$episode', review = '$review', s_author = '$s_author', r_author = '$r_author', last_updated = '$last_updated', date_created = '$date_created', type = '$type'" )) 
    And as you can see it's very big and very messy I want to make it readable and short?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    INSERT INTO `anime_reviews` SET title = '$title',
    Quote Originally Posted by php.net
    It is your responsibility to die()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Something like this maybe? Kinda hacky, but it does show the idea.
    Frankly, for one form its better off to keep what you have. But, if you decide to add more forms, you may want to consider creating an object to do this work for you, where the function would become part of your method calls. That will significantly reduce the amount of work you need to do. I'm more of a templater myself, but this does look like it will work. And no, I didn't test it first :P
    My example uses an ID instead of a class for the identifiers though, since it didn't appear that any of yours were replicated, I see no reason to use a class. But if its classes you need, just change the ID's to class.
    PHP Code:
    <?php

    $aFieldNames 
    = array(
        array(
    'select''type''*Type', array('anime' => 'Anime''yaoi' => 'Yaoi''yuri' => 'Yuri''movie' => 'Movie'), 0),
        array(
    'text''title''*Title'),
        array(
    'text''japtitle''Japanese Title'),
        array(
    'text''image''Image'),
        array(
    'text''genres''Genres'),
        array(
    'text''formats''Formats'),
        array(
    'text''episode_number''Number of Episodes'),
        array(
    'text''length''Length'),
        array(
    'text''year' 'Year(s) Published'),
        array(
    'textarea''opening''Opening Theme Song(s)'),
        array(
    'textarea''ending''Ending Theme Song(s)'),
        array(
    'textarea''summary''*Summary'),
        array(
    'text''s_author''*Summary\'s Author'),
        array(
    'textarea''review''*Review'),
        array(
    'text''s_review''*Review\'s Author'),
        array(
    'textarea''episode''Episodes Titles')    
    );

    function 
    processFields($field)
    {
        
    $result "    <tr>\n";
        if (
    count($field) < 3) :
            
    // Or throw an exception whatever is good
            
    trigger_error("Invalid field provided: " implode(", "$field) . "\n");
            return 
    false;
        endif;
        
        switch(
    $field[0]) :
            case 
    'text':
                
    $result .= buildTextField($field[1], $field[2]);
                break;
            case 
    'textarea':
                
    $result .=buildTextArea($field[1], $field[2]);
                break;
            case 
    'select':
                
    $result .= buildSelectMenu($field[1], $field[2], $field[3], $field[4]);
                break;
        endswitch;
        
        
    $result .= "    </tr>\n";
        return 
    $result;
    }
    function 
    buildTextField($fieldName$fieldLabel)
    {
        
    $result "      <td>" $fieldLabel "</td>\n";
        
    $result .= "      <td>";
        
    $result .= '<input type="text" name="' $fieldName '" id="INPUT_' $fieldName '" value="" />';
        
    $result .= "      </td>\n";
        return 
    $result;
    }
    function 
    buildTextArea($fieldName$fieldLabel)
    {
        
    $result "      <td>" $fieldLabel "</td>\n";
        
    $result .= "      <td>";
        
    $result .= '<textarea name="' $fieldName '" id="INPUT_' $fieldName '"></textarea>';
        
    $result .= "      </td>\n";
        return 
    $result;   
    }
    function 
    buildSelectMenu($fieldName$fieldLabel$options$selIndex)
    {
        
    $result "      <td>" $fieldLabel "</td>\n";
        
    $result .= '      <td>';
        
    $result .= '        <select name="' $fieldName '" id="INPUT_' $fieldName '">' "\n";
        
    // Bah, I'm too lazy to change the index specifier.  Keeping with int.
        
    $i 0;
        foreach (
    $options AS $val => $disp)
        {
            if (
    $i++ == $selIndex) :
                
    $selected 'selected="selected"';
            else :
                
    $selected '';
            endif;
            
    $result .= '          <option value="' $val '"' $selected '>' $disp "</option>\n";
        }
        
    $result .= "        </select>\n";
        
    $result .= "      </td>\n";

        return 
    $result;
    }
    ?>
    <form action="<?php $_SERVER['SCRIPT_NAME']; ?>" method="post">
    <table class="editor" border="0" cellspacing="0" cellpadding="5">
    <?php
    foreach ($aFieldNames AS $fields)
    {
        echo 
    processFields($fields);
    }
    ?>
      <tr>
        <td colspan="2"><input class="INPUT_submit" type="submit" name="submit" value="Submit!" /></td>
        </tr>
    </table>
    </form>
    Nihongo ga hanase masuka? Boku mo! (sorry, not japanese character sets installed :P)

    EDIT
    Did I have the wrong idea for what you wanted for 'shorter' code? I see someone else posted in the time I was typing :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Thanks that was very useful, but I think I'm going to stick with what I got. What I had in mind was a smaller PHP code. Like create loop for every name that I want.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No problem. That is exactly what I have done for you above. The problem is, that you have different type of input values and need to determine how to show those as well. Thats why I stuck with the array option.
    I think I mentioned it as well, that this really doesn't help you if you only have one form. It would be more useful if you created an object out of it and used it across many different forms - then you would see the benifits of doing it in PHP.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •