Monthly Archives: August 2011

How to show html code on a web page |php

When you are displaying the data into the website , sometime it is necessary to write the HTML code as it is. Specially when you are trying to explain the html code through the website. There are various way you can write but for me that i like the most :
1. Begin and end all tags with &lt and &gt
2. Or include that code in the textarea.
Simply
to insert a “<" you will have to type in &lt semicolon and for ">” type &gt semicolon in YOUR html code

that should do the trick, the ampersand and the semi-colon are both required
you will write like this < h1 > hi php fresher < /h1 >
Similar Links
1.Plus2net
2. Digital Point Forum
3. Web Master World

Difference between mysql_fetch_array and mysql_fetch_assoc

There is not so much difference between the mysql_fetch_array and mysql_fetch_assoc in the sense that both can fullfill your requirements but it depends on your need and ease of use. I prefer mysql_fetch_array because with it i can use both indexed value and associative value.

I will explain this difference with short example:
$sql = mysql_fetch_array(‘select name, address from tbl_customer’);
It means that you are getting answer directly into an array , and you dont need to know the field value of the elements to be outputed.
Just print the output as:

foreach($sql as $ans){
    echo $ans[0].' lives in '.$ans[1];
}
or 
foreach($sql as $ans){
    echo $ans['name'].' lives in '.$ans['address'];

}
mysql_fetch_assoc, you can output the result as $ans['name'] but not $ans[0] . What i want say is you need to know about the field name of the table. Ok here is the sample code:
foreach($sql as $ans){
    echo $ans['name'].' lives in '.$ans['address'];
}
But not
foreach($sql as $ans){
    echo $ans[0].' lives in '.$ans[1];

}

Which again relies upon your knowing what is coming out of the database, and in my opinion is better because
a) its easier to read, and
b) if you don't use OOP, it at least gets you thinking about and using OO notation.
mysql_fetch_assoc — Fetch a result row as an associative array
mysql_fetch_array — Fetch a result row as an associative
array, a numeric array, or both

htmlspecialchars vs htmlentities

From the PHP documentation for htmlentities:

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

From the PHP documentation for htmlspecialchars:

Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made; the translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use htmlentities() instead.

While htmlspecialchars converts only 5 special characters to html entities, htmlentities converts all characters which have HTML character entity equivalents.

Example

htmlentities

PHP CODE:
<?php
$str = "A 'quote' is <b>bold</b>";

echo htmlentities($str);
echo htmlentities($str, ENT_QUOTES);
?>

OUTPUT

A 'quote' is &lt;b&gt;bold&lt;/b&gt;

A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;

htmlspecialchars

PHP CODE:

<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; //
?>

OUTPUT


&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;

Sources:
stackoverflow
Wallpaperama
Webune
Php Freaks