简体   繁体   English

使用javascript / jquery-在php LOOP中设置div的值/文本

[英]Set the value/text of a div using javascript/jquery - inside a php LOOP

I want to set the value/text of a div using javascript/jquery inside a loop but I don't know how to implement it. 我想在循环内使用javascript / jquery设置di​​v的值/文本,但我不知道如何实现它。 I need help with this one guys. 我需要这个家伙的帮助。

Objectives: 目标:

  1. Retrieve data from database. 从数据库检索数据。
  2. Set the value of an element using javascript/jquery (inside a loop) from the database. 使用javascript / jquery(在循环内)从数据库中设置元素的值。
  3. Make the value a link 将值链接

I have this a_link column from links table with the ff. 我在ff links表中有此a_link列。 values: 值:

- www.google.com

- https://www.google.com

- www.stackoverflow.com

And here is my code: 这是我的代码:

<?php
    $querylink = "SELECT * from links";
    $resultlink = mysql_query($querylink);

    while ($rowlink = mysql_fetch_array($resultlink))
    { 
     $thelink = $rowlink['a_link'];
?>

     <div class = "row">
         <span id = "linkhere"></span> 
     </div>

     <script>

        var link = "<?php echo $thelink; ?>";
        $("#linkhere").html(urlify(link));

        function urlify(text) {
           var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
           //var urlRegex = /(https?:\/\/[^\s]+)/g;
           return text.replace(urlRegex, function(url,b,c) {
               var url2 = (c == 'www.') ?  'http://' +url : url;
              // return '<span style = "color:blue;text-decoration:underline">' + url + '</span>';
              return '<a href="' +url2+ '" target="_blank">' + url + '</a>';
          }) 
        }

     </script>
<?php
    }
?>

Any help would be highly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

That's not how it works, that's not how any of this works 这不是它的工作原理,也不是任何一个工作原理

Now let's assume that you really need to use Javascript to process your generated links (which is not). 现在,假设您确实需要使用Javascript处理生成的链接(不是)。

You first need to separate your Javascript code from your PHP code. 首先,您需要将Javascript代码与PHP代码分开。 You will only use Javascript once you have fetched your data and generated some output. 仅在获取数据并生成一些输出后,才使用Javascript。

I guess you just want some kind of working code 我想你只想要某种工作代码

<?php
    $querylink = "SELECT * from links";
    $resultlink = mysql_query($querylink);

    while ($rowlink = mysql_fetch_array($resultlink)) : 
     $link = $rowlink['a_link'];
?>
     <div class="row">
         <a href="" data-url="<?php echo $link ?>"></a>
     </div>
<?php 
    endwhile;
?>

<script type="text/javascript">
    $(function() {
        $('.row a').each(function() {
            var urlified = urlify($(this).data('url'));
            $(this).attr('href', urlified.url)
                   .text(urlified.label);
        });
    });

    function urlify(text) {
        var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
        return text.replace(urlRegex, function(url,b,c) {
          var label = (c == 'www.') ?  'http://' +url : url;
          return {url: url, label: label};
        }); 
    }
</script>

@aimme is technically not wrong about using a different database library. @aimme从技术上讲关于使用其他数据库库是没有错的。 Please read " Why shouldn't I use mysql_* functions in PHP? " for reasons why not to use mysql_ and for some neat alternatives, some tutorials, and some good reads. 请阅读“ 为什么我不应该在PHP中使用mysql_ *函数? ”,以了解为什么不使用mysql_的原因,以及一些简洁的替代方法,一些教程和一些不错的读物。 (yes, all in the same page! just scroll down) (是的,全部在同一页面上!只需向下滚动)

