【问题标题】:Linking to search results directly直接链接到搜索结果
【发布时间】:2014-04-18 13:03:28
【问题描述】:

我在工作中参与了一个超出我(非常普通的)编码技能的项目。 所以我有一个包含 6 个字段的数据库。我可以使用 php(我遵循 youtube 教程)对其进行搜索、添加记录、删除记录和更新记录。所有这些都工作得很好。 我现在需要的是:

我需要在图片上链接搜索结果(只有少数,最受欢迎的)。 例如,我的主页上有一个公司徽标(假设公司名称是..我不知道..“Google”。当我点击它时,我希望它重定向到“google”的搜索结果,例如我已经在搜索字段中插入了它。这可能吗? (请注意,我的数据库中没有公司,我只是想举一个例子,以便人们可以理解我的意图。在我的数据库中,我需要链接以反映“unidade”字段的搜索结果)

到目前为止,这是我的代码。我认为不需要添加/更新 .php。

<?php

// include the connection file
include "connection.php";

$sql = "SELECT * FROM usuariotb";

if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .=" WHERE nome LIKE '%".$search_term."%'";
$sql .=" OR posto = '{$search_term}' ";
$sql .=" OR nim = '{$search_term}' ";
$sql .=" OR unidade LIKE '%".$search_term."%'";
$sql .=" OR codigoueo LIKE '%".$search_term."%'";
}



$query = mysql_query($sql) or die (mysql_error());

if (isset($_GET['recordId'])) {

$id = mysql_real_escape_string($_GET['recordId']);
$sql_delete = "DELETE FROM usuariotb WHERE id = {$id}";
mysql_query($sql_delete) or die(mysql_error());

header("location:display_data.php");
exit();
}

?>

<html>
<head>
<meta charset="iso-8859-1">
<title></title>
<link rel="stylesheet" type="text/css" href="format.css" />
</head>
<body>
<div class="container">


<form name="search_form" method="POST" action="display_data.php">
Search: <input type="text" name="search_box" value=""/>
<input type="submit" name="search" value="Procurar">
</form>

<div class="container">
<div class="content">
<div class="toolbar"><a href="form_display.php">Adicionar Novo Militar</a></div>
</div>
<div class="toolbar"><a href="search.php">Voltar à página de pesquisa</a></div>
  </div>
</div>

<div class="container">
<div class="content">
<table width="90%" cellpadding="5" cellspacing="5">
<tr>
<td><strong>Nome</strong></td>
<td><strong>Nim</strong></td>
<td><strong>Posto</strong></td>
<td><strong>Unidade</strong></td>
<td><strong>Sigla Unidade</strong></td>
<td><strong>Observa&ccedil;&otilde;es</strong></td>
<td><strong>Ac&ccedil;&otilde;es</strong></td>
</tr>
<?php if (mysql_num_rows($query)) { ?>
<?php while ($row=mysql_fetch_array($query)) {?>

<tr>
<td><?php echo $row['nome'];?></td>
<td><?php echo $row['nim'];?></td>
<td><?php echo $row['posto'];?></td>
<td><?php echo $row['unidade'];?></td>
<td><?php echo $row['codigoueo'];?></td>
<td><?php echo $row['observacoes'];?></td>
<td><a href="display_data.php?recordId=<?php echo $row['id']; ?>">Delete</a></td>
<td><a href="edit.php?edit=<?php echo $row['id']; ?>">Edit</a></td>
</tr>

<?php } /* end loop  */ ?>

<?php } else { ?>
<h2> Nothing to display!</h2>
<?php } /* end rows checking */ ?>
</table>
</div>
</div>
</body>
</html>

【问题讨论】:

    标签: php search


    【解决方案1】:

    首先,我很同情你被赋予了比你当前能力更复杂的任务。去过也做过。许多公司都这样做。

    关于您的任务,对于搜索,每个网站通常使用GET 进行搜索,例如https://www.google.co.in/search?q=stackoverflow。注意q=stackoverflow 部分。为此,您无需提交任何表单,直接使用 URL 即可。

    因此,我建议您访问要直接链接的网站进行搜索。搜索一些示例文本,说 TEST 并观察打开页面的 URL。

    然后使用 JavaScript/jQuery/etc,点击徽标,阅读您想要搜索的任何文本,在 JavaScript 中创建一个 URL 并在那里redirect

    这是总体思路,欢迎评论并询问您是否在实施中遇到任何问题。

    【讨论】:

      猜你喜欢
      • 2011-03-20
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 2018-11-15
      • 1970-01-01
      相关资源
      最近更新 更多