Friday, March 13, 2015

HOW TO MAKE THE STAR TRIANGLE IN PHP?

It is very easy to make a STAR Triangle in php .This is a type of matrix in which we can print rows and columns .You can make the STAR triangle by using (for loop) or (foreach) loop in php. For or foreach loop make it very easy in to make STAR Triangle.This can improve the logic of Fresher . Check below the programming syntax in front of output:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
1
2
3
4
5
6
7
8
<?php
for($i=0;$i<=5;$i++){
for($j=5-$i;$j>=1;$j--){
echo "*&nbsp;";
}
echo "<br>";
}
?>
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
1
2
3
4
5
6
7
8
9
</pre>
<?php
for($i=0;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*&nbsp;";
}
echo "<br>";
}
?>
 *
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
1
2
3
4
5
6
7
8
<?php for($i=0;$i=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "*"." ";
}
echo "<br>";
}?>
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
1
2
3
4
5
6
7
8
9
10
11
12
<?php
 
for($i=5;$i-->=0;$i--){
for($k=5;$k>=$i;$k--){
echo "  ";
}
for($j=1;$j<=$i;$j++){
echo "*  ";
}
echo "<br>";
}
?>
 *
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
for($i=0;$i<=6;$i++){
for($k=6;$k>=$i;$k--){
echo " &nbsp;";
}
for($j=1;$j<=$i;$j++){
echo "* &nbsp;";
}
echo "<br>";
}
for($i=5;$i>=1;$i--){
for($k=6;$k>=$i;$k--){
echo " &nbsp;";
}
for($j=1;$j<=$i;$j++){
echo "* &nbsp;";
}
echo "<br>";
}
?>




1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1
2
3
4
5
6
7
<?php
for($i=0;$i<=7;$i++){
for($j=1;$j<=$i;$j++){
echo $j;
}
echo “<br>”;
} ?>
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
1
2
3
4
5
6
<?phpfor($i=0;$i<=7;$i++){
for($j=1;$j<=$i;$j++){
echo $i;
}
echo "<br>";
} ?>
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1 1
1
2
3
4
5
6
<?phpfor($i=0;$i<=7;$i++){
for($j=1;$j<=$i;$j++){
echo "1";
}
echo "<br>";
} ?>

PHP Program to print Pyramid of Stars.

Here is another fun PHP program for the school and college kids those who want to print stars of pyramid using PHP technology.
Here is the code and you can see the demo below:
Enter the number in the box to see how it works:
Enter number of lines: 




Tuesday, March 10, 2015

what is difference between mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() and mysql_fetch_object() in php

Many of the php programming newbies get confused about mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() and mysql_fetch_object() functions, but all of these functions performs a similar process.
Let us create a table “tb” for clear example with three fields “id”, “username” and “password”
Table:  tb
Insert a new row into the table with values 1 for id, tobby for username and tobby78$2 for password
db.php
1
2
3
4
<!--?<span class="hiddenSpellError" pre=""-->php
$query=mysql_connect("localhost","root","");
mysql_select_db("tobby",$query);
?&gt;
 mysql_fetch_row()
Fetch a result row as an numeric array
1
2
3
4
5
6
7
8
9
10
<html>
<!--?<span class="hiddenSpellError" pre=""-->php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_row($query);
echo $row[0];
echo $row[1];
echo $row[2];
?>
</html>
Result
1  tobby  tobby78$2
 mysql_fetch_object()
Fetch a result row as an object
1
2
3
4
5
6
7
8
9
10
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_object($query);
echo $row->id;
echo $row->username;
echo $row->password;
?>
</html>
Result
1  tobby  tobby78$2
 mysql_fetch_assoc()
Fetch a result row as an associative array
1
2
3
4
5
6
7
8
9
10
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_assoc($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];
?>
</html>

Result
1  tobby  tobby78$2
 mysql_fetch_array()
Fetch a result row as an associative array, a numeric array and also it fetches by both associative & numeric array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_array($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];

<span style="color: #993300;">/* here both associative array and numeric array will work. */</span>

echo $row[0];
echo $row[1];
echo $row[2];

?>
</html>

Thursday, March 5, 2015

Page redirect after certain time PHP

header( "refresh:5;url=wherever.php" );
this is the php way to set header which will redirect you to wherever.php in 5 seconds