| Traffic |
Registered users: 129
Newest member: dxplrndtge
Total Hits: 136336
Unique Hits: 13659
Todays Hits: 196
Todays Unique Hits: 34 |
|
 |
|
| In the spotlight |
| HTML TAGS 2 - Tags between Body Tages |
| How to close down a form with a button |
| How to make a german farmhouse part2 interior |
|
 |
|
|
|
| Replacing codes with smileys from your MySQL DB |
Ok this tutorial Will teach you 2 things to do with displaying the data recieved from a mysql database differently on your page. This is useful because it can be used to make such things as smileys and word censors for your site.
First of all I will show you all of my tables and stuff so that we arent at any confusion. If your reading this tutorial then I am assuming that you know the basics of MySQL and how to make your tables and connect to a database
News
Smileys
Now I'm assuming that you know how to connect to your database or else you wouldn't be reading this tutorial... would you well if you are wondering how to connect to a db and make all of these tables visit the site at a later date to view our full range of tutorials
Here is how you should normally be displaying your news:
| Code |
<table border="0" cellpadding="0" cellspacing="1" bgcolor="#000000" style="color:#000000 ">
<?
$getnews = mysql_query("SELECT * from news order by news_id DESC"); //get data from the table 'news' in your db
while($rgn = mysql_fetch_array($getnews)) //this bit basically sorts out your rows so that you can display them right
{
extract($rgn); //makes each column name be easy to show by just using variables, eg the column username becomes echo $username; if we want to show it
?>
<tr><td bgcolor="#FFFFFF"><? echo $news_post; ?><br>
Posted by <? echo $news_username; ?></td></tr> <? //each mysql row will produce a new row in our html table ?>
<? } ?>
</table>
Link
|
To make the smileys work first insert a load of the ones you want into your table called smileys. Now to make the smileys show we need to use the str_replace(); function. Because we are using mysql and the way I code the extracter we can change the smileys as they come out of the MySQL not as they go into the Mysql as most flat file scripts will do. Here is my table of smileys and word censors(in browse mode of phpMyAdmin)
Now you have your smileys sorted, we can move onto the coding part of things again. Please note that I have added another str_replace function to replace with <br>, this makes your text output on a new line each time there is a new line in the MySQL.
| Code |
<table border="0" cellpadding="0" cellspacing="1" bgcolor="#000000" style="color:#000000 ">
<?
$getnews = mysql_query("SELECT * from news order by news_id DESC"); //get data from the table 'news' in your db
while($rgn = mysql_fetch_array($getnews)) //this bit basically sorts out your rows so that you can display them right
{
extract($rgn); //makes each column name be easy to show by just using variables, eg the column username becomes echo $username; if we want to show it
$getsmileys = mysql_query("SELECT * from smileys");
while($rgsmileys = mysql_fetch_array($getsmileys))
{
extract($rgsmileys);
$news_post = str_replace("$smiley_before","<img src='$smiley_after'>",$news_post); // for each smiley it repeats this code which changes the output of the text
}
$news_post = str_replace(" ","<br>",$news_post); //New lines in mysql now = new lines in text?>
<tr><td bgcolor="#FFFFFF"><? echo $news_post; ?><br>
Posted by <? echo $news_username; ?></td></tr> <? //each mysql row will produce a new row in our html table ?>
<? } ?>
</table>
Link
- Preview
|
For making the word censors repeat the above code but change
| Code | | $news_post = str_replace("$smiley_before","<img src='$smiley_after'>",$news_post); |
To something like
| Code | | $news_post = str_replace("$smiley_before","$smiley_after",$news_post); | |
Posted by
James
In category
Phpon 08/11/2006 |
| Comments |
| RE: Replacing codes with smileys from your MySQL D |
HeadShot Head Moderator

Posts: 20
Joined:
21/10/2006
Rep: 0.100 |
|
Cool tutorial, hope you carry on with LS. ttp://headshots-tutorials.co.nr - Free tutorials! (My Site)
ttp://image-house.co.nr - Free image hosting (My Site)
http://pivot-animation.co.nr - Pivot Animations/Tutorials and more! (My Site)
http://tutorials4u.co.nr - Free range of tutorials!!! Visit now and tell your mates (My Site)
|
|
|