Hi all, I wish to present a user with a text field that can take any of these inputs:

The URL such as http://www.imdb.com/title/tt0892318/
or
a number which will be the title code such as tt0892318 and a number that will be prefixed with tt and be interpreted as the title code such as 0892318

On pressing the submit button the application should find the relevant page on the site.

How am able to present this best in PHP

Thanks in advance

David

Recommended Answers

All 9 Replies

First you will need a little of html for the text box

<form action = 'current.php' method='post'>
Input Movie <input type='text' name='title' />
<input type='submit' name='submit' value='Submit' />
</form>

Then you will use php to determine what the user has entered.

<?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;
   }else if($titleSplit[0] == 'ht' || $titleSplit[0] == 'ww'){
      $url = $title;
   }else{
      $url = 'http://www/imdb.com/title/tt' . $title;
   } 

   //redirecting the user to the move page
   header("Location: $url");
}

One thing on your if else statement your first if statement "$title==tt then do this $url = 'http://www.imdb.com/title/' . $title;" is fine because if the user enter "tt202020" it will append that to the url so you will have http://www.imdb.com/title/tt202020.

One litte problem with "else if $title ==ww then do this $url = 'http://www.imdb.com/title/tt0896542';" if the user decides to enter "http://www.imdb.com/title/tt34343" this part of the code won't run since the first two letters in the string are ht and not ww.

The third part of your else if statement will never be reached since you are checking the same conditions as the previous two statement. If you just use three separate if statements than the third if statement will override the first two.

If you just use the code snippet i posted before it'll work for what you are trying to do.

Hi Again, so can I just use 3 if statements to compare as below

if($titleSplit[0] == 'tt')
  {
     $url = 'http://www.imdb.com/title/' . $title;
  }

if($titleSplit[0] == 'ht' || $titleSplit[0] == 'ww')
  {
     $url = $title;

  }
else
  {
     $url = 'http://www/imdb.com/title/tt' . $title;
  } 
 
   //redirecting the user to the move page
   header("Location: $url");
}

Thanks

David

yes that will work.

One thing you might want to consider and I didn't take care of in the original code snippet is that the code will accept anything not "tt", "ht", "ww", and append whatever the user type to the the url "http:www.imdb.com/titles/tt" So lets say the user type "ererer" the code will append that onto "http:www.imdb.com/titles/tt" making it into "http:www.imdb.com/titles/ttererer"

Something you might want to think about.

Hi, yes there will be some validation on the data input and all so if the reply does not find the required information and returns an empty string.

I will look at this.

Thanks

David

Hi, Thanks for your help, I have added to the my code and I am trying to direct to an error page if the returned value is null or empty. On Line 29 I have added the following:

if ($html=" "){ header("Location: error.html");
}

This code always directs to the error page when there is content in the variable. If you can see where I am going wrong that would be great

Thanks in advance

David

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;
   }else if($titleSplit[0] == 'tt' || $titleSplit[0] == 'ww'){
      $url = $title;
   }else{
      $url = 'http://www/imdb.com/title/tt' . $title;
   } 

 }

$tagName = 'h1';
$attrName = 'class';
$attrValue = 'header';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($title);
 
$html = function_Run($dom, $tagName, $attrName, $attrValue);


if ($html=" "){ header("Location: error.html");
 }
	$i=0;
	$x=0;
	while(isset($html[$i]))
	{
		//Find array value that contains "Release". The "i" means case-Insensitive
		if (preg_match("/Letters/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_Run($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;
 
}

?>

Hi again,

Try this instead. I have used Javascript for the redirect as PHP can sometimes moan about headers:

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 
}

Hi, I am looking at the values of the variable $title and the split function on lines 7-18 will only accept the full url http://www.imdb.com/title/tt0892318. The other two inputs are tt0892318 and 0892318 and I get no output to the screen and the returned values are empty.

Thanks in advance


David

<?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;
   }else if($titleSplit[0] == 'ht' || $titleSplit[0] == 'tt'){
      $url = $title;
   }else{
      $url = 'http://www/imdb.com/title/tt' . $title;
   } 

}

$some_link = $title;

$tagName = 'h1';
$attrName = 'class';
$attrValue = 'header';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($title);
 
$html = function_Run($dom, $tagName, $attrName, $attrValue);

	$i=0;
	$x=0;
	while(isset($html[$i]))
	{
		//Find array value that contains "Release". The "i" means case-Insensitive
		if (preg_match("/Letters/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_Run($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;
 
}
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.