开发者

I have a search results page but I don't know how to mix php and html!? How?

开发者 https://www.devze.com 2023-03-30 21:37 出处:网络
I have a search results page and I want to display it in tables. how do I do so? I am completely unsure of how to mix php together with html.

I have a search results page and I want to display it in tables. how do I do so? I am completely unsure of how to mix php together with html.

My code is:

<?php
if(strlen(trim($_POST['search'])) > 0) {

$search = "%" . $_POST["search"] . "%";
$searchterm = "%"开发者_如何学C . $_POST["searchterm"] . "%";

mysql_connect ("", "", "");
mysql_select_db ("");
if (!empty($_POST["search_string"])) 
{ 

}  

$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND 
location      LIKE '%$searchterm%'";

$result = mysql_query ($query);
if ($result) {
while ($row = mysql_fetch_array ($result)) {
<center>
<table height="20" width="990" cellpadding="0" cellspacing="0">
<tr>
<td>

<table height="20" width="215" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="lucidagrande" size="4" color="#045FB4">$row[0]</font>
</td>
</tr>
</table>

</td>
<td>

<table height="20" width="775" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="lucidagrande" size="4" color="black">$row[1]</font>
<td align="right">
<font face="lucidagrande" size="2" color="red">See More...</font>
</td>
</tr>
</table>

</td>
</tr>
</table>
}
}
}
?>

Thanks!

James


This is one option:

<?php
if(strlen(trim($_POST['search'])) > 0) {

$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";

mysql_connect ("", "", "");
mysql_select_db ("");
if (!empty($_POST["search_string"])) 
{ 

}  

$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND 
location      LIKE '%$searchterm%'";

$result = mysql_query ($query);
if ($result) {
while ($row = mysql_fetch_array ($result)) { ?>
<center>
<table height="20" width="990" cellpadding="0" cellspacing="0">
<tr>
<td>

<table height="20" width="215" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="lucidagrande" size="4" color="#045FB4"><?php echo $row[0]; ?></font>
</td>
</tr>
</table>

</td>
<td>

<table height="20" width="775" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="lucidagrande" size="4" color="black"><?php echo $row[1]; ?></font>
<td align="right">
<font face="lucidagrande" size="2" color="red">See More...</font>
</td>
</tr>
</table>

</td>
</tr>
</table>
<?php
}
}
}
?>


PHP parsing is based on the existence of two tags:

<?php
?>

Anything inside the PHP "xml" tags is to be PHP code. Anything outside is to be normal HTML. So:

<p><?php echo 'Hello, world!'; ?></p>

Means to output the <p></p> normally, but let the PHp parser handle the echo 'Hello, world!';

So, changes are that after your while(...){ statement you want a ?> (to end PHP code and begin outputting the <center> tag and tables.) Then, towards the bottom, you'll need a <?php before the braces (}).

Finally, every time you see a variable like $row[0] you'll need to surround it with php tags. either of these is acceptable:

<?=$row[0];?>
<?php echo $row[0]; ?>


You have to end the PHP before you can start the HTML, and vice versa.

<?php
// PHP goes here
?>
<!-- HTML goes here -->
<?php
// More PHP here
?>


Put the php tags where php code is e.g.

<font face="lucidagrande" size="4" color="black"><?php echo $row[1]; ?></font>
0

精彩评论

暂无评论...
验证码 换一张
取 消