doulu8446 2015-01-21 19:11
浏览 44
已采纳

PHP MySQL记分板,带有配置文件页面的链接

I have a wordpress site with a scoreboard that gets results from a view in the wp database and I ve set up the board on a custom page template.My trouble is to link the players in the scoreboard to a new page with players stats etc. I want to be able to click the player name in the scoreboard and then get to a page with stats for that player.

Here is the relevant code that displays the scoreboard:

<?php //Table with SQL Query
$connection = mysql_connect('localhost', 'root', ''); 
mysql_set_charset('utf8',$connection);
mysql_select_db('worldsbestblog');

$query = "SELECT * FROM Sammenlagt LIMIT 0,20"; 
$result = mysql_query($query); 

while($row = mysql_fetch_array($result)){   //Loop through results
    echo    "<tr><td>" . $row['Dato'] . "</td>
            <td>
                <a href=\"fasan.php?id=". $row["spillerid"]."\">" . $row['Navn'] . "</a>
            </td>
            <td>" . $row['hull1'] . "</td><td>" . $row['hull2'] . 
            "</td><td>" . $row['hull3'] . "</td><td>" . $row['hull4'] . 
            "</td><td>" . $row['hull5'] . "</td><td>" . $row['hull6'] . 
            "</td><td>" . $row['hull7'] . "</td><td>" . $row['hull8'] . 
            "</td><td>" . $row['hull9'] . "</td><td>" . $row['hull10'] . 
            "</td><td>" . $row['hull11'] . "</td><td>" . $row['hull12'] . 
            "</td><td>" . $row['hull13'] . "</td><td>" . $row['hull14'] . 
            "</td><td>" . $row['hull15'] . "</td><td>" . $row['hull16'] . 
            "</td><td>" . $row['hull17'] . "</td><td>" . $row['hull18'] . 
            "</td><td>" . $row['Sammenlagt'] . "</td><td>" . $row['Par'] .
            "</td><td>" . $row['Poeng'] . "</td></tr>";  //$row['index'] the index here is a column name
            }
?>

I've seen countless similar questions where SESSIONS, $_POST and $_GET has been used though I havent been able to either understand or see how/if thats what's missing in order to make it work. As it is now i get a URL showing the userid of player, but when clicked, wordpress cant find that page.


[edit]
OK I have done some editing and found an answer to my question. Unsure how to go about here on stackoverflow (direct me if I should do this elsewhere), but I want to show you what did the job so that the next guy will find this post with an explanation.

So here is the current code and thanks to @Cal Evans and @Hamed Momeni for helpful guidance:

Scoreboard goes:

$query = "SELECT * FROM Sammenlagt";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through     results
echo    "<tr><td>" . $row['Dato'] . "</td>
    <td>
        <a href=\"/wordpress/teststats?id=". $row["spillerid"]."\">" . $row['Navn'] . "</a>
    </td>
    <td>" . $row['hull1'] . "</td><td>" . $row['hull2'] . 
    "</td><td>" . $row['hull3'] . "</td><td>" . $row['hull4'] . 
    "</td><td>" . $row['hull5'] . "</td><td>" . $row['hull6'] . 
    "</td><td>" . $row['hull7'] . "</td><td>" . $row['hull8'] . 
    "</td><td>" . $row['hull9'] . "</td><td>" . $row['hull10'] . 
    "</td><td>" . $row['hull11'] . "</td><td>" . $row['hull12'] . 
    "</td><td>" . $row['hull13'] . "</td><td>" . $row['hull14'] . 
    "</td><td>" . $row['hull15'] . "</td><td>" . $row['hull16'] . 
    "</td><td>" . $row['hull17'] . "</td><td>" . $row['hull18'] . 
    "</td><td>" . $row['Sammenlagt'] . "</td><td>" . $row['Par'] .
    "</td><td>" . $row['Poeng'] . "</td></tr>";  //$row['index'] the index here is a column name
}
?>

.../wordpress/teststats is a new page with a custom page template with the following code:

<?php

$spid = (int) filter_input(INPUT_GET,'id', FILTER_SANITIZE_NUMBER_INT);

$spillerquery = mysql_query("SELECT * FROM Sammenlagt WHERE spillerid='$spid'") or die ("Somthing went wrong!!"); 
    if (mysql_num_rows($spillerquery) < 1){
        die ("Couldnt find that player");
    }
    $navnequery = mysql_query("SELECT Navn FROM Sammenlagt WHERE spillerid='$spid'") or die ("Player does not exist");
    $spillernavn = mysql_result($navnequery,MYSQL_BOTH);
?>

<h2>Her er <?php echo "$spillernavn"; ?> sine resultater</h2>

<?php
    while($row = mysql_fetch_array($spillerquery, MYSQL_ASSOC)){
    echo    "<tr><td>" . $row['Dato'] . 
            "</td><td>" . $row['hull1'] . "</td><td>" . $row['hull2'] . 
            "</td><td>" . $row['hull3'] . "</td><td>" . $row['hull4'] . 
            "</td><td>" . $row['hull5'] . "</td><td>" . $row['hull6'] . 
            "</td><td>" . $row['hull7'] . "</td><td>" . $row['hull8'] . 
            "</td><td>" . $row['hull9'] . "</td><td>" . $row['hull10'] . 
            "</td><td>" . $row['hull11'] . "</td><td>" . $row['hull12'] . 
            "</td><td>" . $row['hull13'] . "</td><td>" . $row['hull14'] . 
            "</td><td>" . $row['hull15'] . "</td><td>" . $row['hull16'] . 
            "</td><td>" . $row['hull17'] . "</td><td>" . $row['hull18'] . 
            "</td><td>" . $row['Sammenlagt'] . "</td><td>" . $row['Par'] .
            "</td><td>" . $row['Poeng'] . "</td></tr>";
    }   

?>
  • 写回答

2条回答 默认 最新

  • douhan9748 2015-01-21 19:35
    关注

    Try this.

    <?php 
    //Table with SQL Query
    
    $player_id = (int) filter_input(INPUT_GET,'id', FILTER_SANITIZE_NUMBER_INT);
    
    $connection = mysql_connect('localhost', 'root', ''); 
    mysql_set_charset('utf8',$connection);
    mysql_select_db('worldsbestblog');
    
    $query = "SELECT * FROM Sammenlagt where player_id = {$player_id} LIMIT 0,20"; 
    
    $result = mysql_query($query);
    

    Although, I do agree with @Hamed that you should be using $wpdb instead of creating your own connection. This is assuming that your table is in the same database as your WordPress tables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?