Implode - Join array elements with a string
Explode - Returns an array of strings, each of which is a substring of
string
formed by splitting it on boundaries formed by the string delimiter
. Example of Implode:
<?php
$var=array('arr1', 'arr2', 'arr3');
echo implode ( ',', $var);
echo implode ( '.', $var);
echo implode ( '/', $var);
?>
$var=array('arr1', 'arr2', 'arr3');
echo implode ( ',', $var);
echo implode ( '.', $var);
echo implode ( '/', $var);
?>
Output:
arr1,arr2,arr3
arr1.arr2.arr3
arr1/arr2/arr3
Example of Explode :
<?php
$var1= "arr1,arr2,arr3,arr4";
$var2= "arr1/arr2/arr3/arr4";
$arr1= explode(",", $var2);
$arr2 = explode("/", $var2);
echo $arr1[0]; // var1
echo $arr1[1]; // var1
echo $arr1[2]; // var1
echo $arr1[3]; // var1
echo $arr2[0]; // var2
echo $arr2[1]; // var2
echo $arr2[3]; // var2
echo $arr2[4]; // var2
?>
$var1= "arr1,arr2,arr3,arr4";
$var2= "arr1/arr2/arr3/arr4";
$arr1= explode(",", $var2);
$arr2 = explode("/", $var2);
echo $arr1[0]; // var1
echo $arr1[1]; // var1
echo $arr1[2]; // var1
echo $arr1[3]; // var1
echo $arr2[0]; // var2
echo $arr2[1]; // var2
echo $arr2[3]; // var2
echo $arr2[4]; // var2
?>
Output:
arr1
arr2
arr3
arr4
arr1
arr2
arr3
arr4
Database :
Database name :demo
Table name: demo_tbl
Fields: id,channel
Implode using database:
Source Code:
<?php
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$select_db=mysql_select_db("demo",$conn) or die(mysql_error());
?>
<!-- Implode-->
<?php
if(isset($_POST['submit']))
{
$channel=$_POST['channel'];
if($channel=="")
{
$error="<span id='errror'>Choose Favorite Tv Shows</span>";
}
else
{
//Implode
$r=implode(",",$channel);
$insert=mysql_query("insert into demo_tbl(channel) value ('$r')") die(mysql_error());
if($insert)
{
$msg="<span id='msg'>Thanks for Chosen</span>";
}
}}
?>
<html>
<head>
<title>Implode and Explode In PHP</title>
</head>
<style type="text/css">
#tbl
{
margin:10% auto;
background-color:#AFD8D8;
color:#005BB7;
border:1px solid #244848;
font-family:"Comic Sans MS", cursive
}
tr
{
text-align:center;
}
#msg
{
color:#093
}
#errror
{
color:#900;
}
</style>
<body>
<form name="form" method="post">
<table border="0" cellpadding="10" cellspacing="0" id="tbl">
<tr><td colspan="2" ><?php echo $msg?><?php echo $error?></td></tr>
<tr><td align="center" colspan="2">Choose Favorite Tv Shows</td></tr>
<tr>
<td>Channels List</td>
<td><input type="checkbox" name="channel[]" value="TenSports" />TenSports
<input type="checkbox" name="channel[]" value="StarSports" />StarSports
<input type="checkbox" name="channel[]" value="Espn" />Espn</td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>
</form>
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$select_db=mysql_select_db("demo",$conn) or die(mysql_error());
?>
<!-- Implode-->
<?php
if(isset($_POST['submit']))
{
$channel=$_POST['channel'];
if($channel=="")
{
$error="<span id='errror'>Choose Favorite Tv Shows</span>";
}
else
{
//Implode
$r=implode(",",$channel);
$insert=mysql_query("insert into demo_tbl(channel) value ('$r')") die(mysql_error());
if($insert)
{
$msg="<span id='msg'>Thanks for Chosen</span>";
}
}}
?>
<html>
<head>
<title>Implode and Explode In PHP</title>
</head>
<style type="text/css">
#tbl
{
margin:10% auto;
background-color:#AFD8D8;
color:#005BB7;
border:1px solid #244848;
font-family:"Comic Sans MS", cursive
}
tr
{
text-align:center;
}
#msg
{
color:#093
}
#errror
{
color:#900;
}
</style>
<body>
<form name="form" method="post">
<table border="0" cellpadding="10" cellspacing="0" id="tbl">
<tr><td colspan="2" ><?php echo $msg?><?php echo $error?></td></tr>
<tr><td align="center" colspan="2">Choose Favorite Tv Shows</td></tr>
<tr>
<td>Channels List</td>
<td><input type="checkbox" name="channel[]" value="TenSports" />TenSports
<input type="checkbox" name="channel[]" value="StarSports" />StarSports
<input type="checkbox" name="channel[]" value="Espn" />Espn</td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>
</form>
Explode using database:
Source Code:
<!-- Explode-->
<table width="400" border="1" cellspacing="0" cellpadding="0" align="center" id="tbl">
<tr></tr>
<tr>
<?php
$select_tbl=mysql_query("select * from demo_tbl",$conn);
while($fetch=mysql_fetch_array($select_tbl))
{
$r1=$fetch['channel'];
//Explode
$i=explode(",",$r1);
?>
<td align="left" height="30"><?php echo $i[0]." ".$i[1]." ".$i[2]; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
<table width="400" border="1" cellspacing="0" cellpadding="0" align="center" id="tbl">
<tr></tr>
<tr>
<?php
$select_tbl=mysql_query("select * from demo_tbl",$conn);
while($fetch=mysql_fetch_array($select_tbl))
{
$r1=$fetch['channel'];
//Explode
$i=explode(",",$r1);
?>
<td align="left" height="30"><?php echo $i[0]." ".$i[1]." ".$i[2]; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
No comments:
Post a Comment