【问题标题】:How do i display only projects with value 0?如何仅显示值为 0 的项目?
【发布时间】:2018-06-01 14:46:53
【问题描述】:

我正在为 WordPress 创建一个插件。使用此插件,管理员可以从后端插入项目。这些项目在数据库中的值始终为“0”。

每个人都可以从 ui 中插入一个项目,但这些项目在数据库中的值为“1”。

我只想在 GUI 的页面上显示值为“0”的项目。

使用此代码,我可以获取班级中的数据库项目。

public function getProjectenList() {

    global $wpdb;
    $return_array = array();

    $result_array = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . 
      "ivs_canvas_tabel` ORDER BY `id`", ARRAY_A);

这是视图中的代码:

   <?php
    if ($projecten->getNrOfProjecten() < 1) {
        ?>
        <tr><td colspan="4">Voeg een project of media-uiting toe</tr>
        <?php
    } else {
        $cat_list = $projecten->getProjectenList();

        //** Show all projects in the tabel
        foreach ($cat_list as $project_obj) {
            // Create update link
            $params = array('action' => 'update', 'id' => $project_obj->getId());
            // Add params to base url update link
            $upd_link = add_query_arg($params, $base_url);

            // Create delete link
            $params = array('action' => 'delete', 'id' => $project_obj->getId());
            // Add params to base url delete link
            $del_link = add_query_arg($params, $base_url);
            ?>

            <tr><td width="10" style="vertical-align: top;"><?php echo $project_obj->getId(); ?>
                </td>
                <?php
                // If update and id match show update form
                // Add hidden field id for id transfer
                if (($action == 'update') &&
                        ($project_obj->getId() == $get_array['id'])) {
                    ?> 
                    <td width="150" style="vertical-align: top;"><input type="hidden" name="id" value="<?php echo $project_obj->getId(); ?>">
                        <input type="text" name="naam" value="<?php echo $project_obj->getNaam(); ?>"></td>
                    <td width="10" style="vertical-align: top;"><input type="text" name="level" value ="<?php echo $project_obj->getLevel(); ?>"></td>
                    <td width="300" style="vertical-align: top;"><textarea name="beschrijving" maxlength="200"> <?php echo $project_obj->getBeschrijving(); ?></textarea></td>
                    <td><input type='file' name='afbeelding' value="<?= IVS_CANVAS_PLUGIN_INCLUDES_UPLOAD_IMGS_DIR_URL . $project_obj->getAfbeelding() ?>"></td>


                    <td colspan="2"><input type="submit" name="update" 
          value="Updaten"/></td>
                <?php } else { ?>
                    <td width="150" style="vertical-align: top;"><?php echo 
          $project_obj->getNaam(); ?></td>
                    <td width="10" style="vertical-align: top;"><?php echo 
        $project_obj->getLevel(); ?></td>
                    <td width="300" style="vertical-align: top;"><?php echo $project_obj->getBeschrijving(); ?></td>
                    <td width="100"><img src="<?= IVS_CANVAS_PLUGIN_INCLUDES_UPLOAD_IMGS_DIR_URL . $project_obj->getAfbeelding() ?>" width="100px" height="100px"/></td></td>



                    <?php
                    if ($action !== 'update') {
                        // If action is update don’t show the action button
                        ?>
                        <td><button><a href="<?php echo $upd_link; ?
       >">Wijzigen</a></button></td>
                        <td><button><a href="<?php echo $del_link; ?
       >">Verwijderen</a></button></td>
                        <?php
                    } // if action !== update
                    ?>

                <?php } // if acton !== update  ?>                   


            </tr>
            <?php
        }
    }
    ?>

表结构

   $sql = "CREATE TABLE $table_name (
        id int (100) NOT NULL AUTO_INCREMENT,
        naam varchar (255) NOT NULL,
        level int (10) NOT NULL,
        beschrijving varchar (1024) NOT NULL,
        afbeelding varchar(1024) NOT NULL,
        status int (1) NOT NULL,
        PRIMARY KEY  (id)

)

希望有人能帮帮我!

【问题讨论】:

  • 可以分享一下表格的结构吗?
  • 我已经添加了结构
  • 在您的查询中添加 where 子句 where somecolumn = 0
  • @MKhalidJunaid 我不能在这里关注..我对此很陌生..我必须在其中添加 WHERE? code $result_array = $wpdb->get_results("SELECT * FROM " . $wpdb-&gt;prefix . "ivs_canvas_tabel ORDER BY id", ARRAY_A);

标签: php database wordpress


【解决方案1】:

如果您只希望某列中值为 0 的项目,您可以在查询中添加 where 过滤器

$result_array = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . "ivs_canvas_tabel` WHERE some_column = 0 ORDER BY `id`", ARRAY_A);

上面将只返回some_column 为零的项目,如果some_column 具有此标志用于管理项目和用户项目,请使用您的列名

【讨论】:

  • @StefanYpma 你能提供你的表数据并检查 0 是否没有像上面的查询那样存储为字符串试试= '0'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多