开发者

<div title="Can I somehow put PHP code in this attribute?"></div> or is there another route I can take?

开发者 https://www.devze.com 2023-04-12 23:53 出处:网络
I want to: Read in text line from \"textfile.txt\". \'echo\' that line to the page in a <div> element.

I want to:

  1. Read in text line from "textfile.txt".
  2. 'echo' that line to the page in a <div> element.
  3. Read in a text line from "namefile.txt".
  4. Make this line become some sort of pop-up-text for that <div> element.

My script:

<? PHP
$fhtext = fopen("textfile.txt","a+") or exit("Error 1");
$fhname = fopen("namefile.txt","a+") or exit("Error 2");

while(!feof($fhtext))
  {
  echo "<div title="HERE IS WHERE I AM STUCK">".fgets($fht开发者_开发知识库ext)."<div/><br />";
  }

Could I perhaps go:

echo "<div title="<? fgets($fhname) ?>".fgets($fhtext)."<div/><br />";

?


<?php
$fhtext = fopen("textfile.txt","a+") or exit("Error 1");
$fhname = fopen("namefile.txt","a+") or exit("Error 2");

while(!feof($fhtext) && !feof($fhname))
{
    echo "<div title=\"", fgets($fhname), "\">", fgets($fhtext), "<div/><br />";
}
?>


I haven't used PHP in a long time, but this should work:

echo "<div title='" . fgets($fhname) ."'>" .fgets($fhtext). "<div/><br />";


Regarding:

  1. Make this line become some sort of pop-up-text for that '' element.

If you mean 'popup' text, as in tooltips of the type you get when you hover over links/images, this is only available on some elements when their title attribute has been set, not DIVs.

As such you can either change the DIV to a A (link) element. Or use Javascript to detect a hover over the DIV and display a popup.


If you are sure both files have the same number of lines you could use the „file“-function of PHP. This will read the file into an array and you can loop over it with a for-loop:

<?php
$file1 = file('file1');
$file2 = file('file2');
for ($i = 0, $max = count($file1); $i < $max; $i++) {
    echo $file1[$i].' '.$file2[$i];
}


Before you dump your fgets() data to the browser, you really ought to HTML encode it first. That will prevent accidental (or not so accidental) problems caused by HTML fragments that might be in your text files, or if the file name can be entered by the user (either as part of the URL or as part of a form).

As a rule of thumb, always HTML encode anything coming from a data source you don't control before spitting it out to the browser. That includes form fields, etc.

0

精彩评论

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

关注公众号