Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Tutorial] Php codes.

Joined
Apr 27, 2008
Messages
667
Reaction score
124
okay, first off.
these scripts aren't all by me, most of them were found on google, and the world wide web.

They have been sourced from the web, after many (many many) hours of searching and simplifying and adapting,
and I've put them all here in one place to try and help others on the same quest.

An acknowledgment is owed to the ease of use, and the power of PHP, and to the many people who are so adept at writing code, and by backtracking through the web, bookmarks etc I've added the begin source URL wherever possible.

I hope these scripts below help.

let's get started shall we?

Alternate coloured rows in a table


<TABLE WIDTH="100%" HEIGHT="40" BORDER="0" CELLPADDING="10" CELLSPACING="5" BGCOLOR="#FFFFFF">
<TR bgcolor="#F0F0F0">

// General display the table information and titles of the columns

<TD HEIGHT="40" ALIGN=bottom ID="header"><em><FONT FACE="Verdana, Arial, Helvetica, sans-serif" SIZE="1" COLOR="#8A8A8A">Column A </FONT></em></TD>
<TD ALIGN=middle ID="header"><em><FONT COLOR="#8A8A8A" SIZE="1" face="Verdana, Arial, Helvetica, sans-serif">Column B</FONT></em></TD>
<TD ALIGN=middle ID="header"><em><FONT COLOR="#8A8A8A" SIZE="1" face="Verdana, Arial, Helvetica, sans-serif">Column C</FONT></em></TD>
<TD ALIGN=middle ID="header"><em><FONT COLOR="#8A8A8A" SIZE="1" face="Verdana, Arial, Helvetica, sans-serif">Column D </FONT></em></TD>
</TR>

//an empty spacer row
<TR>
<TD HEIGHT="40" ALIGN=middle ID="header"> </TD>
<TD ALIGN=middle ID="header"> </TD>
<TD ALIGN=middle ID="header"> </TD>
<TD ALIGN=middle ID="header"> </TD>
</TR>

<?php

$num=mysql_num_rows($result);
$i=0;

// Count rows in database and assign variables to results

while ($i < $num) {
$a=mysql_result($result,$i,"thumb");
$b=mysql_result($result,$i,"reference");
$c=mysql_result($result,$i,"more");
$d=mysql_result($result,$i,"price");

// alternate #8F8F8F with #000000

print ($i % 2) ? "<tr bgcolor=\"8F8F8F\">" : "<tr bgcolor=\"000000\">";

print "<td align=right>$a</td>";
print "<td align=right>$b</td>";
print "<td align=right>$c</td>";
print "<td align=right>$d</td>";

++$i;
}
?>

</table>
This script reads a database, and displays results.
The script creates a table (you can view and format the table in standard html) and then pastes information, swapping row colours if the row result is divisible by 2..


Display 5 rows to a page

<?PHP
mysql_connect("db_host","db_user","db_password");
mysql_select_db("db_database");

// set up alternative query results
if(!$rowstart) $rowstart=0;
$result = mysql_query("select * from dbtable limit $rowstart,5");
$result2 = mysql_query("select * from dbtable");
$records=mysql_num_rows($result2);
?>

<html>
<?php
// echo how many displaying if necessary
if ($records>5) echo "display 5 per page";
?>

<?php
if ($rowstart>$numrows)
{?>

</P><DIV ALIGN="LEFT"><P>
<IMG SRC="images/previous.gif" WIDTH="11" HEIGHT="16" ALIGN="ABSMIDDLE" BORDER="0" ALT="Previous Page">
<A HREF="<? $php_self ?>?rowstart=<? echo $rowstart-5;?>">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif" SIZE="1">Previous Page</FONT></A>

<? }?>

