...

View Full Version : Shorter please!!!?



Dat
01-26-2008, 05:46 AM
Is there an easy way I can make this smaller using php?



<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


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?

abduraooft
01-26-2008, 06:58 AM
INSERT INTO `anime_reviews` SET title = '$title',

It is your responsibility to die()

Fou-Lu
01-26-2008, 07:20 AM
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

$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

Dat
01-28-2008, 02:23 AM
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.

Fou-Lu
01-28-2008, 02:27 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum