2

Okay, I have been stuck for a few days on this...Have been trying to find an answer on how to pass my variable into the url of the php code and update the url each time the form is submitted. However, all I van find is how to pass the variable through a url...Not really been able to implement this as I feel I need a different solution. So here is the code, everything does seem to be working except for the passing of my $var into the URL and reupdating the script to run with the new URL.

For example the original currently looks like this:

    $html = file_get_html('http://www.championselect.net/champ/{$var}');

I want someone to enter a entry into the form and for the $var to update each time.

So: Say someone wants to search for "ziggs" or "jinx", they would enter the name into the form and the form will need to update the script to run the parser with the new $var.

Much appreciated to all help!!

<form action="test.php" method="post">
<input type="text" value="<?php echo $var?>" name="var" />
<input type="submit" value="Send" />
</form>
<?php

$var = $_POST['var'];
echo $var;


// Include the library to use it.
include_once('simple_html_dom.php');

// Get div from site. ... Need to implement user selected queries.

$html = file_get_html('http://www.championselect.net/champ/{$var}');

// Put all of the <a> tags into an array named $result
$result = $html -> find('h2,img[class=CS_Champ_Image],.counterName' );

// Run through the array using a foreach loop and print each link out using echo
foreach($result as $element) {
  echo $element."   ";}   

?>
tiptop
  • 29
  • 1
  • 8

3 Answers3

5

Try...

$html = file_get_html('http://www.championselect.net/champ/'.$var);

Variables aren't parsed inside single quotes

BA_Webimax
  • 2,714
  • 1
  • 13
  • 15
  • 1
    or just a single change: `$html = file_get_html("http://www.championselect.net/champ/{$var}");` – dev Jan 05 '14 at 06:33
  • 1
    I find placing variables inside strings reduces code readability. Concatenation shows clear delineation of the string and the variable. – BA_Webimax Jan 05 '14 at 06:41
2

Your problem is on this line:

$html = file_get_html('http://www.championselect.net/champ/{$var}');

Single quotes don’t handle string substitution. You can read up on how PHP handles strings here. So the full value of that URL—including that literal {$var}—are being set instead of what you expect. You can tackle this issue a few different ways. Such as using double-quotes which allow string substitution:

$html = file_get_html("http://www.championselect.net/champ/{$var}");

Or just concatenating the values like this:

$html = file_get_html('http://www.championselect.net/champ/' . $var);

But I prefer to use sprintf which allows you to use strings as templates like this:

$html = file_get_html(sprintf('http://www.championselect.net/champ/%s', $var));

Beyond that, your code is a bit sloppy & inconsistent, so here is my cleanup. It might not seem like a big deal, but chaotic code is simply hard to read which makes it hard to debug. Cleaner code allows you to spot flaws quicker. Also, mixing HTML & PHP is a bit confusing. In simple cases like this err on the side of what the bulk of the code is. And in this case, err on the side of just parsing it all through PHP:

<?php

// Echo the form.
echo '<form action="test.php" method="post">'
   . '<input type="text" value="' . $var . '" name="var" />'
   . '<input type="submit" value="Send" />'
   . '</form>'
   ;

// Set the $var variable.
$var = $_POST['var'];

// Include the library to use it.
include_once('simple_html_dom.php');

// Get div from site. ... Need to implement user selected queries.
$html = file_get_html(sprintf('http://www.championselect.net/champ/%s', $var));

// Put all of the <a> tags into an array named $result
$result = $html -> find('h2,img[class=CS_Champ_Image],.counterName' );

// Run through the array using a foreach loop and print each link out using echo
foreach($result as $element) {
  echo $element . " ";
}   

?>

EDIT I just noticed something else. Look at the code right a the beginning of yours or the cleaned up version I just did above:

// Echo the form.
echo '<form action="test.php" method="post">'
   . '<input type="text" value="' . $var . '" name="var" />'
   . '<input type="submit" value="Send" />'
   . '</form>'
   ;

// Set the $var variable.
$var = $_POST['var'];

So in your <form … >/</form> tags you are placing the value of $var into '<input type="text" value="' . $var . '" name="var" />'. But that $var is not assigned a value until the after the form is rendered? So shouldn’t that just be removed to be like this?

// Echo the form.
echo '<form action="test.php" method="post">'
   . '<input type="text" value="" name="var" />'
   . '<input type="submit" value="Send" />'
   . '</form>'
   ;
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
2

what you can also to do is this

$html = file_get_html('http://www.championselect.net/champ?var=' . $var);
analyticalpicasso
  • 1,993
  • 8
  • 26
  • 45