doumouyi4039 2015-12-09 05:43
浏览 43

我可以在PHP函数获取的元素列表中创建anchorpoints吗?

I'm fairly new to coding, but have managed to create a function that gets all my elements in the database and echos them out in a list. However, I want each element to have an individual anchorpoint so that i can use the "#" in my link to refer to a specific element.

My whole function currently looks like this (Please excuse some strange names as they're in norwegian):

function visAktiveForslagFlestStemmer()
{
    echo "<section id='innhold'><h2>Aktive forslag</h2>";
    $db = returnerDB();
    if(!$db)
    {
        die("Kunne ikke knytte til databasen");   
    }
    $sql = "SELECT Tittel, Tekst, COUNT(stemme.ForslagID) AS Antall FROM forslag, stemme WHERE  stemme.ForslagID = forslag.ForslagID AND forslag.Aktiv = 1 AND forslag.Besvart IS NULL GROUP BY stemme.ForslagID ORDER BY Antall DESC";

    $resultat = $db->query($sql);

    $i=1;
    $index=1;

    while ($rad = mysqli_fetch_array($resultat, MYSQLI_ASSOC))
    {

        echo "<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js'></script>
        <script>
        $(document).ready(function(){
            $('#beskrivelse".$index."').hide();
            $('#visSkjulButton".$index."').click(function(){
                $('#beskrivelse".$index."').toggle(500, 'swing');
            });
        });
        </script>";

        if ($i%2==0)
        {
            $tall="repeteres";
            $ftall="forslag";
            $sstall="stemmeOgSosialt";
            $stall="stemme";
            $sotall="sosialt";

        }
        else
        {
            $tall="repeteres2";
            $ftall="forslag2";
            $sstall="stemmeOgSosialt2";
            $stall="stemme2";
            $sotall="sosialt2";

        }
        echo "<a id='anchor".$i."'><div class='".$tall."'><div class='".$ftall."'><p><h3>".$rad["Tittel"]."</h3></p><div id='beskrivelse".$index."'>
        <p id='forslagbeskrivelse'>".$rad["Tekst"]."</p></div><button class='btn btn-info' id='visSkjulButton".$index."'>Vis/ Skjul</button></div><div class='".$sstall."'><div class='".$stall."'>
        <p><div class='btn btn-primary btn-lg center-block disabled'>".antallStemmer($rad["ForslagID"])."</div></p>
        <br><br><div class='btn btn-primary btn-lg center-block'>Avgi Stemme</div></div>
        <div class='".$sotall."'><a href='http://facebook.com' class='image1'></a><a href='http://twitter.com' class='image2'></a><a href='http://plus.google.com' class='image3'></a>
        <div class='btn btn-warning btn-md' id='rapporterButton'>Rapportér</div></div></div></a>";
        $i++;
        $index++;
    }
    mysqli_close($db);

This function is called in a separate page, which is included in my index.php-page. My index.php-page also includes a navigation bar, assigning [mywebpage.com/index.php]?page=1 to the end of the url where the function is being called.

The issue I'm having is that adding for example "#anchor1" to this link does not make my browser window jump to the first (or any) element that this function gets from the database.

Can anyone please pont to where my error lies?

  • 写回答

1条回答 默认 最新

  • douluo6626 2015-12-09 23:38
    关注

    In case anyone else has issues wit this, the simple solution was to echo this:

    echo "<a id='forslag".$rad["ForslagID"]."'><div class='".$tall."'></a><div class='".$ftall."'><p><h3>".$rad["Tittel"]."</a></h3></p><div id='beskrivelse".$index."'>
            <p id='forslagbeskrivelse'>".$rad["Tekst"]."</p></div><button class='btn btn-info' id='visSkjulButton".$index."'>Vis/ Skjul</button></div><div class='".$sstall."'><div class='".$stall."'>
            <p><div class='btn btn-primary btn-lg center-block disabled'>".antallStemmer($rad["ForslagID"])."</div></p>
            <br><br><div class='btn btn-primary btn-lg center-block'>Avgi Stemme</div></div>
            <div class='".$sotall."'>
            <a href='https://www.facebook.com/sharer/sharer.php?u=localhost/TestM/index.php?page=1#forslag".$rad["ForslagID"]."' target='_blank' class='image1'></a>
            <a href='https://twitter.com/home?status=St%C3%B8tt%20forslaget%20til%20NHHS%20her%3A%20http%3A//localhost/TestM/index.php?page=1%23".$rad["ForslagID"]."' target='_blank' class='image2'></a>
            <a href='https://plus.google.com/share?url=nhhs.no' target='_blank' class='image3'></a>
            <div class='btn btn-warning btn-md' id='rapporterButton'>Rapportér</button></div></div></div>";
    

    In other words to just surround my first <div> with the anchor point.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100