<<

. 115
( 132 .)



>>


string start_form ([string action, [array attributes]])

end_form()
This function returns a closing form tag.

string end_form(void)

text_field()
Returns an HTML <input type=text> form element. Default size is 10.

string text_field ([string name [, string value [, int size [, int
maximum length]]]])

textarea_field()
This function returns an HTML textarea field. The default size is 50 columns and
10 rows, and the default wrap mode is soft, which means no hard newline characters
674 Part V: Appendixes

are inserted after line breaks in what the user types into the field. The alternative
wrap mode is hard™, which means that hard newlines are inserted.

string textarea_field([string name [, string value [, int cols [,
int rows [, string wrap mode]]]]])

password_field()
This function returns an HTML password field. This is like a text field, but the value
of the field is obscured (only stars or bullets are visible for each character). The
default size of the field is 10. A starting value and maximum data length may be
supplied.

string password_field ([string name [, string value [, int size [,
int maximum length]]]])

hidden_field()
This function returns an HTML hidden-form element. A name and value may be
supplied.

string hidden_field ([string name [, string value]])

file_field()
This function returns a text field from an HTML form.

string file_field([string name])

This function returns an HTML file field. These are used to specify files on the
user™s local hard drive, typically for uploading as part of the form.


See http://www.zend.com/manual/features.file-upload.php
for more information about how PHP interacts with the local file system.




submit_field()
This function returns an HTML submit field. The value of the field is the string dis-
played by the button displayed by the user™s browser. The default value is Submit.

string submit_field ([string name [, string value]])
Appendix H: Helpful User-Defined Functions 675

image_field()
This function returns an HTML image field. An image field works like a submit
field, except that the image specified by the URL given in the second argument is
displayed instead of a button.

string image_field ([string name [, string src [, string value]]])

reset_field()
This function returns an HTML reset field.

string reset_field ([string name, [string value]])

checkbox_field()
This function returns an HTML checkbox field. The optional third argument is
included immediately after the checkbox field, and the pair are included between a
<nobr> tag and </nobr> tag ” meaning that they are displayed together on the
same line. If the value of the second or third argument matches that of the fourth
argument, the checkbox is checked (that is, flipped on).

string checkbox_field ([string name [, string value [, string label
[, string match]]]])

radio_field()
This function returns an HTML radio button field. The optional third argument is
included immediately after the radio button, and the pair are included between a
<nobr> tag and </nobr> tag ” meaning that they are displayed together on the
same line. If the value of the second or third argument matches that of the fourth
argument, the radio button is checked (that is, flipped on).

string radio_field ([string name [, string value [, string label [,
string match]]]])

select_field()
This function returns an HTML select field (popup field). If the optional second
argument is an array, each key in the array is set to the value of an option of the
select field, and the corresponding value from the array is the displayed string for
that option. If the key or the value from the array matches the optional third argu-
ment, that option is designated as the default value of the select field.

string select_field ([string name [, array items [, string default
value]]])
676 Part V: Appendixes


Additional Functions
Not Used in This Book
Here are a couple of functions that may make dealing with common queries a bit
easier.

insert_row()
This is a generic function to run SQL insert statements.

function insert_row($table=””, $atts=””)
{
if(empty($table) || !is_array($atts))
{
return False;
}
else
{
while (list ($col, $val) = each ($atts))
{
//if null go to the next array item
if ($val==””)
{
continue;
}
$col_str .= $col . “,”;
if (is_int($val) || is_double($val))
{
$val_str .= $val . “,”;
}
else
{
$val_str .= “˜$val™,”;
}
}
$query = “insert into $table
($col_str)
values($val_str)”;
//trim trailing comma from both strings
$query = str_replace(“,)”, “)”, $query);
}
safe_query($query);

return mysql_affected_rows();
}
Appendix H: Helpful User-Defined Functions 677

This function takes two attributes: the first is the table name, and the second
should be an associative array, with the key being the column name and the value
being the value to be inserted. Single quotes that should surround a string are
included if the variable is not an integer or a double. The function returns false if
the query fails to perform an action. It does not work in all circumstances, because
it doesn™t check for the column type from the database. But it can be nice for creat-
ing pages quickly.
Empty values in the array are not added to the query. For columns left out of the
query, MySQL inserts either null values or empty strings, depending on whether or
not the column allows nulls.
Note that you can create the associative array from a set of variables using the
compact() function. For example, the following creates an associative array named
$array and then inserts a row into a table named mytable. (It™s assumed that you
have already connected to the database.)

$category=””;
$category_id=6;
$category_name=”my category”;
$array=compact(“category”, “category_id”, “category_name”);
if (!insert_row(“mytable”, $array))
{
echo “insert failed”;
}


update_row()
This function will run SQL update statements.

function update_row($table=””, $atts=””, $where=””)
{
if(empty($table) || !is_array($atts))
{
return FALSE;
}
else
{
while(list ($col, $val) = each ($atts))
{
if ($val==””)
{
continue;
}
if(is_int($val) || is_double($val))
{
$str .= “$col=$val,”;
678 Part V: Appendixes

}
elseif($val==”NULL” || $val==”null”)
{
$str .= “$col=NULL,”;
}
else
{
$str .= “$col=™$val™,”;
}
}
}
$str = substr($str, 0, -1);
$query = “update $table set $str”;
if (!empty($where))
{
$query .= “ where $where”;
}
mysql_query($query) or
die (mysql_error());
return mysql_affected_rows();
}

This function takes three arguments: $table, a string, $atts, an associative
array containing keys of column names and values to be inserted, and $where,
which is the condition ” for example column_id = 1.
Again, this function is not robust enough to work in all circumstances.

delete_row()
This function takes two arguments: $table, the table name, and $where, the value
in the where clause. It returns false on failure or 0 if nothing was deleted.

function delete_row($table=””, $where=””)
{
if (empty($table) || empty($where))
{
return FALSE;
}
$query = “delete from $table where $where”;
mysql_query($query) or die (mysql_error());
return mysql_affected_rows();
}
Appendix H: Helpful User-Defined Functions 679


select_to_table()
This function takes a query and lays it out in a simple HTML table. It assumes that
a database connection has already been made.

function select_to_table($query)
{
$result=mysql_query($query);
$number_cols = mysql_num_fields($result);
echo “<b>query: $query</b>”;
//layout table header
echo “<table border = 1>\n”;
echo “<tr align=center>\n”;
for ($i=0; $i<$number_cols; $i++)
{
echo “<th>” . mysql_field_name($result, $i). “</th>\n”;
}
echo “</tr>\n”;//end table header
//layout table body
while ($row = mysql_fetch_row($result))
{
echo “<tr align=left>\n”;
for ($i=0; $i<$number_cols; $i++)
{
echo “<td>”;
if (!isset($row[$i])) //test for null value
{
echo “NULL”;
}

<<

. 115
( 132 .)



>>