Code Snippets - PHP - MySQL - aExchange : Discussion Board, Collection of PHP code snippets, Question, Answers http://aexchange.info Using bind variables with ADODB - aExchange



Insert




Code:
$sql = $conn->Prepare('INSERT INTO table (id,text) VALUES (?, ?)');

if($conn->Execute($sql,array($id,$text)) === false) {

print '<br /><div id="error">error inserting: '.$conn->ErrorMsg().'</div><br />';

}




Update




Code:
$sql = $conn->Prepare('UPDATE table SET count = count + ? WHERE id = ?');

if($conn->Execute($sql,array("1",$id)) === false) {

print '<br /><div id="error">error inserting: '.$conn->ErrorMsg().'</div><br />';

}
]]>
http://aexchange.info/article.php?name=2013125608 http://aexchange.info/article.php?name=2013125608 Sat, 16 Feb 2013 01:03:46 GMT
Smarty session handling - nocache - aExchange



example 1:




Code:
{nocache}

{if $smarty.session.loggedin|default eq true}

{/nocache}

Logged in user

{nocache}

{/if}

{/nocache}




example 2:




php file



Code:
if(isset($_SESSION['loggedin']) == true){

$smarty->assign('sessiontrue','logged in users');

}else{

$smarty->assign('sessiontrue','guests');

}




template file



Code:
{$sessiontrue nocache}




]]>
http://aexchange.info/article.php?name=2013013106 http://aexchange.info/article.php?name=2013013106 Thu, 14 Feb 2013 01:36:59 GMT
CSS file upload field and browse button - aExchange



Example code:



Code:
<head>

<style>

label{display: inline-table;background: url(http://www.aexchange.info/spacer.gif) no-repeat 0 0;}

input[type="file"]{opacity:0;}

</style>

</head>

<form>

<label><input type="file" name="photoimg" id="photoimg" /></label>

</form>
]]>
http://aexchange.info/article.php?name=2013014407 http://aexchange.info/article.php?name=2013014407 Tue, 12 Feb 2013 13:45:38 GMT
Smarty MySQL Table foreach - aExchange



Smarty MySQL Table foreach Example



Code:
$recordSet = &$conn->Execute('SELECT * FROM category ORDER BY name ASC');

if (!$recordSet)

print $conn->ErrorMsg();

else

while (!$recordSet->EOF) {

$val[] = $recordSet->fields;

$recordSet->MoveNext();

}



$smarty->assign('category',$val);







Template File



Code:
{table_foreach from=$category item=foo cols=3 table_attr='valign="top" width="100%"' td_attr='valign="top" width="33%"'}

<a href="{$foo.catid}.htm">{$foo.name}</a>

{/table_foreach}
]]>
http://aexchange.info/article.php?name=2013053723 http://aexchange.info/article.php?name=2013053723 Mon, 11 Feb 2013 17:42:15 GMT
Validate URL with PHP preg_match - aExchange



checkURL.php



Code:
<?php

if(isset($_POST['checkURL'])) {

if(get_magic_quotes_gpc()) {

$url = stripslashes($_POST['url']);

} else {

$url = $_POST['url'];

}

if(strlen($url) > 0) {

if(!preg_match("/^(https?://+[w-]+.[w-]+)/i",$url)) {

echo "Invalid URL. Please Try Again or Leave that field blank.";

die();

} else {

echo "OK.";

}

}

} else { ?>

<form method="post" action="checkURL.php">

<input type="text" name="url" /><br />

<input type="submit" name="checkURL" value="checkURL" />

</form>

<?php } ?>
]]>
http://aexchange.info/article.php?name=2013034921 http://aexchange.info/article.php?name=2013034921 Sun, 10 Feb 2013 03:55:46 GMT
File Upload with PHP - aExchange



upload.php



Code:
<?php

