Getting content from one site and inserting to our database in PHP


               include 'simple_html_dom.php'; // plugin for html parser with php
		$curl = curl_init(); // initializing command line tool url
		curl_setopt ($curl, CURLOPT_URL, "urlname"); // setting up to the target url
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($curl, CURLOPT_REFERER, "urlname");

		$result = curl_exec ($curl);
		curl_close ($curl);
		//write contents of $result to file
		$File = "video_show.html";
		$fh = fopen($File, 'w') or die("can't open file");
		fwrite($fh, $result);
		fclose($fh);
		$page = file_get_html("video_show.html"); 	//turn file into dom object
		$header = $page->find('table');

XML DOM PARSER


</pre>
<?php

*/ $url = 'urlname';
 $ch = curl_init();
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // setting option for url retrival
 curl_setopt ($ch, CURLOPT_URL, $url);
 curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
 curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
 $raw_data = curl_exec ($ch);
 curl_close ($ch);
 $xml = simplexml_load_string($raw_data); // loads xml content
 for($i=0; $i<1;$i++) {
 $data = $xml->channel->item[$i]; // for counting each item content from server
 $name = $data->title;
 $link = $data->link;
 $description = $data->description;
 $content = $data->content;
 ?>
<!-- <div class='blog_link'><a href='<?php echo $link; ?>'><?php echo $name; ?></a></div>-->
<?php echo "<div class='blog_description'>".$description.$content."</div>";?>
 <div class='blog_more'><a href='<?php echo $link; ?>'>Read More</a></div>
<?php } ?>
<pre>

Creating two functions in AJAX with Javascript


= 60 && $arr[1]!=0)
{
$h = floor($tim/60); $m = $tim%60;
$res = $h.’ Hrs ‘.$m.’ Mins’;
}
else if($tim >= 60 && $arr[1]==0)
{
$h = floor($tim/60); $m = $tim%60;
$res = $h.’ Hrs ‘;
}

return $res;
}
?>

<?php
while($row = mysql_fetch_array($result1)) {
$str = str_replace(' ', '', $row['remark']);
$sr = str_replace('On Time', '',$str);
$s = str_replace('Delay', 'late by', $sr);

$ar = explode(" ", $s);
$me = strtolower($row['arrived']);
$mea = ucwords($me);
$z = $row['image'];
echo "

“;
echo “

To Schduled Status
“.”

PHP HTML Dom Parser


		$counter = 0;
		foreach($header as $url) {
			if ($counter&gt;1) { // counter has set if content is empty/null. Here if counter greater than one contents will be displayed too.
					$item['image'] = $url-&gt;find('a',0)-&gt;href;		
					$item['airline'] = $url-&gt;find('span',0)-&gt;plaintext;
					$item['flight'] = $url-&gt;find('span',1)-&gt;plaintext;
					$item['ac'] = $url-&gt;find('span',2)-&gt;plaintext;
					$item['arrived'] = $url-&gt;find('span',3)-&gt;plaintext;
					$item['time'] = $url-&gt;find('span',4)-&gt;plaintext;
					$item['expected'] = $url-&gt;find('span',5)-&gt;plaintext;
					$item['remark'] = $url-&gt;find('span',6)-&gt;plaintext;			
					$a[] = $item; 
					// inserting content from web page to database after clearing HTML
						$query = "INSERT INTO live_flight (image, airline, flight, ac, arrived, time, expected, remark)
									VALUES('".$item['image']."',
										   '".$item['airline']."',
											'".$item['flight']."',
											'".$item['ac']."',
											'".$item['arrived']."',
											'".$item['time']."',
											'".$item['expected']."',
											'".$item['remark']."')
								";
						$result = mysql_query($query);
}
			while($row = mysql_fetch_array($result1)) {			
					$str = str_replace('&nbsp;', '', $row['remark']);				
					$sr = str_replace('On Time', '',$str);									
					$s = str_replace('Delay', 'late&nbsp;by', $sr);
					$ar = explode(&quot; &quot;, $s);
					$me = strtolower($row['arrived']);
					$mea = ucwords($me);
					$z = $row['image'];
					echo &quot;<tr>";
					echo  "<td>"."<a href='$z'>".$mea."</a>"."</td>".
						  "<td>".$row['time']." "."</td>".
						  "<td>".$ar[0]. ' '.human_time($ar[1])."</td>";
					echo "</tr>"; 					 	
			} 

Swapping two variables without using third variable


<!–Swapping two number without using third variable–>
<?php
$a = 3;
$b = 2;

echo “Before swapping a and b : $a, $b”.”<br/>”;

$a = $a+$b; // value of $a now is 5
$b = $a-$b; // value of $b now is 3
$a = $a-$b; // value of $a now is 2

echo “After swapping a and b : $a, $b”;

?>

This is one of the simple logic questions asking in the freshers interview.
Usually we used to swap two variables using the third temporary variable.
Here we assigned $a itself a temporary varible. So that we don’t need third one.
Here what the logic is assign $a’s or $b’s value to one place.
ie) $a=3; $b=2; $a= $a+$b;

Now $a is 5 (logic goes here)

Using the $a value 5, we can start swapping $a & $b in the above manner.

Limiting the amount of text returned from a query in PHP


<?php
$text = “Hand picked gift ideas for geeks from the geeky gift experts  don’t worry if you aren’t a geeky person yourself, we’ve got some of the coolest geeky products from video game geek stuff to nostalgic geek things to please even the most difficult, hardcore, and obscure geeks.Hand picked gift ideas for geeks from the geeky gift experts  don’t worry if you aren’t a geeky person yourself, we’ve got some of the coolest geeky products from video game geek stuff to nostalgic geek things to please even the most difficult, hardcore, and obscure geeksrson yourself, we’ve got some of the coolest geeky products from video game geek stuff to nostalgic geek things to please even the most difficult, hardcore, and obscure geeks.Hand picked gift ideas for geeks from the geeky gift experts  don’t worry if you aren’t a geeky person yourself, we’ve got some of the coolest geeky products from video game geek stuff to nostalgic geek things to please even the most difficult, hardcore, and obscure geeks.”;

function trunc($phrase, $max_words=50)
{
$phrase_array = explode(‘ ‘,$phrase);
if(count($phrase_array) > $max_words && $max_words > 0)
$phrase = implode(‘ ‘,array_slice($phrase_array, 0, $max_words));
echo $phrase;
}

trunc($text);

?>

Displaying sticky in front page of wordpress


<!– sticky post comes here –>
<?php if(is_home()) {
// The Query
$args = array(‘post__in’ => get_option( ‘sticky_posts’ ),
‘ignore_sticky_posts’ => 1);
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();?>
<div id=”sticky_title”><h1><?php the_title();?></h1></div>
<div id=”sticky_excerpt”><p><?php the_excerpt(); ?></p></div>
<?php
endwhile;
// Reset Query
wp_reset_query();
}
?>
<!– sticky post ends here –>

Back to top