简体   繁体   English

将 echo 插入到具有 id 或 class 的特定 html 元素中,例如 div

[英]insert echo into the specific html element like div which has an id or class

I have this code.我有这个代码。

<html>
<head>
<style type="text/css">
body{background:#666666;}
div{border:1px solid red;}
</style>
</head>
<body>
<?php

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("juliver", $con);

$result = mysql_query("SELECT * FROM hi");

while($row = mysql_fetch_array($result))
{

echo '<img src="'.$row['name'].'" />';  
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
}

mysql_close($con);

?>
</body>
</html>

the above code works.上面的代码有效。

now, I want to insert this现在,我想插入这个

echo '<img src="'.$row['name'].'" />'; 
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";

into the specified div or other element in the html, example, i will insert this到指定的 div 或 html 中的其他元素,例如,我将插入这个

echo '<img src="'.$row['name'].'" />';

into the html element .进入 html 元素。

I dont know how to do this, please help me.我不知道该怎么做,请帮助我。 Thanks谢谢

Juliver朱利文

The only things I can think of are我能想到的只有

  1. including files包括文件
  2. replacing elements within files using preg_match_all使用 preg_match_all 替换文件中的元素
  3. using assigned variables使用分配的变量

I have recently been using str_replace and setting text in the HTML portion like so我最近一直在使用 str_replace 并像这样在 HTML 部分设置文本

{{TEXT_TO_REPLACE}}

using file_get_contents() you can grab html data and then organise it how you like.使用 file_get_contents() 你可以抓取 html 数据,然后按照你喜欢的方式组织它。

here is a demo这是一个演示

myReplacementCodeFunction(){
  $text = '<img src="'.$row['name'].'" />'; 
  $text .= "<div>".$row['name']."</div>";
  $text .= "<div>".$row['title']."</div>";
  $text .= "<div>".$row['description']."</div>";
  $text .= "<div>".$row['link']."</div>";
  $text .= "<br />";
  return $text;
}

$htmlContents = file_get_contents("myhtmlfile.html");
$htmlContents = str_replace("{{TEXT_TO_REPLACE}}", myReplacementCodeFunction(), $htmlContents);
echo $htmlContents;

and now a demo html file:现在是一个演示 html 文件:

<html>
<head>
   <style type="text/css">
      body{background:#666666;}
      div{border:1px solid red;}
   </style>
  </head>
  <body>
    {{TEXT_TO_REPLACE}}
  </body>
</html>

if yo want to place in an div like i have same work and i do it like如果你想像我有同样的工作一样放在一个 div 中,我喜欢这样做

<div id="content>
<?php
while($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['name'].'" />';  
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
}
?>
</div>

refer to the basic.参考基本。

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

You can write the php code in another file and include it in the proper place where you want it.您可以在另一个文件中编写 php 代码,并将其包含在您想要的适当位置。

AJAX is also used to display HTML content that is formed by PHP into a specified HTML tag. AJAX 还用于显示由 PHP 形成的 HTML 内容,并将其转换为指定的 HTML 标记。

Using jQuery:使用jQuery:

$.ajax({url: "test.php"}).done(function( html ) {
    $("#results").append(html);
});

Above code will execute test.php and result will be displayed in the element with id results .上面的代码将执行test.php并且结果将显示在 id results的元素中。

There is no way that you can do it in PHP when HTML is already generated.当 HTML 已生成时,您无法在 PHP 中执行此操作。 What you can do is to use JavaScript or jQuery:你可以做的是使用 JavaScript 或 jQuery:

document.getElementById('//ID//').innerHTML="HTML CODE";

If you have to do it when your URI changes you can get the URI and then split it and then insert the HTML in script dynamically:如果您必须在 URI 更改时执行此操作,您可以获取 URI,然后将其拆分,然后在脚本中动态插入 HTML:

var url = document.URL;
// to get url and then use split() to check the parameter

没有办法用 php 专门针对一个元素,您可以在 div 标签之间嵌入 php 代码,也可以使用更长的 jquery。

Well from your code its clear that $row['name'] is the location of the image on the file, try including the div tag like this好吧,从您的代码中可以清楚地看出 $row['name'] 是图像在文件上的位置,请尝试像这样包含 div 标签

echo '<div>' .$row['name']. '</div>' ;

and do the same for others, let me know if it works because you said that one snippet of your code is giving the desired result so try this and if the div has some class specifier then do this并对其他人做同样的事情,让我知道它是否有效,因为你说你的代码的一个片段给出了想要的结果,所以试试这个,如果 div 有一些类说明符,那么做这个

echo '<div class="whatever_it_is">' . $row['name'] . '</div'> ;

You have to put div with single quotation around it.您必须在 div 周围加上单引号。 ' ' the Div must be in the middle of single quotation . ' ' Div必须在单引号的中间。 i'm gonna clear it with one example :我将用一个例子来清除它:

<?php
        echo '<div id="output">'."Identify".'<br>'.'<br>';
        echo 'Welcome '."$name";
        echo "<br>";
        echo 'Web Mail: '."$email";
        echo "<br>";
        echo 'Department of '."$dep";
        echo "<br>";
        echo "$maj";
        '</div>'
?>

hope be useful.希望有用。

Have you tried this?:你试过这个吗?:

    $string = '';
    while($row = mysql_fetch_array($result))
    {
    //this will combine all the results into one string
    $string .= '<img src="'.$row['name'].'" />
                <div>'.$row['name'].'</div>
                <div>'.$row['title'].'</div>
                <div>'.$row['description'].'</div>
                <div>'.$row['link'].'</div><br />';

    //or this will add the individual result in an array
 /*
     $yourHtml[] = $row;
*/
    }

then you echo the $tring to the place you want it to be然后你将 $tring 回显到你想要的地方

<div id="place_here">
   <?php echo $string; ?>
   <?php 
     //or
    /*
      echo '<img src="'.$yourHtml[0]['name'].'" />;//change the index, or you just foreach loop it
    */
    ?>
</div>

You can repeat it by fetching again the data您可以通过再次获取数据来重复它

 while($row = mysql_fetch_assoc($result)){
 //another html element
 <div>$row['name']</div>
 <div>$row['title']</div>
 //and so on
 }

or you need to put it on the variable and call display it again on other html element或者你需要把它放在变量上并在其他 html 元素上再次调用显示它

 $name = $row['name'];
 $title = $row['title']
 //and so on

then put it on the other element, but if you want to call all the data of each id, you need to do the first code然后放到另一个元素上,但是如果要调用每个id的所有数据,就需要做第一个代码

I use this or similar code to inject PHP messages into a fixed DIV positioned in front of other elements (z-index: 9999) just for convenience at the development stage.我使用这个或类似的代码将 PHP 消息注入到位于其他元素 (z-index: 9999) 前面的固定 DIV 中,只是为了在开发阶段方便。

Each PHP message passes into my 'custom_message()' function and is further conveyed into the innard of preformatted DIV created by echoed JS.每条 PHP 消息都传递到我的“custom_message()”函数中,并进一步传送到由 echoed JS 创建的预格式化 DIV 的内部。

There can be as many as it gets, all put inside that fixed DIV, one under the other.可以有尽可能多的,都放在固定的 DIV 中,一个在另一个下面。

<style>
    #php_messages {
        position: fixed;
        left: 0;
        top: 0;
        z-index: 9999;
    }
    .php_message {
        background-color: #333;
        border: red solid 1px;
        color: white;
        font-family: "Courier New", Courier, monospace;
        margin: 1em;
        padding: 1em;
    }
</style>

<div id="php_messages"></div>

<?php
    function custom_message($output) {
        echo
            '
                <script>
                    var
                        el = document.createElement("DIV");
                    el.classList.add("php_message");
                    el.innerHTML = \''.$output.'\';
                    document.getElementById("php_messages").appendChild(el);
                </script>
            ';
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM