Ooilchen Posted February 27, 2023 Share Posted February 27, 2023 (edited) <?php include "conn.php"; $sql = "SELECT * FROM `promo`"; $result = $con->query($sql); while ($row = $result->fetch_assoc()) { $id = $row['id']; $category = $row['promo_category']; $link = $row['promocat_href']; $title = $row['title']; $content = $row['content']; $image = $row['image']; $created_date = $row['created_date']; $status_promo = $row['status_promo']; $checker = ($status_promo == 1) ? 'checked' : ''; echo " <tr> <td>" . $row['id'] . "</td> <td>" . $row['promo_category'] . "</td> <td>" . $row['promocat_href'] . "</td> <td>" . $row['title'] . "</td> <td class='clamped-text'>" . $row['content'] . "</td> <td>" . $row['image'] . "</td> <td>" . $row['created_date'] . "</td> <td> <button id='" . $row['id'] . "' type='button' class='btn btn-info edit-btn' data-toggle='modal' data-target='#editModal". $row['id'] ."' data-id='" . $row['id'] . "' action=> <i class='fa fa-pencil-square-o' aria-hidden='true'></i> </button> </td> <td class='text-center'> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='switchbtn" . $row['id'] . "' onchange='reply_click({id: this.id, status_promo: $status_promo})'" . $checker . "> </div> </td> </tr> <!-- Edit Modal --> <div class='modal fade' id='editModal". $row['id'] ."' data-id='".$row['id']."' tabindex='-1' aria-labelledby='editModalLabel' aria-hidden='true'> <div class='modal-dialog modal-xl'> <form id='editForm' method = 'POST' enctype='multipart/form-data'> <div class='modal-content'> <div class='modal-header'> <h4 class='modal-title' id='editModalLabel'>Edit News</h4> <button type='button' class='btn-close' data-bs-dismiss='modal' aria-label='Close'></button> </div> <div class='modal-body'> <div class='row'> <div class='col-md-8'> <div class='input-group mb-3'> <label class='input-group-text'>Category</label> <select class='form-select' id='editCategory' name='editCategory'> <option selected>". $row['promo_category'] ."</option> <option value='ANNOUNCEMENTS'>ANNOUNCEMENTS</option> <option value='NEWS'>NEWS</option> <option value='PROMOTIONS'>PROMOTIONS</option> </select> </div> </div> <div class='col-md-4'> <div class='input-group mb-3'> <label class='input-group-text'>Tag</label> <select class='form-select' id='editLink'". $row['id']." name='editLink'> <option selected>". $row['promocat_href'] ."</option> <option value='announcements'>#announcements</option> <option value='#news'>#news</option> <option value='#promotions'>#promotions</option> </select> </div> </div> <div class='col-md-12'> <label>Title</label> <div class='input-group mb-3'> <input type='text' id='editTitle'". $row['id'] ." name='editTitle' class='form-control' value='". $row['title'] ."'> </div> </div> <div class='col-md-12'> <label>Upload Image</label> <div class='input-group mb-3'> <input type='file' class='form-control' id='editImage' name='editImage' value='". $row['image'] ."'> </div> </div> <div class='col-md-12'> <label>Content</label> <div id='div_editor1". $row['id'] ."' name='div_editor1". $row['id'] ."'> </div> </div> </div> <br> <span id='date-time' style='float: right;'></span> <input name='div_editor_html". $row['id'] ."' id='div_editor_html". $row['id'] ."'> </div> <div class='modal-footer'> <button type='button' class='btn btn-secondary' data-dismiss='modal' aria-label='Close'>Close</button> <button class='' type='submit' name='editSave' id='edit". $row['id'] ."' data-id='". $row['id'] ."'>Save changes</button> </div> </div> </form> </div> </div> <!-- End Edit Modal --> <script type='text/javascript' src='./richtexteditor/rte.js'></script> <script type='text/javascript' src='./richtexteditor/plugins/all_plugins.js'></script> <script> var editor1 = new RichTextEditor('#div_editor1". $row['id'] ."'); editor1.setHTMLCode('". $row['content'] ."'); console.log(editor1.getHTMLCode()) editor1.attachEvent('change', function() { var rteHTML = document.getElementById('div_editor_html". $row['id'] ."'); rteHTML.value = editor1.getHTMLCode() }); </script> "; if (isset($_POST['editSave'])) { // here need add && ==id if($_SERVER["REQUEST_METHOD"] == "POST"){ // $id = $_POST['id']; //what if i create an input that hide that put in the edit modal then detect that input if id==id then update? <----- $id = $row['id']; $editCategory = $_POST['editCategory']; $editLink = $_POST['editLink']; $editTitle = $_POST['editTitle']; $editImage = $_POST['editImage']; $rte = $_POST['div_editor_html']; $folder_path = "../Newebsite/images/news/"; $path = "/images/news/"; // Check if the uploaded image is not empty if (!empty($_FILES['upload_img']['name'])) { // Get the uploaded image and save it to the defined folder $image = $_FILES['upload_img']['name']; $image_temp = $_FILES['upload_img']['tmp_name']; move_uploaded_file($image_temp, $folder_path . $image); } else { // If the uploaded image is empty, set the path to blank $image = ""; $path = ""; } $sql2 = "UPDATE promo SET promo_category='$editCategory', promocat_href='$editLink', title='$editTitle', image='$image', content='$rte', status_promo='0' WHERE id='$id'"; $result = $con->query($sql2); if (mysqli_query($con, $sql2)) { echo " <script> alert('Record has been updated.'); window.location = document.URL; </script> "; } else { echo "Error: " . $sql1 . ":-" . mysqli_error($con); } mysqli_close($con); } } } ?> <script> async function retrieveList(){ rawResponse = await fetch('/editNewebsite/newsActionRetrieveAll.php', { method: 'GET', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: null }); const content = await rawResponse.json(); return content; }; retrieveList().then(result => { // maybe i think need to append modal first $("#dataList").empty(); $("#modalContainer").empty(); var id="myId"; if(result){ result.map((item, index) => { var data = $(` <tr> <td>${item.id}</td> <td>${item.promo_category}</td> <td>${item.promocat_href}</td> <td>${item.title}</td> <td class='clamped-text'>${item.content}</td> <td>${item.image}</td> <td>${item.created_date}</td> <td> <button id=${item.id} type='button' class='btn btn-info edit-btn' data-toggle='modal' data-target='#editModal${item.id}' data-id=${item.id}> <i class='fa fa-pencil-square-o' aria-hidden='true'></i> </button> </td> <td class='text-center'> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='switchbtn${item.id}' onchange='reply_click({id: ${item.id}, status_promo: ${item.status_promo}})'" . $checker . "> </div> </td> </tr> `) var modal = $(` <div class='modal fade' id='editModal${item.id}' tabindex='-1' aria-labelledby='editModalLabel' aria-hidden='true'> <div class='modal-dialog modal-xl'> <form> <div class='modal-content'> <div class='modal-header'> <h4 class='modal-title' id='editModalLabel'>Edit News</h4> <button type='button' class='btn-close' data-bs-dismiss='#editModal' aria-label='Close'></button> </div> <div class='modal-body'> <div class='row'> <div class='col-md-8'> <div class='input-group mb-3'> <label class='input-group-text'>Category</label> <select class='form-select' id='editCategory". $row['id'] ."' name='editCategory'> <option selected>". $row['promo_category'] ."</option> <option value='ANNOUNCEMENTS'>ANNOUNCEMENTS</option> <option value='NEWS'>NEWS</option> <option value='PROMOTIONS'>PROMOTIONS</option> </select> </div> </div> <div class='col-md-4'> <div class='input-group mb-3'> <label class='input-group-text'>Tag</label> <select class='form-select' id='editLink". $row['id'] ."' name='editLink'> <option selected>". $row['promocat_href'] ."</option> <option value='announcements'>#announcements</option> <option value='#news'>#news</option> <option value='#promotions'>#promotions</option> </select> </div> </div> <div class='col-md-12'> <label>Title</label> <div class='input-group mb-3'> <input type='text' id='editTitle". $row['id'] ."' name='editTitle' class='form-control' value='". $row['title'] ."'> </div> </div> <div class='col-md-12'> <label>Upload Image</label> <div class='input-group mb-3'> <input type='file' class='form-control' id='editImage' name='editImage' value='". $row['image'] ."'> </div> </div> <div class='col-md-12'> <label>Content</label> <div id='div_editor1". $row['id'] ."' name='div_editor1". $row['id'] ."'> </div> </div> </div> <br> <span id='date-time' style='float: right;'></span> <input class='' name='div_editor_html". $row['id'] ."' id='div_editor_html". $row['id'] ."'> </div> <div class='modal-footer'> <button type='button' class='btn btn-secondary' data-dismiss='modal' aria-label='Close'>Close</button> <button id='". $row['id'] ."' class='btn btn-primary editSave'>Save changes</button> </div> </div> </form> </div> </div> `) $("#dataList").append(data); }) } }) </script> Edited February 27, 2023 by requinix please use the Code <> button when posting code Quote Link to comment https://forums.phpfreaks.com/topic/315955-update-to-1st-row-when-try-to-update-other-rows/ Share on other sites More sharing options...
ginerjm Posted February 27, 2023 Share Posted February 27, 2023 Way too much posted code that doesn't pertain to the topic's point IMHO. Let me ask this though. What are these 2 lines doing for you: $result = $con->query($sql2); if (mysqli_query($con, $sql2)) { You seem to be running the same query twice Quote Link to comment https://forums.phpfreaks.com/topic/315955-update-to-1st-row-when-try-to-update-other-rows/#findComment-1606063 Share on other sites More sharing options...
Strider64 Posted February 27, 2023 Share Posted February 27, 2023 (edited) I think I might chime in as well. I think you're trying to do too much with all that code. Coding should work for you, not against you. This is how I generally start off developing a web page. First, I design my web page with HTML/CSS on how I want it to look then I proceed onto coding the page. Then I try to keep as much as the code separated as much as possible, by that I mean having most of the PHP at the top of the file (page) and the HTML at the bottom of the page. The JavaScript in a separate file (though I might have a little amount of JavaScript sprinkle in) and if I'm using FETCH/JSON the PHP in a separate file as well. When I do have PHP in the HTML I try to keep it to the bare minimum instead of encapsulating all the HTML with an echo statement. Even the HTML I use over and over for other pages I have as a separate file with an include statement though that is usually done when I have most of the website up and running. A fragmented Example: <body class="site"> <header class="headerStyle"> <!-- Button to open the modal login form --> <button id="myButton" onclick="document.getElementById('id01').style.display='block'">Login</button> <!-- The Modal --> <div id="id01" class="modal"> <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span> <!-- Modal Content --> <form class="modal-content animate" method="post"> <div class="imgcontainer"> <img src="assets/images/img-john-pepp-150-150-001.jpg" alt="Avatar" class="avatar"> </div> <div class="container"> <label for="uname"><b>Username</b></label> <input type="text" placeholder="Enter Username" name="user[username]" required> <label for="psw"><b>Password</b></label> <input type="password" placeholder="Enter Password" name="user[hashed_password]" required> <button type="submit" name="submit" value="login">Login</button> <label> <input type="checkbox" checked="checked" name="user[remember]"> Remember me </label> </div> <div class="container" style="background-color:#f1f1f1"> <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel </button> <span class="psw">Please <a href="register.php">Register</a></span> </div> </form> </header> <?php include_once 'assets/includes/inc.navigation.php'; ?> <section class="main"> <?php foreach ($cms as $record) { echo '<header class="sectionHeader">'; echo '<h2 class="sectionHeadingText">'. $record['heading'] . '</h2>'; echo '</header>'; echo '<article class="sectionArticle">'; echo '<img class="imageArticle right-side" src="' . $record['image_path'] . '" alt="LEGO Bookstore">'; echo '<p class="articleText">' . nl2br($record['content']). '</p>'; echo ' </article>'; echo '<footer class="sectionFooter">'; echo '<p class="sectionText">Written by ' . $record['author'] . ' on ' . CMS::styleDate($record['date_added']) .' </p>'; echo '</footer>'; } ?> <div class="flex_container"> <?= $pagination->links(); ?> </div> </section> Though coders do have their own preferences, but this is my preference as I find it easier to go straight to the problem if I am having one instead of hunting through a LOT OF HTML. Edited February 27, 2023 by Strider64 1 Quote Link to comment https://forums.phpfreaks.com/topic/315955-update-to-1st-row-when-try-to-update-other-rows/#findComment-1606068 Share on other sites More sharing options...
ginerjm Posted February 27, 2023 Share Posted February 27, 2023 Totally agree with Strider. To create this topic that seems to reference a problem with doing a db update and post ALL THAT CODE that has nothing to do with (mostly) is a waste of the poster's time as well as ours - if we even try to read thru it all. 1 - can you identify the part of the code that is giving you the problem? 2 - Are you getting any errors? If you are - You Should Be Showing Us the complete message as well as the line(s) of code it refers to. 3 - Try and make it easy for us to help you as well as make it easy on yourself to zero in on that part of your effort as well as when we give you suggested changes to it. Quote Link to comment https://forums.phpfreaks.com/topic/315955-update-to-1st-row-when-try-to-update-other-rows/#findComment-1606069 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.