What is the output from this PHP code?
$valid = TRUE;
$cnt = 0;
while ($valid = TRUE) {
$cnt++;
if ($cnt > 10) { $valid = FALSE; }
}
echo "$cnt";
php Interview Questions
php interview questions,vanila networks interview expected questions,php code sippets, php code result, php new ideas, php coding,php jobs
How to create pagination in forums
Here is a simple pagination program in php that can be used to add pagination for array of results
Step 1:Set Database in function dbconnect()
Step 2: crate a table with fields id(auto increment),postname,post_desc,post_owner
step 3: Uncomment last line in code for first use (For sample data insertion)
step 4:Remove that uncommented line
Paging
_______________________________________________________________________
Step 2: crate a table with fields id(auto increment),postname,post_desc,post_owner
step 3: Uncomment last line in code for first use (For sample data insertion)
step 4:Remove that uncommented line
Paging
_______________________________________________________________________
//code start
<style>
a
{
text-decoration:none;
}
.post
{
width:300px;
background-color:#CCCCCC;
}
.bbutton
{
background-color:#6699FF;
padding-right:10px;
padding-left:10px;
border:thin #000000 solid;
}
.wrapper
{
position:absolute;
height: 800px;
margin: 0 auto;
overflow: hidden;
width: 948px;
}
</style>
<?php
///// Paging for Forums ////////////
// +-----------------------------------------------------------------------------+
// | Please uncomment the last line for first run for sample data insertion.
// | comment it after first run.
// | Set Database in function dbconnect() |
// +-----------------------------------------------------------------------------+
// +-----------------------------------------------------------------------------+
// | Author: sreehari k s email:sreeharinew@gmail.com |
// +-----------------------------------------------------------------------------+
//
class main
{
public function main()
{
///Display Posts //////
$this->post($this->post_array());
$this->paging($this->post_array());
///Display Display //////
$this->paging_display();
}
///Display Posts //////
public function post(array $result)
{
$result=$this->paging($result);
echo '<div class="wrapper">';
if(is_array($result))
for( $i=0; $i<count($result); $i++ )
{
echo '<div class="post">';
echo "<h3>".$result[$i]['postname']."</h3>";
echo "<h4>".$result[$i]['post_desc'] ."</h4>";
echo "postNo ".$result[$i]['id'];
echo '<div>';
}
echo '<div>';
}
public function paging(array $result)
{
////////Variables////////
$this->trows=2;////////Number of posts per page//////
$this->section=1;/////////Current page ////////
$this->arrCount=count($result);////Total Post ////////
////page no from url ///////
if($_REQUEST["pgsec"]!=="")
{
$this->section=$_REQUEST["pgsec"];
}
////Extract Page from array //////
$arrSet=array_chunk($result,$this->trows);
if(isset($arrSet[$this->section-1]))
return($arrSet[$this->section-1]);
else
return($arrSet[0]);
}
//////Paging Display //////////
public function paging_display()
{
$btnsCount=2;///////Howmany Pages want to show before and after current page //////
$stCount=1;
$ttlpages=ceil($this->arrCount/$this->trows);
$cpage=$this->section;
/////Finding end page(in display)
$endCount=$cpage+$btnsCount;
if($endCount>$ttlpages)
$endCount=$ttlpages;
echo '<div>';
if($this->arrCount > $this->trows)
{
//////Print First /////
if($cpage > $btnsCount+1)
{
echo '<a href="http://'.$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] .'?pgsec=1" class="bbutton" >First</a>';
////////Intermediate pages///////
$this->intermediate(2,($cpage-$this->trows));
}
if($cpage>$btnsCount)
{
$stCount=$cpage-$btnsCount;
}
for($i=$stCount;$i<=$endCount;$i++)
{
echo '<a href="http://'.$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] .'?pgsec='.$i.'" class="bbutton" >'.$i.'</a>';
}
//////Print Last/////
/////Removes Last if last page number visible//////
if($endCount<$ttlpages)
{
$this->intermediate(($cpage+$this->trows),$ttlpages); ///////Display Randon pages
echo '<a href="http://'.$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] .'?pgsec='.$ttlpages.'" class="bbutton" >Last</a>';
}
}
echo '</div>';
}
///////Randompages display
public function intermediate($start=1,$end=1)
{
$dispbtn=rand($start,$end);
/////// condition for display ///////
if($dispbtn!=$start && $dispbtn!= $end )
{
echo '<a href="http://'.$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] .'?pgsec='.$dispbtn.'" class="bbutton" >'.$dispbtn.'</a>';
}
}
//////Database Connection///////
Public function dbconnect()
{
$DB_NAME="test_reg";
$DB_HOST="localhost";
$DB_USER="root";
$DB_PASS="";
$cn=mysql_connect($DB_HOST,$DB_USER,$DB_PASS)or die("Connection Error");
mysql_select_db($DB_NAME,$cn);
}
public function databasequery()
{
$this->dbconnect();
$i=1;
while($i<200)
{
mysql_query("insert into forum(postname,post_desc,post_owner) values ('postname ".$i." ', 'Post content for Sample post Sample post Sample post', 'owner')");
$i++;
}
}
//////Retrive array ////////
public function post_array()
{
$this->dbconnect();
$res=mysql_query("select id,postname,post_desc,post_owner from forum");
if(mysql_num_rows($res)>0)
{
while($row=mysql_fetch_array($res))
{
$darray[]=$row;
}
return($darray);
}
}
}
$ob=new main();
////Insert Data to table //////
//$ob->databasequery();
//End of code
Expected php interview questions
Predict the result of following code ?
if (TRUE) { print 'oh'; } else { print 'ha; }
if (TRUE) { print 'oh'; } else { print 'ha; }
Freequently asking php Questions : Require and include
What is the difference between Require_once, Require and include ?
Php Questions:code snippet
What will be the result of following code snippet ?
if ('y') { print 'ohhhhh'; } else { print 'yahoo'; }
if ('y') { print 'ohhhhh'; } else { print 'yahoo'; }
Php interview Questions : Find output
Find the output of php code
for($i=0;$i<2;$i++)
echo ++$z;
echo $z++;
for($i=0;$i<2;$i++)
echo ++$z;
echo $z++;
Subscribe to:
Posts (Atom)