if(isset($_POST['upload'])) {

$file = $_FILES['file']['name'];

//A quick way to get file's extension

$extension = substr(strrchr($file,'.'),1);

if(($extension != "csv") && ($extension != "txt")) {

die('Invalid file type. Please upload files with extension .csv .txt');

}

$filename = date("Yhis");

$file_uploaded = $filename.".".$extension;

$folderfile = "upload/".$file_uploaded;

//Copy - Makes a copy of the file source to destination.

//If the destination file already exists, it will be overwritten.

$copy = copy($_FILES['file']['tmp_name'],$folderfile);

if(!$copy) {

die('There was an error uploading this file to the server.');

} else {

echo "File uploaded successfully";

//Email Notification

$headers = 'MIME-Version: 1.0'."rn";

$headers .= 'Content-type: text/html; charset=utf-8'."rn";

$headers .= "From: email"."rn";

$email = 'email';

$subject = 'Email Notification';

$bodys = 'New File http://www.example.com/'.$folderfile.'';

mail($email,$subject,$bodys,$headers);

}

} else {

?>

<form method="post" enctype="multipart/form-data" action="upload.php">

<input type="file" name="file" ><br />

<input type="submit" name="upload" value="submit">

</form>

<?php } ?>
]]>
http://aexchange.info/article.php?name=2013112441 http://aexchange.info/article.php?name=2013112441 Fri, 8 Feb 2013 11:26:23 GMT
Filtering bad words - PHP - aExchange



Code:
<?php

function badWordFilter($string)

{

$bad = array("sample2","sample4","sample6");

for($i = 0; $i <= sizeof($bad); $i++) {

$string = str_replace($bad[$i],"-!removed!-",$string);

}

return $string;

}

if(isset($_POST['submit'])) {

$words = $_POST['words'];

echo badWordFilter($words);

} else { ?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<input type="text" value="sample2 sample3 sample6" name="words">

<input type="submit" name="submit" value="Check Words">

</form>

<?php } ?>
]]>
http://aexchange.info/article.php?name=2013014504 http://aexchange.info/article.php?name=2013014504 Thu, 7 Feb 2013 01:46:48 GMT
CSS gradient background all browsers - aExchange



Code:
<head>

<style>

#cont{

width:460px;

height:60px;

margin:0px auto;

background:-moz-linear-gradient(top,#C4C4C4 0%,#EAEAEA 0%,lightGrey 100%);

background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,#C4C4C4),color-stop(0%,#EAEAEA),color-stop(100%,lightGrey));

filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#EAEAEA',endColorstr='#C4C4C4',GradientType=0 );

}

</style>

</head>

<body>

<div id="cont"></div>

</body>
]]>
http://aexchange.info/article.php?name=2013055432 http://aexchange.info/article.php?name=2013055432 Tue, 5 Feb 2013 17:56:28 GMT
PHP Get a domain name IP address - gethostbyname - aExchange

Returns the IP address of the Internet host specified by hostname.





Code:
<?php

if(isset($_POST['getvalue'])) {

$host = $_POST['host'];

$host = gethostbyname($host);

echo $host;

} else { ?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

DomainName: <input type="text" name="host">

<input type="submit" name="getvalue" value="Gethostbyname">

</form>

<?php } ?>
]]>
http://aexchange.info/article.php?name=2013032206 http://aexchange.info/article.php?name=2013032206 Tue, 5 Feb 2013 03:25:21 GMT
PHP Captcha Example - aExchange



Code:
<?php session_start();

Header("Content-type: image/png");

$Width = 75;

$Height = 28;

$img = ImageCreateTrueColor($Width,$Height);

$bg = imagecolorallocate($img,255,255,255);

imagefill($img,0,0,$bg);

$randgrid = rand(3,11);

$grid = imagecolorallocate($img,248,248,248);

imagesetstyle($img,array($bg,$grid));

imagegrid($img,$Width,$Height,$randgrid,IMG_COLOR_STYLED);

