Posted by Santhosh Kuma on May 22nd, 2008
This tutorial will provide you with a basic concept for creating alternating row colors. Using while loops and generating a list from the mysql table.
Firstly, Save the sql structure below as data.sql and upload the sources to your mysql database
#
# Table structure for table data_list
#
CREATE TABLE `data_list` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
#
# Dumping data for table data_list
#
INSERT INTO `data_list` VALUES (1,'Ford');
INSERT INTO `data_list` VALUES (2,'Toyota');
INSERT INTO `data_list` VALUES (3,'Mitsubishi');
INSERT INTO `data_list` VALUES (4,'Honda');
INSERT INTO `data_list` VALUES (5,'Chevrolet');
INSERT INTO `data_list` VALUES (6,'Mercidez Benz');
INSERT INTO `data_list` VALUES (7,'Isuzu');
INSERT INTO `data_list` VALUES (8,'Jeep');
INSERT INTO `data_list` VALUES (9,'Suzuki'); |
Secondly, Once you have done creating and uploading your database structure. You can start working on table.php.
1
2
3
4
5
6
7
8
9
| <?php
$dbhost = 'localhost'; // Change to your host.
$dbuser = 'USERNAME'; // Change to your user.
$dbpass = 'PASSWORD'; // Change to your password.
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
// $dbname relates to the database name you gave when setting up the mysql settings.
$dbname = 'DATABASENAME';
mysql_select_db($dbname);
?> |
10
11
12
13
| <table border="1"><tr>
<td bgcolor="#FF9900"><strong>No.</strong></td>
<td bgcolor="#FF9900"><strong>Company</strong></td>
</tr> |
This section above is the start of the table with its headers
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
| <?php
// Get data from mysql table.
$sql = "SELECT * FROM data_list";
$result = mysql_query($sql) or die("Error connecting to mysql");
// Make a variable "$num" with a value "1".
$num=1;
// Do while loop for out put records.
while($row=mysql_fetch_assoc($result)){
// Plus 1 at $num.
$num++;
// Use modulus by 2 in $num value and set the value of "$bg" if result equal 0 or none.
if(($num%2)!=0){
// Prints the First part of the row.
$bg="#FFCC00";
echo "<tr><td bgcolor='$bg'><strong>$row[id]</strong></td><td bgcolor='$bg'><strong>$row[name]</strong></td></tr>";
}else{
// Prints the SECOND part of the row.
$bg="#FFFF00";
echo "<tr><td bgcolor='$bg'><strong>$row[id]</strong></td><td bgcolor='$bg'><strong>$row[name]</strong></td></tr>";
} // Close (if).
} // End while loop.
mysql_close(); // Close database connection.
?>
</table> |
The last section above is self explanatory.
To view a live Demo : Click Here!
To Download the script above : Click Here!
Posted by Santhosh Kuma on May 21st, 2008
This is a PHP hit counter that logs one occurrence of each visitor’s IP address. It then displays an accurate count of the total number of unique visitors to your website.
The script is placed on your page wherever you would like the hit count to show (usually on a home page). Every time that page is accessed, the script checks a log file for the user’s IP address. If it finds a match, the count is not advanced. If it does not find a match, the count is incremented by one and the new IP is appended to the log file. The count is displayed on screen.
Files You will need crip_hits.php and crip_hits.txt (should be blank).
Brief explanation on crip_hits.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| /* Don't forget to CHMOD $file to 777 */
$file = "crip_hits.txt";
// Obtain users IP address
$ipadd = getenv(REMOTE_ADDR);
$addip = "TRUE";
$hits = 0;
if (file_exists($file))
{
} else
{
echo "$file does not exist!";
exit;
} |
The above code will obtain the ip of the visitor and also checks for the log file, it displays a error message when the log.txt file is missing and ends the script.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| // Open $file and search each line (IP address) for a match
$fp = fopen($file,"r");
while (!feof($fp))
{
$line = fgets($fp, 4096); //gets one line at a time
$line=trim($line);
if ($line != "")
{
$hits++;
}
// If IP is already logged
if ($line==$ipadd)
{
$addip = "FALSE";
}
}
fclose($fp); |
This part opens the log file and checks for the ip of the visitor and ends if the ip is already logged.
1
2
3
4
5
6
7
8
9
10
11
12
| // If the IP was not previously logged, append it to $file
if ($addip == "TRUE")
{
$fp = fopen($file,"a");
fwrite($fp, "\n");
fwrite($fp, $ipadd);
fclose($fp);
$hits++;
}
// Display hits
echo $hits; |
Lastly, if the ip of the visitor is NOT logged. The script will open the .txt file and log the ip. It than displays the count on the page.
Embedding PHP in HTML
If you want to embed the counter to your html pages, you will need to edit the .htacess file in your Apache Server.
1
| AddType application/x-httpd-php .htm .html |
INSTALLING
- Copy the script from crip_hits.php and paste it directly into your HTML code where you want the count to show. Save the file.
- Upload the saved file and crip_hits.txt to the same directory (folder).
- Upload the .htacess file to where your main index page is located
- CHMOD crip_hits.txt to 777.
- CHMOD .htacess to 777.
NOTE: You must upload the blank crip_hits.txt file for the script to work correctly. If crip_hits.txt is not present when the script is executed it will display an error message in the count! The file will not be automatically created!
To Download the script above : Click Here!
Posted by Santhosh Kuma on April 10th, 2007
I have been looking online for some time to redirect http:// URL to https:// automatically. So when user request for cripperz.com it will be redirected to https://cripperz.com. After several tries this is what i have came up with :
1
2
3
| RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} |
- Open your preferred editor
- Copy-paste text and save as . htaccess
- Upload the file to your root directory (where your main index file is located)
- Lastly, CHMOD the file to 775 or 777
You have got yourself a redirection 
~Happy Coding~
Recent Comments