php 每一个加两个,一个用于输出,一个用于计数

carvr3hs  于 2023-04-19  发布在  PHP
关注(0)|答案(1)|浏览(126)
$result = $wpdb->get_results
("SELECT 
    p1.post_title as title,
    p1.post_name as slug,
    p1.post_type as eh_type,
    p1.post_date as eh_date 

    FROM wp_posts p1 
    WHERE p1.post_status='publish' AND p1.post_type='tribe_events' OR p1.post_type='post' 
    GROUP BY p1.ID
    ORDER BY p1.ID DESC;");

$countall = $wpdb->get_results
("SELECT COUNT(*) AS 'count_all' FROM wp_posts WHERE wp_posts.post_status='publish' AND wp_posts.post_type='tribe_events' OR wp_posts.post_type='post';");

            foreach($result as $res){
                ?><tr>
                       <td><h6><?php echo $res->title; ?></h6></td>
                       <td id="h-center"><?php if ($res->eh_type == 'post') { echo '<h6 id="color-p">POST</h6>'; } else if ($res->eh_type == 'tribe_events') { echo '<h6 id="color-e">EVENT</h6>'; }?></td>
                       <td id="h-center"><h6><?php echo $res->eh_date; ?></h6></td>
                       <td><h6><?php echo $res->slug; ?></h6></td>

                       <td id="h-center"><?php $totalrows =  $countall[0]->count_all;
                                                    for($chknum=0; $chknum<=$totalrows; $chknum++){
                                                        echo '<input type="checkbox"  onclick="chkcontrol('.$chknum.')" name="hcb">'; }
                                          ?>

                        </td>
                    </tr>
                <?php
            }
  • 'for each'细分:*
foreach($result as $res)

这一个从我的表中获取sql并将其输出到<td>中,例如$res->title$res->slug
我试图实现的是一个选择计数,并将其显示在onclick in复选框中:

<input type="checkbox"  onclick="chkcontrol($chknum)" name="hcb">

我试着把for each放在里面,但是复选框是在里面循环的:

<?php $totalrows =  $countall[0]->count_all;
for($chknum=0; $chknum<=$totalrows; $chknum++){
echo '<input type="checkbox"  onclick="chkcontrol('.$chknum.')" name="hcb">'; } ?>

表变成这样:

我如何使每个函数的正确,使onclick数的作品?我的意图是为每个是这样的:

<input type="checkbox" onclick="chkcontrol(0)" name="hcb">
<input type="checkbox" onclick="chkcontrol(1)" name="hcb">
<input type="checkbox" onclick="chkcontrol(2)" name="hcb">
<input type="checkbox" onclick="chkcontrol(3)" name="hcb">
<input type="checkbox" onclick="chkcontrol(4)" name="hcb">

希望我提供足够的信息

bkhjykvo

bkhjykvo1#

由于从问题中不是很清楚,我假设你想要的是每行一个复选框,并将正确的数字(或排序的id)传递给复选框。如果这是目标,那么你应该能够做到以下几点:

$result = $wpdb->get_results;

foreach ($result as $index => $res) { ?>
    <tr>
    <td><h6><?php echo $res->title; ?></h6></td>
    <td id="h-center">
        <?php
        if ($res->eh_type == 'post') {
            echo '<h6 id="color-p">POST</h6>';
        } else if ($res->eh_type == 'tribe_events') {
            echo '<h6 id="color-e">EVENT</h6>';
        } ?>
    </td>
    <td id="h-center"><h6><?php echo $res->eh_date; ?></h6></td>
    <td><h6><?php echo $res->slug; ?></h6></td>

    <td id="h-center">
        <?php
            echo '<input type="checkbox"  onclick="chkcontrol(' . $index . ')" name="hcb">';
        ?>
    </td>
    </tr>
<?php }

重要的部分是foreach ($result as $index => $res),这让你可以获取数组的键,在这种情况下,它将是一个索引,并在for循环中使用它。你可以使用它作为复选框的运行计数。
不相关,但你应该避免做不必要的数据库调用来提高性能。给出的代码摆脱了第二个db调用,但即使这对你不起作用,你可以做以下操作来删除第二个调用:

$result = $wpdb->get_results;
$countall = count($result); //Get the total rows buy just counting the result

相关问题