Closed Thread Icon

Preserved Topic: PHP Znippets? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=21031" title="Pages that link to Preserved Topic: PHP Znippets? (Page 1 of 1)" rel="nofollow" >Preserved Topic: PHP Znippets? <span class="small">(Page 1 of 1)</span>\

 
DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 03-11-2002 12:49

// output javascript variants of the GET variables
reset ($HTTP_GET_VARS);
echo "\n<script language=\"Javascript\">\n";
while (list ($key, $val) = each ($HTTP_GET_VARS)) {echo "\t$key = \"$val\";\n";}
echo "</script>\n";

I'm finding that I'm collecting a bunch of little scripts like this, (this one above just takes any and all GET variables and outputs a small javascript with those variables defined as Javascript variables.) I'm going to give some thought to collecting these all together into one main "require" file for the start of all my new PHP pages, seems like fun! Does anyone else have any small&sweet scripties like this? Maybe we could share them all here, might help some people out.

Your pal, -doc-

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 03-11-2002 16:42

Here's the only function I have that might be more generally useful to people. All it does is gather all the post variables, and format them so all the fields line up in a mono-spaced font. It disregards the Submit button as long as it is named as such.

code:
function gatherOutput() {
global $HTTP_POST_VARS, $HTTP_SERVER_VARS;
reset($HTTP_POST_VARS);
$maxLength = 0;
while (list($key,$value) = each($HTTP_POST_VARS)) {
if (strlen($key) > $maxLength) $maxLength = strlen($key);
}
for ($i=0; $i < $maxLength + 3; $i++) $emptyRow .= ' ';
reset($HTTP_POST_VARS);
while (list($key,$value) = each($HTTP_POST_VARS)) {
if ($value != '' && $key != 'Submit') {
$value = preg_replace ('/(\r\n)/',"\\1$emptyRow",$value);
if (is_array($value)) {
$firstRun = 1;
foreach ($value as $item) {
if ($firstRun) {
$pendingValue = $item;
$firstRun = 0;
} else {
$pendingValue .= "\r\n$emptyRow$item";
}
}
$value = $pendingValue;
}
$spacer = ' ';
for($i = 0; $i < ($maxLength - strlen($key)); $i++) $spacer .= ' ';
$output .= "$key$spacer: $value\n";
}
}
$output .= "\n\nForm Submitted by: {$HTTP_SERVER_VARS['REMOTE_ADDR']} aka {$HTTP_SERVER_VARS['REMOTE_HOST']}";
return $output;
}



I use certain conventions such as email address fields are 'email', and names are 'name'. Also, some email clients (Outlook Exchange Server Web Access for example) use the From: header rather than Reply-to: when replying. Here's an example of how I use this:

code:
include('/var/www/html/lib/form.php');

if($events && $name) {
$emailRecipient = "schul182@umn.edu, dasil003@tcsu.umn.edu";
$emailSubject = "Alumni Group Event Registration Form";
$emailHeaders = "From: TCSU Alumni Group Webpage\nReply-to: $email";
$emailBody = gatherOutput();

mail($emailRecipient, $emailSubject, stripslashes($emailBody), $emailHeaders);
} else {
...Code to deal with insufficient data in form.
}



-jiblet

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-12-2002 00:19

YMMV

but this is a DB class I created to make my stuff work easier. IT really needs some documentation and I stole lots from a couple of places

<?