$randangle = rand(0,0);

$randay = rand(22,24);

$randfnt = rand(22,24);

$font = 'classes/fonts/cour.ttf';

$white = imagecolorallocate($img,0,128,234);

$word = rand(1111,9999);

$where = imagettfbbox($randfnt,0,$font,$word);

$_SESSION['check'] = ($word);

imagettftext($img,22,$randangle,($Width - $where[4]) / 2,$randay,$white,$font,$word);

ImagePNG($img);

ImageDestroy($img);

/**

* imagegrid()

*

* @param mixed $image

* @param mixed $w

* @param mixed $h

* @param mixed $s

* @param mixed $color

* @return

*/

function imagegrid($image,$w,$h,$s,$color) {

for($iw = 1; $iw < $w / $s; $iw++) {

imageline($image,$iw * $s,0,$iw * $s,$w,$color);

}

for($ih = 1; $ih < $h / $s; $ih++) {

imageline($image,0,$ih * $s,$w,$ih * $s,$color);

}

}

?>
]]>
http://aexchange.info/article.php?name=2013044600 http://aexchange.info/article.php?name=2013044600 Mon, 4 Feb 2013 16:47:00 GMT
Check if option is selected - aExchange



Code:
<?php

if(isset($_POST['send'])) {

$selector = $_POST['selector'];

if($selector == '0') {

echo "<font color='red'>Please Select...</font>";

die();

}

echo $selector;

} else {

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<select name="selector">

<option value="0">Please Select...

<option value="Select1">Select1

<option value="Select2">Select2

<option value="Select3">Select3

<input type="submit" name="send" value="Query">

</select>

</form>

<?php } ?>
]]>
http://aexchange.info/article.php?name=2013023525 http://aexchange.info/article.php?name=2013023525 Mon, 4 Feb 2013 02:45:03 GMT
Resolving Error Loading XML - aExchange
libxml_use_internal_errors(true);


Disable standard libxml errors and enable user error handling



Code:
libxml_use_internal_errors(true);

$load = simplexml_load_string($result);

if (!$load) {

echo "Failed loading document";

foreach(libxml_get_errors() as $error) {

echo "<pre>" . $error->message . "</pre>";

}



}else{

$load = new SimpleXmlElement($result);

}
]]>
http://aexchange.info/article.php?name=2013031548 http://aexchange.info/article.php?name=2013031548 Sun, 3 Feb 2013 15:18:44 GMT
PHP Contact Us Form with HTML output - aExchange



Code:
<?php session_start();

