Hi all, I am having a problem with this code, When the submit button is pressed all the php code is echoed to the screen.


Thanks

David

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>

<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

 <div id="wrapper">
   <div id="content"><!-- Main content -->
  	
    <!-- Search movie information -->
     <form action = 'current.php' method='post'>
      Search Movie Title
      <input type='text' name='title' />
      
       <p> i.e </p>
       <p>url: http://www.imdb.com/title/tt0892318</p>
       <p>     Title code: tt0892318</p>
       <p>     Numerical code: 0892318</p>
       <br/>
       <br/>
       &nbsp; <input type='submit' name='submit' value='Submit' />
     </form>
    </div><!-- Content tag end -->
 </div><!-- Wrapper tag end -->

</body>
</html>
<?php
//current.php

if (isset($_POST['submit'])){
   $title = $_POST['title'];
  
 
   //Here I'm spliting the title variable to see what the first two letter are
   $titleSplit = str_split($title, 2);
 
   if($titleSplit[0] == 'tt'){
      $url = 'http://www.imdb.com/title/' . $title;// $title value expected tt0892318
	  $title = $url;
   }
   elseif($titleSplit[0] == 'ht' ){
      
	  $title = $url;// $title value expected http://www.imdb.com/title/tt0892318
   }
   else
   {
      $url = 'http://www/imdb.com/title/tt' . $title;// title value expected tt0892318
	  $title = $url;
	  
   } 

}



$tagName = 'h1';
$attrName = 'class';
$attrValue = 'header';
 
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($title);
 
$html = getTitle($dom, $tagName, $attrName, $attrValue);

// Error reporting for empty strings
if (!$html)
{
	echo 'Incomplete Data, Redirecting...';
			Redirect("error.html");
}
 
/**
 * JavaScript Redirect Function - Redirects the user should the result data be empty
 * @param string $url the dynamic url to redirect to
 */
function Redirect($url) 
{ ?>
	<script type="text/javascript">
		window.location = "<?php echo $url ?>"
	</script>
<?php 
}

$i=0;
	while(isset($html[$i]))
	{
		$header = $html[$i];
 		echo $header;
		$take = preg_split("/\ /", $html[$i]);
		
		for($n=0;$n<count($take);$n++)
		{ 
			//print($take[$n]);
			
		}		
		$i++;
	}
	echo "<br/>";
function getTitle($dom, $tagName, $attrName, $attrValue)
{
    	$html = '';
    	$domxpath = new DOMXPath($dom);
    	$newDom = new DOMDocument;
    	$newDom->formatOutput = true;
 
	$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
 
    	$i = 0;
   	$x = 0;
    	while( $myItem = $filtered->item($i++) )
	{		
		$html[$x] = $filtered->item($x)->nodeValue ."<br>";
    		$x++;
	}
    	return $html;
 
}
// Extract ratings
$tagName = 'span';
$attrName = 'id';
$attrValue = 'star-bar-user-rate';
$subtag = 'b';
 
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($title);
 
$html = getRatings($dom, $tagName, $attrName, $attrValue);
 
	$i=0;
	while(isset($html[$i]))
	{
		echo "Viewer Ratings:"."&nbsp;",$html[$i];
 
 
		$i++;
	}
echo "<br/>";
function getRatings($dom, $tagName, $attrName, $attrValue)
{
	
    	$html = '';
    	$domxpath = new DOMXPath($dom);
    	$newDom = new DOMDocument;
    	$newDom->formatOutput = true;
 
	$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
 
    	$i = 0;
   	$x = 0;
    	while( $myItem = $filtered->item($i++) )
	{		
		$html[$x] = $filtered->item($x)->nodeValue ."<br>";
    		$x++;
	}
    	return $html;
 
}
//Produces the directors name
$tagName = 'div';
$attrName = 'class';
$attrValue = 'txt-block';
$subtag = 'a';
 
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($title);
 
$html = getDirector($dom, $tagName, $attrName, $attrValue, $subtag);
 
	$i=0;
	//while(isset($html[$i]))
	while(isset($html[$i]) and ($i < 1))
	{
		echo $html[$i];
 		echo "<br/>";
 
		$i++;
	}
 
function getDirector($dom, $tagName, $attrName, $attrValue, $subtag)
{
    	$html = '';
    	$domxpath = new DOMXPath($dom);
    	$newDom = new DOMDocument;
    	$newDom->formatOutput = true;
 
	//$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
 $filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']"); //remove "/$subtag"
    	$i = 0;
   	$x = 0;
    	while( $myItem = $filtered->item($i++) )
	{		
		$html[$x] = $filtered->item($x)->nodeValue ."<br>";
    		$x++;
	}
    	return $html;
 
}

// Extract cast members

$tagName = 'td';
$attrName = 'class';
$attrValue = 'name';
$subtag = 'a';
 
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($title);
 
$html = getCast($dom, $tagName, $attrName, $attrValue, $subtag);
 
	$i=0;
	//extracting the first 3 cast members using a while loop
	while(isset($html[$i]) and ($i < 3))
	{
		
		echo $html[$i];// echo to the browser the first 3 cast members 
 
 
		$i++;
	}
 echo "<br/>";
