codeignite如何在函数内的模型页面中回显获取的值

时间:2017-09-05 05:51:29

标签: php mysql codeigniter

我正在使用codeigniter,我想使用从mysql表中获取的信息来测试模型页面中的条件。

我的问题是,如何回应从"评论"中提取的值的总数。 mysql表。结果是3 - 所有数据都在

中找到

此处我的代码位于模型页

$query = "
    SELECT 3 - COUNT(*) AS value FROM `reviews`
    INNER JOIN users ON user_id = users.uid

    WHERE value = 1 AND user_id = '" . $logged_in_user_via_session . "'
";


$query = $this->db->query($query);

// how to I echo the total number of values fetched from the "reviews" mysql table.
// so I basically want to know if the result is 0 or 1 or 2 so i can continue with additional conditions 

提前致谢

最终清洁答案

勾选答案虽然顺利,但所有其他答案都是如此。但是,codeigniter允许您将类CI_DB_mysqli_result的Object添加到 $ records-> num_rows()-3; 中显示的数字。建议在记录后将其添加到变量中,如下所示

$query = "
                    SELECT COUNT(*) AS value FROM `reviews`
                    INNER JOIN users ON user_id = users.uid

                    WHERE value = 1 AND user_id = '" . $logged_in_user_via_session . "'
                    ";

                    $records = $this->db->query($query);

                    $countedRows=$records->num_rows();
                    $countedRows;
                    // subtracting three (3)
                    $total_minus_three = $countedRows - 3;

                    echo $total_minus_three;

3 个答案:

答案 0 :(得分:1)

echo '-3 result is ->'. $records->num_rows() -3 ;

修改

在我看到您的评论时,您希望从结果中减去3。

<ul class="ui-multiselect-checkboxes ui-helper-reset" style="height: 175px;">
<li class=" ">
    <label for="ui-multiselect-tar-option-0" title="" class="ui-corner-all">
        <input id="ui-multiselect-tar-option-0" name="multiselect_name" value="Abkhazian" title="Abkhazian" type="checkbox"><span>Abkhazian</span></label>
</li>
<li class=" ">
    <label for="ui-multiselect-tar-option-1" title="" class="ui-corner-all">
        <input id="ui-multiselect-tar-option-1" name="multiselect_name" value="Afar" title="Afar" type="checkbox"><span>Afar</span></label>
</li>
<li class=" ">
    <label for="ui-multiselect-tar-option-2" title="" class="ui-corner-all ui-state-hover">
        <input id="ui-multiselect-tar-option-2" name="mmultiselect_name" value="Afrikaans" title="Afrikaans" type="checkbox"><span>Afrikaans</span></label>
</li>
<li class=" ">
    <label for="ui-multiselect-tar-option-3" title="" class="ui-corner-all">
        <input id="ui-multiselect-tar-option-3" name="multiselect_name" value="Albanian" title="Albanian" type="checkbox"><span>Albanian</span></label> </li>
<li class=" ">
    <label for="ui-multiselect-tar-option-4" title="" class="ui-corner-all">
        <input id="ui-multiselect-tar-option-4" name="multiselect_name" value="Amharic" title="Amharic" type="checkbox"><span>Amharic</span></label>
</li>

答案 1 :(得分:1)

你应该这样试试

$query = "
    SELECT COUNT(*) AS value FROM `reviews`
    INNER JOIN users ON user_id = users.uid

    WHERE value = 1 AND user_id = '" . $logged_in_user_via_session . "'
";

$records = $this->db->query($query);

$countedRows=$records->num_rows();
echo "Total number".$countedRows;
echo "What you want is".$countedRows-3;

答案 2 :(得分:0)

您可以使用此命令查找记录总数。

$query = "
    SELECT 3 - COUNT(*) AS value FROM `reviews`
    INNER JOIN users ON user_id = users.uid

    WHERE value = 1 AND user_id = '" . $logged_in_user_via_session . "'
";

   echo $query->num_rows(); // for total record

$this->db->from("table name")->count_all_results()

echo "Total Records ->".$records->row_array();