简码中的“编辑”按钮从数据库Wordpress重定向到项目页面

时间:2018-09-06 11:37:30

标签: php html wordpress

我有一个简码,其中显示了另一页上提交的表单中的一些信息。问题是我如何“重定向”到传递按编辑按钮的项目ID的表单?有人可以给些建议吗

这是简码的代码:

<?php
global $wpdb;
$table_name = $wpdb->prefix . 'property';
$properties = $wpdb->get_results("SELECT * FROM $table_name");

?>
<?php foreach ($properties as $property) : ?>

    <div class="col-sm-4 my-4 card-selector">
        <div class="card bg-light text-dark card-height">
            <?php if ($property->sale_rent == 0) : ?>
                <div class="corner-ribbon top-left sticky red shadow">Sale</div>
            <?php elseif ($property->sale_rent == 1): ?>
                <div class="corner-ribbon top-left sticky red shadow">Rent</div>
            <?php endif; ?>
            <img class="img-responsive card-img-top" src="" alt="">
            <div class="card-body">
                <h2 class="card-title"><?php echo $property->country ?></h2>
                <h3 class="card-title"><?php echo $property->price ?> £</h3>
                <h5 class="card-title"><?php echo $property->county, $property->town ?></h5>
                <h6 class="card-title"><?php echo $property->displayable_address ?></h6>
                <i class="fas fa-bed"><?php echo $property->nr_of_bedrooms ?></i> <i
                        class="fas fa-bath"><?php echo $property->nr_of_bathrooms ?></i>
                <p id="description" class="description card-text"><?php echo $property->property_description ?></p>
                <?php if ($property->custom) : ?>
                    <a href="" class="card-link">Edit</a>
                    <a class="deleterow card-link"
                       href="">Delete</a>
                <?php endif; ?>
            </div>
        </div>
    </div>
<?php endforeach; ?>

这是我提交的表单的一部分:

url: http://test.localhost/wordpress/add-property/
<h1>Add Property</h1>
<hr>
<div id="property-status"></div>
<form id="add-form" method="post" action="#" enctype="multipart/form-data">
    <div class="row">

        <div class="col-md-8">
            <form name="contact-form" action="" method="post" id="contact-form">
                <div class="form-group ">
                    <label>County*</label>
                    <input type="text" id="county" class="form-control" name="county" value="" required/>
                </div>
                <div class="form-group">
                    <label>Country*</label>
                    <input type="text" id="country" class="form-control" name="country" value="" required/>
                </div>
                <div class="form-group">
                    <label>Town</label>
                    <input type="text" id="town" class="form-control" name="town" required/>
                </div>
                <div class="form-group">
                    <label>Postcode</label>
                    <input type="number" id="postcode" class="form-control" name="postcode" value="" required/>
                </div>
                <div class="form-group">
                    <label>Description</label>
                    <textarea name="property_description" id="property_description" class="form-control"></textarea>
                </div>

2 个答案:

答案 0 :(得分:1)

您需要这样做

<?php
global $wpdb;
$table_name = $wpdb->prefix . 'property';
$properties = $wpdb->get_results("SELECT * FROM $table_name");

?>
<?php foreach ($properties as $property) : ?>

    <div class="col-sm-4 my-4 card-selector">
        <div class="card bg-light text-dark card-height">
            <?php if ($property->sale_rent == 0) : ?>
                <div class="corner-ribbon top-left sticky red shadow">Sale</div>
            <?php elseif ($property->sale_rent == 1): ?>
                <div class="corner-ribbon top-left sticky red shadow">Rent</div>
            <?php endif; ?>
            <img class="img-responsive card-img-top" src="" alt="">
            <div class="card-body">
                <h2 class="card-title"><?php echo $property->country ?></h2>
                <h3 class="card-title"><?php echo $property->price ?> £</h3>
                <h5 class="card-title"><?php echo $property->county, $property->town ?></h5>
                <h6 class="card-title"><?php echo $property->displayable_address ?></h6>
                <i class="fas fa-bed"><?php echo $property->nr_of_bedrooms ?></i> <i
                        class="fas fa-bath"><?php echo $property->nr_of_bathrooms ?></i>
                <p id="description" class="description card-text"><?php echo $property->property_description ?></p>
                <?php if ($property->custom) : ?>
                    <a href="<?php echo 'http://test.localhost/wordpress/add-property/?id='.$property->id;?> " class="card-link">Edit</a>
                    <a class="deleterow card-link"
                       href="">Delete</a>
                <?php endif; ?>
            </div>
        </div>
    </div>
<?php endforeach; ?>




<?php

global $wpdb;
$table_name = $wpdb->prefix . 'property';


if(isset($_GET['id'])){
    $properties = $wpdb->get_results("SELECT * FROM $table_name where id= $_GET['id']");

    print_r($properties);
}

?>

<h1>Add Property</h1>
<hr>
<div id="property-status"></div>
<form id="add-form" method="post" action="#" enctype="multipart/form-data">
    <div class="row">

        <div class="col-md-8">
            <form name="contact-form" action="" method="post" id="contact-form">
                <div class="form-group ">
                    <label>County*</label>
                    <input type="text" id="county" class="form-control" name="county" value="" required/>
                </div>
                <div class="form-group">
                    <label>Country*</label>
                    <input type="text" id="country" class="form-control" name="country" value="" required/>
                </div>
                <div class="form-group">
                    <label>Town</label>
                    <input type="text" id="town" class="form-control" name="town" required/>
                </div>
                <div class="form-group">
                    <label>Postcode</label>
                    <input type="number" id="postcode" class="form-control" name="postcode" value="" required/>
                </div>
                <div class="form-group">
                    <label>Description</label>
                    <textarea name="property_description" id="property_description" class="form-control"></textarea>
                </div>
            </form>
     </div>
</div>

答案 1 :(得分:1)

如果要使添加和编辑页面相同,则需要在添加页面中添加更多代码。

首先,进行如下更改

<a href="<?php echo site_url().'/add-property/?id='.$property->id;?>" class="card-link">Edit</a>
<a class="deleterow card-link" href="">Delete</a>

我猜$property->id是主键。 点击后,这种编辑链接将导航到同一页面。

然后,您需要在顶部的添加页面中获取ID,然后从数据库中获取数据。

global $wpdb;
$id = $_GET['id'];
$table_name = $wpdb->prefix . 'property';
$properties = $wpdb->get_results("SELECT * FROM $table_name WHERE property_id=$id");

希望获得帮助!