function getCast($dom, $tagName, $attrName, $attrValue, $subtag)
{
    	$html = '';
    	$domxpath = new DOMXPath($dom);
    	$newDom = new DOMDocument;
    	$newDom->formatOutput = true;
 
	//$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
 $filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']"); //remove "/$subtag"
    	$i = 0;
   	$x = 0;
    	while( $myItem = $filtered->item($i++) )
	{		
		$html[$x] = $filtered->item($x)->nodeValue ."<br>";
    		$x++;
	}
    	return $html;
 
}
// Extracting release date
$tagName = 'div';
$attrName = 'class';
$attrValue = 'txt-block';
$subtag = 'h4';
 

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($title);
 
$html = function_Release_date($dom, $tagName, $attrName, $attrValue, $subtag);
			
	$i=0;
	$x=0;
	while(isset($html[$i]))
	{
		//Find array value that contains "Release". The "i" means case-Insensitive
		if (preg_match("/Runtime/i", $html[$i]))
		{ 
			if(preg_match("/See/i", $html[$i]))
			{
				//Ignore the array value that says "See more"
			}
			else
			{
				echo $html[$i] ."<br>";
				
			}
		}
		$i++;
	}
 
function function_Release_date($dom, $tagName, $attrName, $attrValue, $subtag)
{
    	$html = '';
    	$domxpath = new DOMXPath($dom);
    	$newDom = new DOMDocument;
    	$newDom->formatOutput = true;
 
	$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
 
  
    	$i = 0;
   		$x = 0;
    	while( $myItem = $filtered->item($i++) )
	{		
		$html[$x] = $filtered->item($x)->nodeValue ."<br>";
    		$x++;
	}
    	return $html;
 
}
//Runtime of movie
$tagName = 'div';
$attrName = 'class';
$attrValue = 'txt-block';
$subtag = 'h4';
 

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($title);
 
$html = function_Runtime($dom, $tagName, $attrName, $attrValue, $subtag);
		
	$i=0;
	$x=0;
	while(isset($html[$i]))
	{
		//Find array value that contains "Release". The "i" means case-Insensitive
		if (preg_match("/Release/i", $html[$i]))
		{ 
			if(preg_match("/See/i", $html[$i]))
			{
				//Ignore the array value that says "See more"
			}
			else
			{
				echo $html[$i] ."<br>";
			}
		}
		$i++;
	}
 
function function_Runtime($dom, $tagName, $attrName, $attrValue, $subtag)
{
    	$html = '';
    	$domxpath = new DOMXPath($dom);
    	$newDom = new DOMDocument;
    	$newDom->formatOutput = true;
 
	$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
 
  
    	$i = 0;
   		$x = 0;
    	while( $myItem = $filtered->item($i++) )
	{		
		$html[$x] = $filtered->item($x)->nodeValue ."<br>";
    		$x++;
	}
    	return $html;
 
  }

?>

Recommended Answers

All 3 Replies

strange... the code looks okay to me. Maybe start commenting out blocks of code to narrow down where the problem is occurring?

Hi Nonshatter, I have included form html and the below php code and there is no output at line 28. I am using WAMP and I have also uploaded it to the my domain and still the code is displayed on screen?

Really puzzling me now

Cheers

David

<?php
//current.php
 
if (isset($_POST['submit'])){
   $title = $_POST['title'];
 
 
   //Here I'm spliting the title variable to see what the first two letter are
   $titleSplit = str_split($title, 2);
 
   if($titleSplit[0] == 'tt'){
      $url = 'http://www.imdb.com/title/' . $title;// $title value expected tt0892318
	  $title = $url;
   }
   elseif($titleSplit[0] == 'ht' ){
 
	  $title = $url;// $title value expected http://www.imdb.com/title/tt0892318
   }
   else
   {
      $url = 'http://www/imdb.com/title/tt' . $title;// title value expected tt0892318
	  $title = $url;
 
   } 
 
}
echo $title;
?>

Hi Nonshatter, I have started to get this to work again by rebuilding the page. Now one of the problems is that the only statement that is working is the 1st if and elseif and else don't provide any value outputs at all? I have placed a number of echo statments throughout the script to test this

I am not sure if you or anyone has any ideas

Thanks in advance

David

<?php
if (isset($_POST['submit'])){
   $title = $_POST['title'];
 
 //echo $title;// Test expected string output ok
 
 $titleSplit = str_split($title, 2);
  
  
   if($titleSplit[0] == 'tt'){
      $url = 'http://www.imdb.com/title/' . $title;// $title value expected tt0892318
	  $title = $url;
	  echo " this should be tt", $title;
   }
   elseif($titleSplit[0] == 'ht' ){// This could also search ww to enable the programmer to ommit the HTTP:// element of the url
     
	  $title = $url;// $title value expected http://www.imdb.com/title/tt0892318
	   echo " this should be ht", $title;
   }
   else
   {
      
	  $url = 'http://www/imdb.com/title/tt' . $title;// title value expected 0892318
	  $title = $url;
	   echo " this should be 08", $title;
   } 


}


echo $title;
 ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.