Change href link with php form

I am making a website with a search bar. I want the search bar to be interactive as soon as it “searches” and shows results. Therefore, I want href to be processed according to which identifier is used. For example: someone is looking for "Pinecones" if there is an ID in the database, for this example it's number 4. As soon as they searched him, he would appear as a link. But I want the link to use "/#IDNumber.php"

This is the im code using:

<?php $output = ''; //collect if(isset($_POST['search'])) { $searchq = $_POST['search']; $searchq = preg_replace("#[^0-9a-z]#i","",$searchq); $query = mysql_query("SELECT * FROM `findgame` WHERE name LIKE '%$searchq%' OR keywords LIKE '%$searchq%' LIMIT 1") or die("Search unavailable."); $count = mysql_num_rows($query); if($count == 0){ $output = 'Results not found.'; }else{ while($row = mysql_fetch_array($query)) { $name = $row['name']; $kwords = $row['keywords']; $id = $row['id']; $output .= '<div style="position:absolute;margin:110px 20px;padding:25px;">'.$id.' '.$name.'</div>'; } } } ?> 

and

 <a href="/<?php$id?>.php"> <?php print("$output");?></a> 

Any help in her work?

+5
source share
2 answers

You need to print the variable.

 $id = 123; <?php $id ?> => <?php echo $id ?> => 123 <?= $id ?> => 123 

Thus, the end result will be something like this:

 <a href="/<?= $id ?>.php"> <?php print($output); ?> </a> 

Note. You don’t need " around $output . It will not hurt, but it is not needed.

+4
source

I don’t understand what the problem is, for example, I don’t know if you are setting $ id and $ output to any value. I do not know if the context of the code you posted is in the PHP line or in the template.

However, if this is just a matter of syntax, then it might be better:

Like a PHP string:

 $out = '<a href="/' . $id . '">' . $output . '</a>'; 

Alternative:

 $out = "<a href=\"/$id.php\">$output</a>"; 

In HTML:

 <a href="/<?php print $id; ?>.php"><?php print $output; ?></a> 

or even:

 <?php print '<a href="/' . $id . '">' . $output . '</a>'; ?> 

Hope this is your problem.

Note: printing and echo can be used interchangeably; people prefer printing in this scenario, although echo is slightly faster than IIRC. None of these functions is a function, so it is not necessary to include brackets such as print ('blah').

0
source

All Articles