Home Code Blog

Here I plan to put interesting code snippets and useful things I encouter.

Hopefully something I post here will make life a little easier for well...someone.



Using PHP to send SMS text messages

Simple one page script that will send SMS text messages using PHP.

Hope you like it.

Code: sms_app.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="/style.css" />
<title> PHP SMS Message Generator </title>
</head>
<body>
 
<?php
 
// if the form has NOT been submitted
if (!isset($_POST['submit'])) {
 
?>
<div id="container">
    <h1>PHP SMS Message Generator</h1>
<form id="sms" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table width="410" align="center">
  <tr>
    <td align="right">From:</td>
    <td align="left"><input name="from" type="text" id="from" size="30" /></td>
  </tr>
  <tr>
    <td align="right">Recipient's 10 digit #:</td>
    <td align="left">
     <input name="number" type="text" id="number" size="14" maxlength="15"/> @
      <select name="carrier" id="carrier">
	<option>Select A Carrier</option>
        <option value="@message.alltel.com">Alltel</option>
        <option value="@txt.att.net">AT&T</option>
        <option value="@myboostmobile.com">Boost</option>
        <option value="@messaging.nextel.com">Nextel</option>
        <option value="@tmomail.net">T-Mobile</option>
        <option value="@vtext.com">Verizon Wireless</option>
        <option value="@vmobl.com">Virgin Mobile</option>
      </select>
    </td>
  </tr>
  <tr>
    <td align="right" valign="top">Message:</td>
    <td align="left"><textarea name="message" cols="40" rows="5" id="message"></textarea></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input class="button" type="submit" id="submit" name="submit" value="Send Message" /></td>
    </tr>
</table>
</form>
</div>
 
<?php
}// close of if--> the form has NOT been submitted
 
// if the form HAS been submitted
else {
 
// make safe variables function
function make_safe($variable) {
    $variable = trim($variable); //if storing in a database --> mysql_real_escape_string(trim($variable))
    return $variable;
    }
 
//make safe needed variables
$from = make_safe($_POST['from']);
$number = make_safe($_POST['number']);
$message = make_safe($_POST['message']);
 
//do not make_safe ---> $carrier
$carrier = $_POST['carrier'];
 
//concatenate the number and carrier
$sendto = $number.$carrier;
 
//check to see if form has been filled out correctly
if ((empty($carrier)) || (empty($sendto)) || (empty($message))) {
    echo "<div id='container'>An error has occured please try again.";
    echo "<br />";
    echo "<a href='/sms_app.php'>Go back</a></div>";
}
 
else {
//SENDS TEXT MESSAGE TO
//format like this  (example)
//mail($PhoneNumber, $Subject, $Message, 'From: ' . $Email);
//OR
//mail($PhoneNumber, $Subject, $Message);
mail($sendto,'SMS',$message,'From:' .$from);
// Currently, the subject is set to "SMS", change this is you wish
 
//has been sent successfully
echo "<div id='container'>Success!</div>";
echo "<br />";
echo "<a href='/sms_app.php'>Go back</a></div>";
}
 
 
}// close of else --> the form HAS been submitted
 
 
//the end
?>
 
</body>
</html>

And the style sheet...

Code: style.css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
body {
    font-family: Arial, Helvetica, Verdana,sans-serif;
    font-size: 14px;
    margin:10px 0 0 0;
}
 
p {
    font-size: 14px;
    font-weight: normal;
}
 
