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 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!