Mini Shell

Direktori : /home/haworld/www/admin/
Upload File :
Current File : /home/haworld/www/admin/about-edit.php

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include('include/head_admin.php');

    $page_id = '18';

    $image = '';
    $image_alt_tag = '';
    $heading = '';
    $description = '';
    $description2 = '';
    $mission = '';
    $vision = '';
    $goal = '';
    $service = '';

    if (isset($_GET['id']) && $_GET['id'] != '') {
        $id = get_safe_value($con, $_GET['id']);
        $image_required = '';
        $res = mysqli_query($con, "select * from `about` where id='$id'");
        $check = mysqli_num_rows($res);
        if ($check > 0) {
            $row = mysqli_fetch_assoc($res);
            $image = $row['image'];
            $image_alt_tag = $row['image_alt_tag'];
            $heading = $row['heading'];
            $description = $row['description'];
            $description2 = $row['description2'];
            $mission = $row['mission'];
            $vision = $row['vision'];
            $goal = $row['goal'];
            $service = $row['service'];
        } else {
            header('location:about.php');
            die();
        }
    }

    if (isset($_POST['update_about_submit'])) {
        echo $id = $_POST['id'];
        $image_alt_tag = get_safe_value($con, $_POST['image_alt_tag']);
        $heading = get_safe_value($con, $_POST['heading']);
        $description = get_safe_value($con, $_POST['description']);
        $description2 = get_safe_value($con, $_POST['description2']);
        $mission = get_safe_value($con, $_POST['mission']);
        $vision = get_safe_value($con, $_POST['vision']);
        $goal = get_safe_value($con, $_POST['goal']);
        $service = get_safe_value($con, $_POST['service']);

        // Check if new images are provided
        if (isset($_FILES['image'])) {
            // Fetch existing images from the database
            $row = mysqli_query($con, "SELECT * FROM `about` WHERE id='$id'");
            $result = mysqli_fetch_assoc($row);
            $old_images = json_decode($result['image'], true); // Decode JSON array

            $new_images = [];

            // Loop through the new images
            foreach ($_FILES['image']['name'] as $key => $value) {
                // Check if the file field is not empty
                if (!empty($_FILES['image']['name'][$key])) {
                    $new_image_name = $_FILES['image']['name'][$key];
                    $new_image_tmp_name = $_FILES['image']['tmp_name'][$key];
                    move_uploaded_file($new_image_tmp_name, '../media/about/' . $new_image_name);
                    $new_images[] = $new_image_name;
                } else {
                    // If no new image was uploaded for this index, add the corresponding old image
                    $new_images[] = $old_images[$key] ?? '';
                }
            }

            // Encode the modified array of image names as JSON
            echo $about_image = json_encode($new_images);
        } else {
            // If no new images were provided, keep the existing images
            echo  $about_image = $result['image'];
        }

        // Update the about information in the database
        $update_sql = "UPDATE `about` SET  `image`='$about_image',`image_alt_tag`='$image_alt_tag',`heading`='$heading', `description`='$description',`description2`='$description2',`mission`='$mission', `vision`='$vision',`goal`='$goal',`service`='$service' WHERE `id`='$id'";
        mysqli_query($con, $update_sql);
        header('location: about.php');
        die();
    }

    ?>
</head>