if(isset($_POST['submit'])) {

$ccmail = 'admin';

$ipadress = $_SERVER['REMOTE_ADDR'];

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

$email = $_POST['email'];

$subject = $_POST['subject'];

$comments = $_POST['comments'];

$subtext = substr($comments,0,8);

if(@$_SESSION["reloadse"] == $subtext) {

echo "<center><div>Whoops, you already said that.</div></center>";

die();

}

$fields = array($firstname,$lastname,$email,$subject,$comments);

foreach($fields as $fields) {

if(preg_match("/</",$fields)) {

echo "<center><div>Invalid Characters";

Die();

}

if(preg_match("/\[/",$fields)) {

echo "<center><div>Invalid Characters";

Die();

}

}

if(strlen($firstname) < 2) {

echo "<center><div>Field must be at least 2 characters long.

<a href='javascript:history.go(-1)'>Go Back</a></div>";

die();

}

if(strlen($comments) < 10) {

echo "<center><div>Field must be at least 10 characters long:

<a href='javascript:history.go(-1)'>Go Back</a></div>";

die();

}

if(strlen($fields) > 500) {

echo "<center><div>Max Characters Field: 500

<a href='javascript:history.go(-1)'>Go Back</a></div>";

die();

}

$headers = 'MIME-Version: 1.0'."rn";

$headers .= 'Content-type: text/html; charset=utf-8'."rn";

$headers .= "From: $ccmail"."rn";

$bodys = "

<head>

<style>

#content{padding:4px;margin:0px auto;border:1px dotted #f8f8f8;width:733px;font-family;tahoma;color:#36539D;}

</style>

</head>

<div id = 'content'><h1>$subject</h1>First Name:$firstname<br /><br />

Last Name:$lastname<br /><br />Email:$email<br /><br />

Comments:$comments<br /><br />IP:$ipadress<br /></div>";

mail($ccmail,$subject,$bodys,$headers);

echo "<div style='text-align:center;font-family:tahoma;'>Thank You.</div>";

@$_SESSION["reloadse"] = $subtext;

} else {

?>

<head>

<style>

input, textarea{width:318px;font-family: 'trebuchet ms';font-size: 15px;}

#content{margin:0px auto;text-align:center;color: #417394;font-family:'trebuchet ms';font-size:15px;font-style:normal;}

#floatleft{float:left;}

#floatright{float:right;}

#field{width:444px;height:44px;margin:0px auto;text-align:center;}

</style>

</head>

<div id="content">

<h2>Contact Us</h2>

<form name="maForm" action="contactus.php" method="post">

<div id="field">

<div id="floatleft"><b>First name:</b></div>

<div id="floatright"><input name="firstname" type="text" /></div>

</div>

<div id="field">

<div id="floatleft"><b>Last name:</b></div>

<div id="floatright"><input name="lastname" type="text" /></div>

</div>

<div id="field">

<div id="floatleft"><b>Email:</b></div>

<div id="floatright"><input name="email" type="text" /></div>

</div>

<div id="field">

<div id="floatleft"><b>Subject:</b></div>

<div id="floatright"><input name="subject" type="text" /></div>

</div>

<div id="field">

<div id="floatleft"><b>Comments:</b></div>

<div id="floatright"><textarea name="comments"></textarea></div>

</div><br />

<div id="field">

<div id="floatleft"><input type="submit" value="Contact Us" name="submit" /></div>

</div>

</form>

<?php } ?>
]]>
http://aexchange.info/article.php?name=2013110144 http://aexchange.info/article.php?name=2013110144 Sun, 3 Feb 2013 11:02:30 GMT
PHP Form Basic Example - aExchange



Code:
<?php

if(isset($_POST['send'])) {

$value = $_POST['value'];

$second_value = $_POST['second_value'];

echo $value." ".$second_value;

} else { ?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<input type="text" name="value">

<input type="hidden" name="second_value" value="second_value">

<input type="submit" name="send" value="Query">

</form>

<?php } ?>
]]>
http://aexchange.info/article.php?name=2013103458 http://aexchange.info/article.php?name=2013103458 Sun, 3 Feb 2013 10:37:20 GMT
Smarty foreach with a adodb database query - aExchange



php file



Code:
$recordSet = &$conn->Execute('SELECT * from post ORDER by postid desc LIMIT 5');

if(!$recordSet)

print $conn->ErrorMsg();

else

while(!$recordSet->EOF) {

$val[] = $recordSet->fields;

$recordSet->MoveNext();

}



....



$smarty->assign('post',@$val);




template file





Code:
{section name="post" loop=$post}

{$post[post].title}

{/section}
]]>
http://aexchange.info/article.php?name=2013095855 http://aexchange.info/article.php?name=2013095855 Sun, 3 Feb 2013 10:04:49 GMT
PHP get Tags from text - aExchange



Code:
<?php

$text = 'Explode every individual word in text';

$single = explode(' ', $text);

foreach ($single as $value) {

$value = trim($value);

echo " <a href='search.php?q=$value'>$value</a> ";

}

?>


]]>
http://aexchange.info/article.php?name=2013094725 http://aexchange.info/article.php?name=2013094725 Sun, 3 Feb 2013 09:52:38 GMT
Sitemap