<?php
//use 2 images as well - previous.gif and next.gif
$numrows=mysql_num_rows($result2);
if($rowstart+5<$numrows)
{?><A HREF="<? $php_self?>?rowstart=<?echo $rowstart+5 ;?>"><FONT FACE="Verdana, Arial, Helvetica, sans-serif" SIZE="1">Next
Page</FONT> <IMG SRC="images/next.gif" WIDTH="11" HEIGHT="16" BORDER="0" ALT="Next Page" ALIGN="ABSMIDDLE"></A>
<? }?>

<?php
// and then do normal database query stuff
$num_rows = mysql_num_rows($result);
if ($myrow = mysql_fetch_array($result)) {
echo "eg $records available";
} else {
echo "Sorry, no records available";
}

</html>
Simple display Previous and Next Results, 5 to a page

2 column display from database

<html>
<table border="0" CELLSPACING="0" CELLPADDING="10" align="center">

<?php


//db connect and select
$db = @mysql_connect("host", "name", "password");
if( ! ($db = @mysql_connect("host", "name", "password")) ) {
} else {
mysql_select_db("database",$db) or die("Select DB Error: ".mysql_error());
}
$result=mysql_query ("select * from table");

$count = 1;
$column = 1;
//loop statement
while ($myrow = mysql_fetch_array ($result))
{
// first column display
if ($column == 1)
{

//field is the column in your table
printf("<tr><td>%s</td>",$myrow["field"]);
}

else{
//second column display
printf("<td>%s</td></tr>",$myrow["field"]);
}

$count += 1;

$column = $count % 2;
}
?>

</table>
</html>

This script reads a database, and displays results.
The php default display is one column, whereas this neatly packages the results in 2 columns.


3, 4 or 5, etc column display from database


<html>
<table border="0" CELLSPACING="0" CELLPADDING="10" align="center">

<?php
$db = @mysql_connect("host", "name", "password");
if( ! ($db = @mysql_connect("host", "name", "password")) ) {
} else {
mysql_select_db("database",$db) or die("Select DB Error: ".mysql_error());
}

//set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc
$numcols = 3; // how many columns to display
$numcolsprinted = 0; // no of columns so far

// get the results to be displayed
$query = "SELECT * FROM table";
$mysql_result = mysql_query($query, $db);

// get each row
while($myrow = mysql_fetch_row($mysql_result))
{

//get data - eg, reading fields 0 and 1
$tn = $myrow[0];
$in= $myrow[1];

if ($numcolsprinted == $numcols) {
print "</tr>\n<tr>\n";
$numcolsprinted = 0;
}

// output row from database
echo "<td>$in $tn</td>\n";

// bump up row counter
$numcolsprinted++;

} // end while loop

$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {

}
print "<TD></TD>\n";
?>

</table>
</html>

Read a database, and display results.
The php default display is one column - here are the results in 3 or more columns.


Mailout from a MySQL database


<?php

$db = mysql_connect(host,name,password);
mysql_select_db(database,$db);

// the database has fields: name, email, hits

$sql = mysql_query("SELECT * FROM table WHERE email !='' AND hits !=''",$db);
// above to MAKE SURE NO email goes to a blank adress, or with 0 hits.

while ($row = mysql_fetch_array($sql)) {
$line="your_signature_line"; // type what you like here as a sign-off
$v = $row["name"];
$h = $row["hits"];

if (!mail($row["email"],
$subject = "your_subject_line",
$message = "1st line of message.Hi there $v.\n\n2nd line of message.\n\n
Your page had $h visitors this month\n\n
Best regards,\n$line"))
{
echo "<FONT FACE=VERDANA, ARIAL SIZE=2>Error : Mail not sent to " . $row["email"] . "</FONT><BR>\n";
} else {
echo "<FONT FACE=VERDANA, ARIAL SIZE=2>Stamped, sealed and delivered to " . $row["email"] . "</FONT><BR>\n";
}

}

mysql_close($db);
?>

Read your database, and send every entry a personal email

Well, that's all i have time for, but i will be adding more of this sooner or later.

thank me if i helped ya. :)
 
Back
Top