Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

into the html element .

I dont know how to do this, please help me. Thanks

Juliver

like image 528
Juliver Galleto Avatar asked Mar 26 '12 02:03

Juliver Galleto


People also ask

How do you write an echo in html?

echo '<head>', "\n"; echo '</head>', "\n"; echo '<body>', "\n"; echo '</body>', "\n"; echo '</html>', "\n"; php. html. templates.

How to echo html div in php?

php echo "<h1> Hello PHP. </h1>"; if(isset($_GET['formHELPbutton'])) { echo "Hello Form Help."; echo "<div id='formHELP' style='display: block'>"; } if(isset($_GET['formFEEDBACKbutton'])) { echo "Hello Form Feedback."; echo "<div id='formFEEDBACK' style='display: block'>"; } ?>

How do I echo a class in php?

$str = (string)$class; The example linked is as follows: <? php // Declare a simple class class TestClass { public $foo; public function __construct($foo) { $this->foo = $foo; } public function __toString() { return $this->foo; } } $class = new TestClass('Hello'); echo $class; ?>

How to call div in php?

click(function(){ var id = split($(this). attr('id'). split("_")[1];//get the php index from the id //do something with the id, e.g. ajax or whatever });


2 Answers

if yo want to place in an div like i have same work and i do it like

<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>
like image 159
najeeb k rahman Avatar answered Sep 19 '22 20:09

najeeb k rahman


The only things I can think of are

  1. including files
  2. replacing elements within files using preg_match_all
  3. using assigned variables

I have recently been using str_replace and setting text in the HTML portion like so

{{TEXT_TO_REPLACE}}

using file_get_contents() you can grab html data and then organise it how you like.

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>
<head>
   <style type="text/css">
      body{background:#666666;}
      div{border:1px solid red;}
   </style>
  </head>
  <body>
    {{TEXT_TO_REPLACE}}
  </body>
</html>
like image 40
Adsy2010 Avatar answered Sep 18 '22 20:09

Adsy2010