h1 {
    font-size: 22px;
    font-weight: normal;
    color: #666;
    text-align: center;
}
 
 
a:link {color:#666}      /* unvisited link */
a:visited {color:#666}  /* visited link */
a:hover {color:#808080; text-decoration: none;}  /* mouse over link */
a:active {color:#666}  /* selected link */
 
#container {
    width:430px;
    margin: 10px auto;
    padding: 8px;
    background: #F1F1F1;
    border:1px solid #BBBBBB;
}
 
.button  {
background: #FFF;
border:1px solid #336699;
color:#336699;
font-size:85%;
padding:3px 10px;
text-transform:uppercase;
}
 
.button:hover, .button:focus {
background:#DDD;
border:1px solid #000;
color:#111;
}

 

 

Limit the number of rows returned by MySQL using PHP - Pagination?

Ever wanted to know how you can display results from a table using mysql and php and limit them to whatever number you choose? I know I have. This is often refered to as "pagination" and is actually pretty simple to execute, all we will do is add the LIMIT clause to the MySQL query and it should work fine!

Using LIMIT, to do the obvious of... limiting the results.

1
2
3
4
<?PHP
//this will query records 0 to 10!
$query = mysql_query("SELECT * FROM table LIMIT 0,10") or die ("Error in query: $query. ".mysql_error());
?>

This code will allow you to fetch 10 records starting from record 0 (the first), this is helpful when you have hundreds or thousands of records in a table and/or you only want to show part of them.

How can you show the next 10 records? Easy.

You simply have to store the value of the starting row in a variable and pass it in the URL using GET.

Making sure to check if a value was already passed or not and set a default value if it wasn't zero = first row

1
2
3
4
5
6
7
8
9
10
<?PHP 
//check to see if the starting row variable was passed in the URL or not if (!isset($_GET['beginrow']) or !is_numeric($_GET['beginrow'])) { //we give the value of the starting row to 0 because nothing was found in URL $beginrow = 0;
  //we assign the value of the starting row as '0' because nothing was found in the URL, this will start from array '0'
} else { $beginrow = (int)$_GET['beginrow']; } ?>

Now your query should have this new variable ($startrow) in the LIMIT clause (if you want more or less change "10" to whatever, making sure to do the same for the below)

1
2
3
4
<?PHP
//put this part after you have done your check for the $_GET variable
$query = mysql_query("SELECT * FROM mytable order by id DESC LIMIT $beginrow, 10") or die ("Error in query: $query. ".mysql_error());
?>

Now to see the next 10 records you should have a link which will add 10 to $startrow so you can view the next 10 records

1
2
3
4
<?PHP
//this is the link to show the next 10 results
echo '<a href="'.$_SERVER['PHP_SELF'].'?beginrow='.($beginrow+10).'">Show Next 10 results</a>'; ?>

If you want to have a link to the previous results, do something like this, which in this case will only show up if the variable "beginrow" is greater than 10.

1
2
3
4
5
6
7
8
9
10
11
<?PHP
//check to see if these are the first 10 results and show previous link if not if ($beginrow >='10'){ $showpreviouslink = '<a href="'.$_SERVER['PHP_SELF'].'?beginrow='.($beginrow-10).'">Show Previous 10 results</a> - '; }   //if this is the first 10, then no results to display else { $showpreviouslink = 'No previous results - '; }
?>

And change your results link to this

1
2
3
<?PHP
echo ''.$showpreviouslink.' <a href="'.$_SERVER['PHP_SELF'].'?beginrow='.($beginrow+10).'">Show Next 10 results</a>';  ?>

Hope that helps.

 

Calculating MPG and Gas Prices using Javascript

Pretty simple little script, that allows you to enter your mileage and gallons of gas used, as many times you want. Enter the current cost of gas at the end and your done.

When your done, enter "-1" to end the loop.

Enjoy

Code snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<html>
<head>
<title>MPG Form</title>
 
<style type="text/css">
 
table {
width: 500px;
border-collapse: collapse;
border-color: #BBB;
background: #FFFFFF;
}
 
td {
text-align: left;
padding: 5px;
}
 
th {
text-align: center;
padding: 5px;
font-size: 15px;
color: #656565;
}
 
h2 {
padding: 9px 0 0 0;
font-size: 19px;
text-align: center;
color: #3366CC;
font-family: helvetica,arial,verdana;
}
 
h3 {
font-size: 15px;
text-align: center;
color: #3366CC;
padding: 0;
margin: 0;
font-family: helvetica,arial,verdana;
}
 
.notation {
text-align: center;
color: #990000;
font-size: 13px;
}
 
body {
border-top: 5px solid #990000;
background: url('/kccx.jpg') no-repeat scroll top center #FFFFFF;
}
 
body, html {
padding: 0;
margin: 0;
font-size: 11px;
color: #808080;
font-family: helvetica,arial,verdana;
}
</style>
 
<script type="text/javascript">
<!--
 
//set your variables
var origodometer = 0;
var oldodometer = 0;
var newodometer;
var newgallons;
var totalmiles = 0;
var totalgallons = 0;
var mpg;
 
//create heading
document.writeln("<h2>The most magical calculator of miles, gallons and everything in between</h2>");
 
//begin table
document.writeln("<table border='1' align='center'>");
 
//create first table row with headings
document.writeln("<tr><th>Miles</th><th>Gallons</th><th>MPG</th></tr>");
 
origodometer = parseInt(window.prompt("Enter beginning odometer reading"));
 
newodometer = parseInt(window.prompt("Enter odometer reading for this fill up (or -1 to quit)"));
 
oldodometer = origodometer;
 
// create new row for initial odometer reading
document.writeln("<tr><td>** " + origodometer + "</td><td> n/a </td><td> n/a </td></tr>");
 
 
while (newodometer != -1)
{
 
newgallons = parseInt(window.prompt("Enter gallons pumped this fill up"));
 
mpg = (newodometer - oldodometer) / newgallons;
 
totalgallons = totalgallons + newgallons;
 
totalmiles = totalmiles + (newodometer - oldodometer);
 
oldodometer = newodometer;
 
// create new row for each set of data
document.writeln("<tr>");
 
// create a column one for each piece of data
document.writeln("<td>" + totalmiles + "</td><td>" + totalgallons + "</td><td>" + mpg + "</td></tr>");
 
newodometer = parseInt(window.prompt("Enter odometer reading for this fill up or (-1 to quit)"));
 
}
 
 
// one time enter my best guess as to the average cost of a gallon of gas for this whole process
var currentcostofgas = parseFloat(window.prompt("Enter CURRENT cost of a gallon of gas"));
 
//calculate total cost of gas
var totalcostofgas = totalgallons * currentcostofgas;
 
//notation for table entry displaying original odometer reading
document.writeln("<tr><td colspan='3' ><p class='notation'>**denotes beginning odometer reading</p></td></tr>");
 
//write current cost of gas
document.writeln("<tr><td colspan='3' ><h3>Current cost of gas right now - $ " + currentcostofgas + "</h3></td></tr>");
 
//write total cost of gas
document.writeln("<tr><td colspan='3'><h3>Total cost of gas to date - $ " + totalcostofgas + "</h3></td></tr>");
 
//make title change
document.title = "Total cost of gas to date has been calculated";
 
//note to look at title change
document.writeln("<br /><h3>^ Now look at your fancy NEW title ^</h3><br />");
 
 
//close the table
document.writeln("</table>");
 
// -->
</script>
</head>
<body>
</body>
</html>
 


WELCOME

Hello, my name is Jonathan Carrico and I am a web designer/developer and technology professional living in Louisville, Kentucky. I'm also the founder of Green Web Enterprises.

I build clean functional interfaces and provide practical technology solutions . I have a genuine passion for technology, and a hunger to constantly evolve as a technology professional.

Thanks for visiting.

Our Green Web Hosting

green host

Looking for a greener host?

give them a try