Blog

Newest Post
Blog Archives
Blog Categories

Portfolio

3D Models
Banners
Brochures
Business Cards
Custom Programming
Environments
Flash Projects
Game Design
Image Altering
Web Design

About

Resume
Recommendation Letters

Contact

November 22, 2007

Character Replace within a String using PHP

Filed under: PHP — admin @ 12:45 pm

Replacing a character within a string is easy and very useful. The only problem is I have the hardest time remembering the syntax so I figured I would post it here.

$string = 'test-data';
 
//Remove the "-" and replace it with a space
$new_string = str_replace('-',' ', $string );
 
echo $new_string; //test data

November 21, 2007

Looping with PHP to Build a Table

Filed under: PHP — admin @ 12:00 pm

Looping comes in handy when you may want to execute a certain script “x” number of times or if you want to display all the information within a Recordset. Here is an example of how I display all the information stored within a Recordset into a nice table that is easy to read.

<!-- setting up the table -->
 
<table border=”1″>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Id</td>
</tr>
 
<?php //create the loop
 
do { ?>
 
<tr>
<td><?php echo $row_Recordset1['first_name']; ?></td>
<td><?php echo $row_Recordset1['last_name']; ?></td>
<td><?php echo $row_Recordset1['id']; ?></td>
</tr>
 
<?php //end the loop
 
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

This will display all the information for Recordset1 within a table with the heading cells being First Name, Last Name, and ID. Recordset1 must first be established before you will be able to populate the cells or else no data is found.

November 20, 2007

PHP Recordsets

Filed under: PHP — admin @ 8:07 pm

Recordsets in PHP are useful bits of code used to display data that is stored within a MYSQL table. You can run a loop to display all of this data or you can display only the bit of information you want. Here is how I do this:

<?php
//First Connect to the Database
 
$hostname_connect = "localhost";
$database_connect = "db_name";
$username_connect = "db_user";
$password_connect = "db_password";
$connect = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
 
//Create the Recordset and grab the data you want
 
mysql_select_db($database_connect, $connect);
$query_Recordset1 = sprintf("SELECT * FROM names WHERE name='steve'");
$Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
 
?>
 
<!–Display the data within the page –>
<body>
 
<p>Hello <?php echo $row_Recordset1['name']; ?>, you live on <?php echo $row_Recordset1['street']; ?>. You have <?php echo $row_Recordset1['kids'] ?> kids.</p>
 
</body>
 
<?php
 
//Close the Recordset and Connection
 
mysql_free_result($Recordset1);
mysql_close($connect);

The above example grabbed data from the MYSQL table “names” only where the row’s name was “Steve”. Then data is displayed about Steve in each of the rows within the table; such as address and kids.

« Previous Page
© - Digital Epiphany Designs, Online Portfolio for Web Design, Custom Programming, Grapic Design and Game Design.