class DB {
var $lid = 0; // Link ID for database connection
var $qid = 0; // Query ID for current query
var $row; // Current row in query result set
var $record = array(); // Current row record data
var $error = ""; // Error Message
var $errno = ""; // Error Number

// Connects to DB and returns DB lid
// PRIVATE
function connect() {

if ($this->lid == 0) {
$this->lid = mysql_pconnect(DB_HOST,DB_USER,DB_PWD);
if (!$this->lid) {
$this->halt("connect(" . DB_HOST . "," . DB_USER . ",PASSWORD) failed.");
}

if (!@mysql_select_db(DB_NAME,$this->lid)) {
$this->halt("Cannot connect to database ".DB_NAME);
return 0;
}
}
return $this->lid;
}


// Runs query and sets up the query id for the class.
// PUBLIC
function query($q) {

if (empty($q))
return 0;

if (!$this->connect()) {
return 0;
}

if ($this->qid) {
@mysql_free_result($this->qid);
$this->qid = 0;
}

$this->qid = @mysql_query($q, $this->lid);
$this->row = 0;
$this->errno = mysql_errno();
$this->error = mysql_error();
if (!$this->qid) {
$this->halt("Invalid SQL: ".$q);
}

return $this->qid;
}


// Return next record in result set
// PUBLIC
function next_record() {

if (!$this->qid) {
$this->halt("next_record called with no query pending.");
return 0;
}

$this->record = @mysql_fetch_array($this->qid);
$this->row += 1;
$this->errno = mysql_errno();
$this->error = mysql_error();

$stat = is_array($this->record);
return $stat;
}


// Field Value
// PUBLIC
function f($field_name) {
return stripslashes($this->record[$field_name]);
}

// Selective field value
// PUBLIC
function sf($field_name) {
global $vars, $default;

if ($vars["error"] and $vars["$field_name"]) {
return stripslashes($vars["$field_name"]);
} elseif ($default["$field_name"]) {
return stripslashes($default["$field_name"]);
} else {
return stripslashes($this->record[$field_name]);
}
}

// Print field
// PUBLIC
function p($field_name) {
print stripslashes($this->record[$field_name]);
}

// Selective print field
// PUBLIC
function sp($field_name) {
global $vars, $default;

if ($vars["error"] and $vars["$field_name"]) {
print stripslashes($vars["$field_name"]);
} elseif ($default["$field_name"]) {
print stripslashes($default["$field_name"]);
} else {
print stripslashes($this->record[$field_name]);
}
}

// Returns the number of rows in query
function num_rows() {

if ($this->lid) {
return @mysql_numrows($this->qid);
}
else {
return 0;
}
}

// Returns the auto increment ID from the last insert.
function get_id() {
if ($this->lid) {
return @mysql_insert_id($this->lid);
} else {
return false;
}

}



// Halt and display error message
// PRIVATE
function halt($msg) {
$this->error = @mysql_error($this->lid);
$this->errno = @mysql_errno($this->lid);

printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br>\n",
$this->errno,
$this->error);

exit;

}

}
?>


I use this in conjunction with a included config.ini File (which has all site constants set all over the place) that fills in the constants.

DB_HOST,DB_USER,DB_PWD & DB_NAME otherwise you can fill them in on your own.

$myDB = new DB;
$myDB->query('YOUR SQL QUERY");

You can do this a bunch of ways if you just have one row then you can print columns from the row like this

$myDB->p('NAME OF COL');

Field them like so
&myDB->('NAME OF COL');

To loop then
while ($myDB->next_record()) {
// Then same as above
$myDB->p('NAME OF COL');

}

whatever I'll document this better, just take a look it's pretty straight forward. It's based on some code I got from PHPSHOP which was based on code from PHPLIB

So whatever this is what works for me

I'm really into putting a bunch of related functions together.
I'm working on a file upload class based on a previous post. The idea being to try to use HTTP POST to upload and failing that using straight ftp.





.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

Weadah
Maniac (V) Mad Scientist

From: TipToToe
Insane since: Aug 2000

posted posted 03-12-2002 01:37

Asylum index0r, as seen here. Non recursive folder indexing, upload file, view, link and ubb tags. Filters for most common files, view source of docs etc etc. Not too buggy (tho output may only be ok for IE), never finished, lots stolen from sources forgotten - have at it.

Weadz (the Steve that is seen is not the true Steve)

[This message has been edited by Weadah (edited 03-12-2002).]

« BackwardsOnwards »

Show Forum Drop Down Menu