PHP函数离开容器Div

时间:2017-11-04 18:24:05

标签: php

基本上我用PHP创建了一个基本的在线商店,我创建了一个名为get_brands()的文件,它只是从db获取产品的品牌名称,如下所示:

    <?php 
function get_brands(){
    $get_brands = "select * from brands";
    $run_brands = mysqli_query($GLOBALS['con'],$get_brands);
    while($row_brands=mysqli_fetch_array($run_brands)){
        $brand_id = $row_brands['brand_id'];
        $brand_title = $row_brands['brand_title'];
        echo "
            <option value='$brand_id'>$brand_title</option>
        ";
    }
}
?>

现在我想在我的另一页内回应:

<div class='content-wrapper'>
        <section class='content-header'>
            <h1>
                Add New Products
                <small>onlinestore.danoup.com</small>
            </h1>
            <ol class='breadcrumb'>
                <li class='active'>insertproducts.php</li>
            </ol>
        </section>
        <?php 
        if(($dataSet->GetLevel()==1) || ($dataSet->GetLevel()==2)){ echo "
        <section class='content'>
            <div class='box box-default'>
                <div class='box-header with-border'>
                    <h3 class='box-title'>Overall Info</h3>

                    <div class='box-tools pull-right'>
                        <!-- <button type='button' class='btn btn-box-tool' data-widget='collapse'><i class='fa fa-minus'></i></button> 
                        <button type='button' class='btn btn-box-tool' data-widget='remove'><i class='fa fa-remove'></i></button> -->
                    </div>
                </div>
                <div class='box-body'>
                    <form action='' method='POST' enctype='multipart/form-data'>
                        <div class='row'>
                            <div class='col-md-6'>
                                <div class='form-group'>
                                    <div class='form-group'>
                                        <label>Product Title:</label>
                                        <input type='text' name='product_title' class='form-control my-colorpicker1'>
                                    </div>
                                </div>
                                <div class='form-group'>
                                    <label>Product Category:</label>
                                    <select class='form-control select2' name='product_cat' style='width: 100%;'>
                                        ".get_cats()."
                                        ?>
                                    </select>
                                </div>
                                <div class='form-group'>
                                    <label>Product Brand:</label>
                                    <select class='form-control select2' name='product_brand' style='width: 100%;'>
                                        ".get_brands()."
                                    </select>
                                </div>
                                <div class='form-group'>
                                    <div class='form-group'>
                                        <label>Product Image 1:</label>
                                        <input type='file' name='product_img1' class='form-control my-colorpicker1'>
                                    </div>
                                </div>
                                <div class='form-group'>
                                    <div class='form-group'>
                                        <label>Product Image 2:</label>
                                        <input type='file' name='product_img2' class='form-control my-colorpicker1'>
                                    </div>
                                </div>
                                <div class='form-group'>
                                    <div class='form-group'>
                                        <label>Product Image 3:</label>
                                        <input type='file' name='product_img3' class='form-control my-colorpicker1'>
                                    </div>
                                </div>
                            </div>
                            <div class='col-md-6'>
                                <div class='form-group'>
                                    <div class='form-group'>
                                        <label>Product Price:</label>
                                        <input type='text' name='product_price' class='form-control my-colorpicker1'>
                                    </div>
                                </div>
                                <div class='form-group'>
                                    <div class='form-group'>
                                        <label>Product Keywords:</label>
                                        <input type='text' name='product_keywords' class='form-control my-colorpicker1'>
                                    </div>
                                </div>
                                <input type='submit' name='insert_product' class='btn' value='Submit'>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-md-12'>
                                <div class='box box-info'>
                                    <div class='box-header'>
                                        <h3 class='box-title'>Product Description:
                                            <small>Add new product to your onlinestore</small>
                                        </h3>
                                        <div class='pull-right box-tools'>
                                            <button type='button' class='btn btn-info btn-sm' data-widget='collapse' data-toggle='tooltip' title='Collapse'>
                                            <i class='fa fa-minus'></i></button>
                                            <button type='button' class='btn btn-info btn-sm' data-widget='remove' data-toggle='tooltip' title='Remove'>
                                            <i class='fa fa-times'></i></button>
                                        </div>
                                    </div>
                                    <div class='box-body pad'>
                                            <textarea name='product_desc' placeholder='Add description to your product' style='width: 100%; height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;'></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class='box-footer'>
                    Visit <a href='https://zite.pouyavagefi.com/documentation/onlinestores.php'>Online Store</a> documentation and know more about this plugin.
                </div>
            </div>
        </section> ";
        }else{ 
            echo "
                <section class='content'>
                    <div class='alert alert-warning'>
                        <strong>Access Denied!</strong> You don\'t have permission to access this page.
                    </div> 
                </section> 
            ";
        }
        ?>
    </div>

现在唯一的问题是,它显示div之外的名称,而不是我设置的 select 标签内部...(查看下面的打印屏幕):

print screen

我不知道为什么会发生这种情况,因为我设置了这样的代码:

<div class='form-group'>
    <label>Product Brand:</label>
    <select class='form-control select2' name='product_brand style='width: 100%;'>
        ".get_brands()."
    </select>
</div>

所以,如果您对此有任何想法,请告诉我,我真的很感激!

1 个答案:

答案 0 :(得分:1)

您需要从<option>函数返回get_brands标记,而不是直接回复它们。如果将函数值组合成这样的字符串,那么函数中的任何echo调用都将首先发生,因为在将字符串发送到浏览器之前,PHP会对该字符串进行评估。

一个基本的例子是:

echo 1 . ' ' . two();
function two() {
  echo 2;
}

这实际上会显示2 1,因为在构建字符串时运行该函数,并且其echo语句首先运行。

为了使代码正常运行,我们可以将其更改为:

echo 1 . ' ' . two();
function two() {
  return 2;
}

这将正确显示1 2,因为该功能不再直接调用echo

对于您的示例,您应该在<option>中构建一串get_brands个标记,然后将其返回:

$return = '';

while($row_brands=mysqli_fetch_array($run_brands)){
    $brand_id = $row_brands['brand_id'];
    $brand_title = $row_brands['brand_title'];
    $return .= "
        <option value='$brand_id'>$brand_title</option>
    ";
}

return $return;