index.php?page=aboutme

April 26, 2002 by  
Filed under Code Snippets

I recieved an email yesterday about this:
Hi,

I visited your site daynah.net, very nice site you got :) I very curious how do you ppl extract data from a
database like ‘index.php?page=aboutme’. I read through php.net which some of them refer me and i don’t find very good page on the site to teach me on that. I currently have a database (mySQL) so i can try it out :) but i need some guide, im very new to it =)

Would you mind to guide me with how to extract data on the database and do a link like ‘index.php?page=aboutme’? I really very curious about it..heh.

Hope to hear from you soon,
Edward

—————

Hello there curious Edward. ;) A lot of people have been asking me this, so I decided to post it up to share . The coding is pretty simple. And this is only one way of doing it.

In your index.php page, put in this:

<?
  if($page == 'aboutme')
    include('aboutme.php');
  else if($page == 'downloads')
    include('downloads.php');
  else
    include('main.php');

?>

now create other files called aboutme.php, downloads.php, and main.php. And put something in them like “this is the about me page,” “this is the downloads page,” etc.

Go to your url, http://yourdomain.com/index.php?page=aboutme What do you see?

So.. is that simple? :) Another note, the pages don’t have to be aboutme.php… it can also be aboutme.txt of aboutme.html, or rename it to anything else if you like. And a mysql database is not required at all. I hope that helps.

DAYOFWEEK

April 12, 2002 by  
Filed under Code Snippets

Here’s a trick Al showed me today. It uses a mysql function called DAYOFWEEK.

$dayofweek = mysql_fetch_row(mysql_query(“select DAYOFWEEK(‘$selectday’)”));

The function returns the day of the week in numerical form. 1 is Sunday, 2 is Monday, 3 is Tuesday, and so on… Jus thought you’d like to know. :)

Working with Arrays

April 5, 2002 by  
Filed under Code Snippets

I haven’t used arrays in a very long time. Usually I just stick everything in a database and run a query. But in some cases, arrays are easier use. I figured out how to use an array fine, but then I noticed some of my array values were duplicates. How do you solve this? I did a search on google.com and found this function called array_unique(). Wow. And to think, I was going to write my own algorithm! Thanks php.net :)

Here’s a sample code on how to use an array:

< ? // This program gets all the cost centers and prints them out, deleting duplicates // Query: Get all the cost center code name
$cc_sql = “SELECT * FROM costcenter”;
$cc_result = mysql_query($cc_sql, $db);

// Create an array
$myarray = array();

// Get all cost center codes and insert into array
while($mycostcenters = mysql_fetch_array($cc_result))
{
$myarray[] = $mycostcenters["code"];
}

$myarray = array_unique($myarray); // Deletes duplicate entries
sort($myarray); // sort the array

// Iterate through the array
foreach( $myarray as $value )
{
print “value is: $value
“;
}
?>

Modularizing Code

April 4, 2002 by  
Filed under Code Snippets

Modularizing your code is the way to go. I know I haven’t been doing that much, so I’m going back and re-writing code, putting certain code in functions if they should be. Database functions are very important. They’re used really often and calling a simple line of code is a lot nicer than re-writing the same lines on ever file. Here’s a sample code of how to make your date look nice when grabbing it from a mySQL database.

function ConvertNiceDate($date)
{
// Convert mySQL date to nice formatted date
$normaldate = mysql_query(“SELECT DATE_FORMAT(‘$date’,’%c/%e/%y’)”);
$normaldate = mysql_fetch_row($normaldate);
$normaldate = $normaldate[0];
return $normaldate;
}

For more variables for the mysql date/time formats, go here.

mySQL Table Status

April 4, 2002 by  
Filed under Code Snippets

Ever wanted to know the date a table was last updated? use this:

$info = mysql_fetch_array(mysql_query(“show table status from
databasename like ‘tablename’”));
echo $info["Update_time"];

Al sent me this little beauty. ;) All you have to do is replace “databasename” with the name of your database, and “tablename” with the name of your table. Et viola! :)

Simple Pseudocode -> Complex Code

April 3, 2002 by  
Filed under Code Snippets

When programming, I usually write out what I want my program to do. Then as I’m coding, I know how to make my program do what I want it to. :)

Daynah’s Pseudocode:
1) Submit request
2) Get dept. of that request -> $myuser_dept_id
3) Find the manager of that department
4) Check he manager’s email preference
5) Get the email address of the manager
6) Email the manager IF email preference is set on yes

Daynah’s Real PHP/mySQL Code

Yup, sometimes it takes a lot of code to do something small. ;)

« Previous Page