PHP:跟踪搜索查询中的数组元素

发布于 2024-12-10 13:31:14 字数 677 浏览 1 评论 0原文

我构建了一个简单的数据库,其中页面显示随机选择的报价,这些报价显示在无序列表中。 js 脚本循环遍历每个列表项,一次显示一个。

这是查询循环:

while ($row = mysql_fetch_assoc($query)) {
    echo "<li><span id='id'>";
    // print the quote number
printf ("%s", $row['id']);
    echo "</span>&nbsp;<span id='quote'>";
    // print the quote
printf ("%s", $row['strategy']);
    echo "</span>&nbsp;<span id='author'>";
    // print the quote author
    printf ("by %s", $row['author']);
    echo "</span></li>";

}

到目前为止一切顺利。我的问题是如何显示喜欢和不喜欢的数量,并让用户对其进行“投票”。我不知道如何在像这样打印出来后保持数组数据处于活动状态。我可以使用计数器来跟踪 PHP 中的行元素,以便我可以知道用户正在投票的引用吗?

谢谢!

I built a simple database where the page displays a random selection of quotes which are displayed in an unordered list. A js script cycles through each list item showing them one at a time.

This is the query loop:

while ($row = mysql_fetch_assoc($query)) {
    echo "<li><span id='id'>";
    // print the quote number
printf ("%s", $row['id']);
    echo "</span> <span id='quote'>";
    // print the quote
printf ("%s", $row['strategy']);
    echo "</span> <span id='author'>";
    // print the quote author
    printf ("by %s", $row['author']);
    echo "</span></li>";

}

So far so good. My problem is how to show the number of likes and dislikes, and have users 'vote' on them. I cannot figure out how I can keep array data active once it is printed out like this. Could I use a counter to keep track of row elements in PHP so I can then know which quote the user is voting on?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-12-17 13:31:15

你的代码很难读,所以我重写了它。您想要使用报价 ID 号来访问有关报价的其他数据。 Dhruv 的示例是使用元素 id 并通过 javascript 获取数据的好方法。但对于某些事情,您可能希望在服务器上处理更多内容。

// assume the numbers I'm using are valid quote ids
$like_array=array(12=>'15 likes', 14=>'2 likes', 18=>'6 likes');

$output ='';

while ($row = mysql_fetch_assoc($query)) {
    $output .= "<li><span id='id'>{$row['id']}</span> ";                     // quote number
    $output .= "<span id='quote'>{$row['strategy']}</span> ";                // print the quote
    $output .= "<span id='author'>by {$row['author']}</span></li>";               // print the quote author
    $output .= '<a href="http://somelink.com/page.php?like='.$row['id'].'"></a>'; // like link
    $output .= '<span>Number of likes: '.$like_array[$row['id'].'</span>';        // like link
 }

 echo $output;

您可以看到,like 链接只是将报价的 id 作为 $_GET 变量传递的链接。对于点赞数部分,假设我们已经拥有该信息,并且它已经位于一个数组中,索引键为引用 ID。 (这就是 $like_array)

这只是基本想法,但应该让您朝着正确的方向前进。

Your code was hard to read so I rewrote it. You want to use the quote id number to access other data about the quotes. Dhruv's example was a good way to use an element's id and get the data with javascript. But for some things you might want to handle more of it on the server.

// assume the numbers I'm using are valid quote ids
$like_array=array(12=>'15 likes', 14=>'2 likes', 18=>'6 likes');

$output ='';

while ($row = mysql_fetch_assoc($query)) {
    $output .= "<li><span id='id'>{$row['id']}</span> ";                     // quote number
    $output .= "<span id='quote'>{$row['strategy']}</span> ";                // print the quote
    $output .= "<span id='author'>by {$row['author']}</span></li>";               // print the quote author
    $output .= '<a href="http://somelink.com/page.php?like='.$row['id'].'"></a>'; // like link
    $output .= '<span>Number of likes: '.$like_array[$row['id'].'</span>';        // like link
 }

 echo $output;

You can see that the like link is just a link that passes the quote's id as a $_GET variable. For the number of likes part assume that we already have that information and it's already in an array with the index keys being the quote ids. (That's the $like_array)

This is just the basic idea, but should get you going in the right direction.

抱猫软卧 2024-12-17 13:31:15

您可以将数据库主键或索引号回显为行中的 id。
然后 JavaScript 就可以轻松检测到哪个元素被投票了。

示例:

while ($row = mysql_fetch_assoc($query)) {
    echo "<li><span id='id'>";
    // print the quote number
printf ("%s", $row['id']);
    echo "</span> <span id='quote'>";
    // print the quote
printf ("%s", $row['strategy']);
    echo "</span> <span id='author'>";
    // print the quote author
    printf ("by %s", $row['author']);
    echo "</span>";
    echo "<a id='votelink_'".$row['id']."href='javascript:void(0);' onclick='checkVote(this.id);' > VOTE HERE   </a>";    
  echo "</li>";

}

Javascript:

function checkVote(theId)
{
   alert("Element with id = " + theId + " was voted" );
}

You can echo database primary key or an index number as id in the row.
Then javascript can easily detect which element was voted upon.

Example :

while ($row = mysql_fetch_assoc($query)) {
    echo "<li><span id='id'>";
    // print the quote number
printf ("%s", $row['id']);
    echo "</span> <span id='quote'>";
    // print the quote
printf ("%s", $row['strategy']);
    echo "</span> <span id='author'>";
    // print the quote author
    printf ("by %s", $row['author']);
    echo "</span>";
    echo "<a id='votelink_'".$row['id']."href='javascript:void(0);' onclick='checkVote(this.id);' > VOTE HERE   </a>";    
  echo "</li>";

}

Javascript :

function checkVote(theId)
{
   alert("Element with id = " + theId + " was voted" );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文