Jump to content

imbruceter

Members
  • Posts

    11
  • Joined

  • Last visited

imbruceter's Achievements

Member

Member (2/5)

0

Reputation

  1. I have a different script where it gets handlet like this: if (isset($_POST['submit'])) { $id = filter_var($_POST['id'], FILTER_SANITIZE_NUMBER_INT); //generated an id for the post itself $category_id = filter_var($_POST['category'], FILTER_SANITIZE_NUMBER_INT); //attaches choonen the categorys id to the post } //uploading to the database $query = "UPDATE posts SET category_id=$category_id WHERE id=$id LIMIT 1"; $result = mysqli_query($connection, $query); And then in the new script I fetch the category data and get these values with the form: //fetching categories $category_query = "SELECT * FROM categories"; $categories = mysqli_query($connection, $category_query); if (isset($_GET['id'])) { $id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT); $query = "SELECT * FROM posts WHERE id=$id"; $result = mysqli_query($connection, $query); $post = mysqli_fetch_assoc($result); } <form action="<?= ROOT ?>editing-logic.php" enctype="multipart/form-data" method="POST"> Sorry if the explanation is not describing enough, I try to make it as clear as possible.
  2. Thank you for your reply! I inserted your coding and this is what I get: I've changed "category" to "id" because it is already defined, the warning disappears, but the problem is that "id" is the id of the post and not the category.
  3. I'm quite new to PHP. I have a selection dropdown, which sends the data to the phpmyadmin database. This works fine, but I'd like it to be selected as default when I'm opening it again. Here is how my code currently looks like: $category_query = "SELECT * FROM categories"; $categories = mysqli_query($connection, $category_query); if (isset($_GET['id'])) { $id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT); } <select name="category"> <?php while ($category = mysqli_fetch_assoc($categories)) : ?> <option value="<?= $category['id'] ?>"><?= $category['title'] ?></option> <?php endwhile ?> </select> Here is what I've tried: <option value="<?= $category['id'] ?>"><?= $category['title'] ? "selected" : ""?></option> I basically just inserted "selected" into it, but it's not the correct way, because every single option is shown as "selected". Here is an image about the frontend: Does anyone know how to make it to work? This is the last thing I need to fix on the website. Thank you for being here!
  4. God! You did it! Sorry for being this terribly dumb, I am very far from web development (came from game development). Yes, I haven't recognised that I didn't delete the if statement. It works now like a charm! Thank you a lot for your time and your help!
  5. Yes, I have checked the error and warning log a couple of times, nothing in there. The foreach loop never gets executed since the <p> tag doesn't show up (the tags you see on the image are different ones).
  6. I have this already at the top of my code file but there is still no error message. i have put this right above this code snippet so you can see that I am not flashing:D I am starting to humiliate myself but I have really no idea what is going on...
  7. This is how it looks like now: $res = $connection->query("SELECT c.title, COUNT(p.category_id) as total FROM category c LEFT JOIN post p ON c.id = p.category_id GROUP BY c.id"); if (is_array($res)) { foreach ($res as $row) { ?> <p><?= "{$row['title']} : {$row['total']}<br>"; ?></p> // <?php echo $row['title']; //just for testing } } I had to add an if statement because without having it, it gives this warning: The issue is, that nothing on the website gets displayed about this code. No error but not even the paragraph can be seen in the console relating to this code snippet (it should be listed under "Categories" on the page). I'm so lost:D
  8. I am thankful for your answer! I am sorry for the noob question but can you tell me how should I display the result? I have tried extending your suggestion with these lines: $allselect_query = mysqli_query($connection, $allselect); $allselect_assoc = mysqli_fetch_assoc($allselect_query); $num_rows_allselect = $allselect_assoc['total']; At least this is how I displayed every data from a row but that's not the case for now. I am truly confused, this is so complicated.
  9. I'm really sorry for the poor explanation, I am absolutely not familiar with PHP. I would like to get data from my phpmyadmins 2 table, so I can display how many posts have been submitted with the same category type. One is called "posts" and the other one is called "categories". I have attached a picture to see the how it looks like. What I want is to make the category tables "id" equal to the posts tables "catagory_id", and then display how many posts have bben uploaded of each category. Here is how far I went with the coding, which is nowhere close to my expectations: <div class="stats__div"> <p class="stats__text">Categories</p> <p> <?php $all_categories_query = "SELECT * FROM categories ORDER BY title ASC"; $all_categories = mysqli_query($connection, $all_categories_query); while ($category = mysqli_fetch_assoc($all_categories)) : ?> <p><?= $category['title'] ?> <?= $category['id'] ?></p> <?php endwhile ?> <?php $all_posts_query = "SELECT * FROM posts GROUP BY category_id"; $all_posts = mysqli_query($connection, $all_posts_query); while ($posts = mysqli_fetch_assoc($all_posts)) : ?> <p><?= $posts['category_id'] ?></p> <?php endwhile ?> </p> </p> </div> I know, the coding is at the edge of being horrible, I am really sorry for that. I have tried my best. Does anyone know how to do that? I have seen that the right solution is using "array_count_values", but all the examples I have seen were lists. Any kind of help is highly appreciated!
  10. I am really sorry for the poorly asked question. I want the pagination display only 4 pages at the same time (2 previous and 2 next), like on this photo: Example image of the paginary This is how far I went with the coding: ?php //fetch all posts from posts table $query = "SELECT * FROM posts ORDER BY date_time DESC"; $posts = mysqli_query($connection, $query); $posts_per_page = 1; $number_of_posts = mysqli_num_rows($posts); $number_of_pages = ceil($number_of_posts/$posts_per_page); if(!isset($_GET['page'])) { $page = 1; } else { $page = $_GET['page']; } $this_page_first_posts = ($page - 1) * $posts_per_page; $query = "SELECT * FROM posts LIMIT " . $this_page_first_posts . ', ' . $posts_per_page; $posts = mysqli_query($connection, $query); ?> Displaying pagination: <div class="pagination"> <?php if($page > 1) { echo '<a class="page__numbers" href="blog.php?page=' . ($page - 1) . '">Previous</a>'; } for($p = 1; $p < $number_of_pages; $p++) { echo '<a class="page__numbers" href="blog.php?page=' . $p . '">' . $p . '</a> '; } if($p > $page) { echo '<a class="page__numbers" href="blog.php?page=' . ($page + 1) . '">Next</a>'; } ?> </div> It currently displays every single available pages, which is obviously unideal once the blog gets filled with content. Every help is highly appreciated, have a nice day!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.