duanjuelu8874 2013-07-16 21:37
浏览 9
已采纳

PHP从While循环中分离内容?

Okay so my code below echoes out two URLs from the database but I want to have the ability to separate the two and update the row where the image is clicked. Example: Two images are output, users clicks image on the left, MySQL knows which row to update depending on which image they clicked. Depending on the image they click it will update the image's votes in MySQL. Ideally I want the "vote-ups" to show in place of the 112 and 156 accordingly from the database.

Let me rephrase to clarify: my code echoes out two images selected from the database. The code should tell you that if you read it. From there, I want to be able to separate the two images so I can do a query to update the specific row with that image and update the total votes that image has from votes to votes+1. I tried doing this but it would update both row's votes by 1.

How can I do this?

<?php
    include_once('db.php');

    $selectURLSQL = "SELECT * FROM `urls` ORDER BY RAND() LIMIT 3";
    $selectURLQuery = mysql_query($selectURLSQL);
    $URL_Row = mysql_fetch_array($selectURLQuery);
?>

<?php
    while($URL_Row = mysql_fetch_array($selectURLQuery)) {
?>

<img class="img" name="img" src="<?php echo $URL_Row['url']; ?>" style="height:400px;width: 260px;">&nbsp;&nbsp;&nbsp;&nbsp;

<?php 
    } 
?>
<br /><br />
<span style="font-size:18px;font-family:trebuchet ms;">156 Vote-Ups</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<small>Vs.</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span style="font-size: 18px; font-family: trebuchet ms;">112 Vote-Ups</span><br /><br />
  • 写回答

1条回答 默认 最新

  • duanbipu7601 2013-07-17 01:00
    关注

    Say your table urls is structured like this:

    id  |  url  |  rank
    

    you have to increment the rank column for each click on the image corresponding to the url.

    my idea is to create a link around the image, and this link will contain the id of the image so it can be passed in the URL like this ?id=xxx

    if you want to show the votes outside the loop, you can save them temporarily in an array

    Your Code:

    <?php 
        include_once('db.php');
    
        $selectURLSQL = "select * from urls ORDER BY RAND() LIMIT 2";
        $selectURLQuery = mysql_query($selectURLSQL);
    
        if (isset($_GET['id'])){
            $id = $_GET['id'];
            $sql = "UPDATE urls set rank = rank+1 WHERE id = $id";
            mysql_query($sql);
        }
    
        $i = 0;
        $votes = array();
        while ($URL_Row = mysql_fetch_array($selectURLQuery)):
            $votes[$i++] = $URL_Row['rank'];
        ?>
            <a href="?id=<?php echo $URL_Row['id']; ?>">
                <img class="img" name="img" src="<?php echo $URL_Row['url']; ?>" style="height:400px;width: 260px;">
            </a>
        <?php endwhile; ?>
    
    
    <br /><br />
    <span style="font-size:18px;font-family:trebuchet ms;"><?php echo $votes[0]; ?> Vote-Ups</span>
    <small>Vs.</small>
    <span style="font-size: 18px; font-family: trebuchet ms;"><?php echo $votes[1]; ?> Vote-Ups</span><br /><br />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制