<body>
    <!--== MAIN CONTRAINER ==-->
    <?php include('include/header_admin.php'); ?>

    <!--== BODY CONTNAINER ==-->
    <div class="container-fluid sb2">
        <div class="row">

            <?php include('include/sidebar_admin.php'); ?>

            <div class="sb2-2">
                <div class="sb2-2-2">
                    <ul>
                        <li><a href="index.php"><i class="fa fa-home" aria-hidden="true"></i> Home</a>
                        </li>

                        <!-- <li class="page-back"><a href="index.php"><i class="fa fa-backward" aria-hidden="true"></i> Back</a>
                        </li> -->
                    </ul>
                </div>
                <div class="sb2-2-add-about sb2-2-1">
                    <div class="box-inn-sp">
                        <div class="bor">
                            <form method="post" enctype="multipart/form-data">
                                <div class="row">
                                    <?php if (isset($_GET['id'])) { ?>
                                        <div class="input-field col s12">
                                            <div class="row" id="image_box">
                                                <div class="col-lg-9">

                                                    <?php
                                                    $all_image = json_decode($image);
                                                    for ($i = 0; $i < count($all_image); $i++) { ?>
                                                        <div class="image-row">
                                                            <img src="../media/about/<?php echo $all_image[$i] ?>" alt="" width="80">
                                                            <input type="file" name="image[]" value="<?php echo $all_image[$i] ?>" class="form-control" <?php echo  $image_required ?> <?php echo $all_image[$i] ?>>
                                                        </div>
                                                    <?php } ?>
                                                    <label for="categories" class=" form-control-label">Images</label>
                                                </div>
                                                <div class="col-lg-3">
                                                    <button id="" type="button" class="btn btn-sm btn-info btn-block mt-2" onclick="add_more_images()">
                                                        <span id="payment-button-amount">Add Image</span>
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    <?php } else { ?>
                                        <div class="input-field col s12">
                                            <div class="row" id="image_box">
                                                <div class="col-lg-9">
                                                    <div class="image-row">
                                                        <input type="file" name="image[]" class="form-control">
                                                    </div>
                                                    <label for="categories" class=" form-control-label">Images</label>
                                                </div>
                                                <div class="col-lg-3">
                                                    <button id="" type="button" class="btn btn-sm btn-info btn-block mt-2" onclick="add_more_images()">
                                                        <span id="payment-button-amount">Add Image</span>
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    <?php } ?>
                                    <div class="input-field col s12">
                                        <input id="image_alt_tag" type="text" name="image_alt_tag" class="validate" value="<?php echo $image_alt_tag ?>" required>
                                        <label for="image_alt_tag">Image Alt Text</label>
                                    </div>
                                    <div class="input-field col s12">
                                        <input id="heading" type="text" name="heading" class="validate" value="<?php echo $heading ?>" required>
                                        <label for="heading">Heading</label>
                                    </div>


                                </div>
                                <div class="row">
                                    <h3>Description </h3>
                                    <div class="input-field col s12">
                                        <textarea id="editor" name="description"><?php echo $description ?></textarea>
                                        <!-- <label>about Descriptions:</label> -->
                                    </div>
                                    <!-- <br><br>
                                    <h3>Description 2</h3>
                                    <div class="input-field col s12">
                                        <textarea id="editor1" name="description2"><?php echo $description2 ?></textarea>
                                        <label>about Descriptions:</label>
                                    </div> -->
                                </div>
                                <div class="row">
                                    <div class="input-field col s12">
                                        <input id="mission" type="text" name="mission" class="validate" value="<?php echo $mission ?>" required>
                                        <label for="mission">Mission</label>
                                    </div>
                                    <div class="input-field col s12">
                                        <input id="vision" type="text" name="vision" class="validate" value="<?php echo $vision ?>" required>
                                        <label for="vision">Vision</label>
                                    </div>
                                    <!-- <div class="input-field col s12">
                                        <input id="goal" type="text" name="goal" class="validate" value="<?php echo $goal ?>" required>
                                        <label for="goal">Goal</label>
                                    </div>  -->
                                    <!-- <div class="input-field col s12">
                                        <input id="service" type="text" name="service" class="validate" value="<?php echo $service ?>" required>
                                        <label for="service">History</label>
                                    </div> -->
                                </div>
                                <div class="row">
                                    <input type="hidden" name="id" value="<?php echo $id ?>">
                                    <div class="input-field col s12">
                                        <input type="submit" name="update_about_submit" class="waves-effect waves-light btn-large" value="Submit">
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php include('include/footer_admin.php'); ?>

    <?php include('include/foot_admin.php'); ?>
    <script>
        var total_image = 1;

        function add_more_images() {
            total_image++;
            var html = '<div class="col-lg-9" style="margin-top:20px;" id="add_image_box_' + total_image + '"><label for="categories" class=" form-control-label">Image</label><input type="file" name="image[]" class="form-control"><button type="button" class="btn btn-sm btn-danger btn-block mt-2" onclick=remove_image("' + total_image + '")><span id="payment-button-amount">Remove</span></button></div>';
            jQuery('#image_box').append(html);
        }

        function remove_image(id) {
            jQuery('#add_image_box_' + id).remove();
        }
    </script>

</body>

</html>