【问题标题】:Using mysqli_num_rows with while loop and onclick event将 mysqli_num_rows 与 while 循环和 onclick 事件一起使用
【发布时间】:2012-12-05 22:32:52
【问题描述】:

我想回显行数(假设行数= 3)。如果用户单击数字(我从 while 循环中传递 1 个参数),则会提醒数据 但问题是

  1. 如果我在 while 循环中回显 numrows,则所有行(相关的 3 行数据)都会在 onclick 事件中收到警报,但 numrows 会显示 3 次,如 333。
  2. 如果我在 while 循环之外回显,则显示 num 行,但只有一个结果传递给函数。

我也使用了count(col),但是这样只检索到一个结果。

任何一次显示行数但将$uid(在while循环中)的所有结果传递给onclick函数的解决方案?.请帮助。

  $sql=mysqli_query($this->db->connection,"SELECT * from user_data where  scid='$scid'");
            $num=mysqli_num_rows($sql);

            while($row=mysqli_fetch_array($sql)){
                $uid=$row['uid'];



                ?>


            <span onclick=o4_sameby_scid(<?php echo $uid;  ?>)><?php echo  $num  ?></span>



            <script type="text/javascript">

                function o4_sameby_scid(o4_id) {
                    alert(o4_id);
                }


            </script>



            <?php
            }

【问题讨论】:

  • @itachi 循环中 $uid 的结果
  • 这个问题很模糊。解释Any solution to show the num rows one time but passing all the results to onclick function?这部分。可以举个例子。
  • @itachi.plz 查看问题。我已更新上述行
  • 什么格式?如1,2,3,.... 或其他?

标签: php jquery mysql-num-rows


【解决方案1】:

我认为您的问题是您在 while 循环中嵌套了错误的代码部分。这种方法怎么样?:

<?php
$sql=mysqli_query($this->db->connection,"SELECT * from user_data where  scid='$scid'");
$num=mysqli_num_rows($sql);

$allvalues = array();

//  Do all looping before any echoing
while($row=mysqli_fetch_array($sql)){
    // add this row's value to an array of all values
    $allvalues[] = $uid=$row['uid'];
}

// implode this array
$valuesascsv = implode(', ', $allvalues);


//  This is echo'd outside of the loop, because we only need to see it one time
echo '<span onclick=o4_sameby_scid("'. $valuesascsv .'")>'. $num .' results found! </span>';
?>

<script type="text/javascript">
    function o4_sameby_scid(o4_id) {
        alert(o4_id);
    }
</script>

这应该输出:

<span onclick=o4_sameby_scid("uid#1, uid#2, uid#3")>3 results found!</span>

<script type="text/javascript">
    function o4_sameby_scid(o4_id) {
        alert(o4_id);
    }
</script>

单击任何行数都会提醒所有 UID。

这是您要寻找的行为吗?

【讨论】:

  • ,谢谢你。没错,我想要同样的结果,但你能不能把 while 循环从警报中(在 javascript 标签之前)取出来让它更简单?
猜你喜欢
  • 2014-04-11
  • 2013-07-06
  • 2017-10-09
  • 2014-01-09
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 2011-08-06
相关资源
最近更新 更多