I think you're trying to: 我认为您正在尝试:

  • display a <div> of class 'row' 显示类'row'的<div>
    • with an <a> tag inside that uses the 'a_link' column of the 'links' table as the href and the label. 内部带有<a>标记,该标记将“链接”表的“ a_link”列用作href和标签。
      • The href for the tag must always have a scheme (http://). 标签的href必须始终具有方案(http://)。

Just PHP and HTML 只是PHP和HTML

<?php
    $querylink = "SELECT * from links";
    $resultlink = mysql_query($querylink);

    while ($rowlink = mysql_fetch_array($resultlink))
    { 
        $theLink= $rowlink['a_link'];
        $regexMatches = array();

        // removed (what seemed to be) needless groups in regex
        $urlFound = preg_match("@((https?:\/\/|www\.)[^\s]+)@",$theLink,$regexMatches);

        if($urlFound === 1) {
            // only add http:// if http:// was not detected
            $href = ($regexMatches[2] === "www." ? "http://" : "") . $theLink;
?>
    <div class="row">
        <a href="<?php echo $href; ?>" target="_blank"><?php echo $theLink; ?></a>
    </div>
<?php  }
    }
?>

This code won't echo a row if a_link doesn't contain either 'http://' or 'www.' 如果a_link不包含“ http://”或“ www”,则此代码不会回显一行。 in it. 在里面。 so google.com will not be displayed. 因此不会显示google.com。

Of note, as written, the regex will work on "urls" like 'applewww.google.com'. 值得注意的是,如前所述,正则表达式将可在“ url”(如“ applewww.google.com”)上使用。 Don't know if that matters. 不知道这是否重要。 Adding a '^' to the beginning of the regex may solve the problem (like so: preg_match("@^((https?:\\/\\/|www\\.)[^\\s]+)@",$theLink,$regexMatches); ) 在正则表达式的开头添加“ ^”可以解决问题(例如: preg_match("@^((https?:\\/\\/|www\\.)[^\\s]+)@",$theLink,$regexMatches);

A (better|different) solution could use parse_url($url) (更好)解决方案可以使用parse_url($ url)

<?php
    $querylink = "SELECT * from links";
    $resultlink = mysql_query($querylink);

    while ($rowlink = mysql_fetch_array($resultlink))
    { 
        $theLink= $rowlink['a_link'];

        $href = (parse_url($theLink,PHP_URL_SCHEME) === NULL ? "http://" : "") . $theLink;
    ?>
    <div class="row">
        <a href="<?php echo $href; ?>" target="_blank"><?php echo $theLink; ?></a>
    </div>
    <?php
    }
?>

However, using parse_url() would mean any old string would be displayed (while the first solution would not display any links that didn't have either http:// or www.) but since your pulling from a table called 'links' it's probably safe to assume everything is a valid path. 但是,使用parse_url()意味着将显示任何旧字符串(而第一个解决方案将不显示任何没有http://或www。的链接),但是由于从名为“ links”的表中提取,假设一切都是有效的路径,也许是安全的。

First i want to advice that use PDO or mysqli instead of mysql. 首先,我想建议使用PDO或mysqli而不是mysql。 as it is vulnerable to sql injection and its depreciated. 因为它容易受到sql注入的影响,并且已贬值。

"I want to set the value/text of a div using javascript/jquery inside a loop but I don't know how to implement it. I need help with this one guys." “我想在循环内使用javascript / jquery来设置div的值/文本,但我不知道如何实现它。我需要这方面的帮助。”

for that i would say Php is a server side language whereas javascript is a client side language. 为此,我会说Php是服务器端语言,而javascript是客户端语言。 and Ajax is the way to manipulate client side from server vice versa, without refreshing the whole page. Ajax是从服务器反过来从服务器操作客户端的方法,而无需刷新整个页面。

below is just a demonstration that i edited little bit from your code to show the separation of server side and client side code and to just give an idea how it works.I don't know whether the code will work or not. 下面只是一个示例,我对您的代码进行了一些编辑,以展示服务器端代码和客户端代码的分离,并给出了其工作原理的信息。我不知道代码是否有效。 haven't tested. 还没测试 php code (server side) will be executed first but could control the display of it using javascript(client side) functions inside document.ready() or window.load() to apply the affects as soon as possible.Through this we could bring changes to the links that we want before its being shown to the client . php代码(服务器端)将首先执行,但可以使用document.ready()window.load() javascript(客户端)函数控制其显示,以便尽快应用效果。更改我们需要的链接,然后才显示给客户。 For each of the link retrieved and displayed you could use a specific class and jquery .each() function to apply certain fix to the selected link as Lyes BEN mentioned above or all the elements with a specific class could be manipulated as a whole without using .each. 对于检索和显示的每个链接,您都可以使用特定的类和jquery .each()函数对选定的链接应用特定的修复,因为上述Lyes BEN或具有特定类的所有元素都可以作为一个整体进行操作,而无需使用。每。

<?php
    $querylink = "SELECT * from links";
    $resultlink = mysql_query($querylink);

    while ($rowlink = mysql_fetch_array($resultlink))
    { 
     $thelink = $rowlink['a_link'];
     echo '<div class = "row">
                <span id = "linkhere">
                    <a href="'.$thelink.'"></a>
                 </span> 
           </div>';
    }
?>
     <script>
        $("#linkhere a").html(urlify(link));

        function urlify(text) {
           var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
           //var urlRegex = /(https?:\/\/[^\s]+)/g;
           return text.replace(urlRegex, function(url,b,c) {
               var url2 = (c == 'www.') ?  'http://' +url : url;
              // return '<span style = "color:blue;text-decoration:underline">' + url + '</span>';
              return '<a href="' +url2+ '" target="_blank">' + url + '</a>';
          }) 
        }

     </script>

You can implement this using php with parse_url function ( http://php.net/manual/en/function.parse-url.php ) to get different components. 您可以使用带有parse_url函数( http://php.net/manual/en/function.parse-url.php )的php实现此功能,以获取不同的组件。

In parse_url, there is 'scheme' key for http or https. 在parse_url中,有用于http或https的“方案”键。

Then to do this with php, just call formatUrl function to make the url 然后使用php进行此操作,只需调用formatUrl函数以使url

<?php 
 function formatUrl($url)
 {
    $urlData = parse_url($url);

    if(!isset($urlData['scheme'])) {
        $url = 'http://' . $url;
    }

    return '<a href="' . $url . '" target="_blank">' . $url . '</a>';
 }
?>

<?php
 $querylink = "SELECT * from links";
 $resultlink = mysql_query($querylink);

 while ($rowlink = mysql_fetch_array($resultlink))
 { 
  $thelink = $rowlink['a_link'];
 ?>

 <div class = "row">
     <span id="linkhere"><?php echo formatUrl($thelink)?></span> 
 </div>
 <?php
 }
?>

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

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