I have a problem with checkboxes.
I have some project they you can filter at categories.
So the problem is when you want edit a project that automatic the correct category are checked.
here you see a project but the categories are empty
In mine databank I use is a combo table - one to insert my categories and one for the project and there I have a combo table from.
Here you see my code for display the checkboxes and select for editing so now I looking for some help, for checked the correctly checkbox that belongs to that project.
<div id="categoriefilter">
<?php
$sql = "SELECT * FROM `categories` ORDER BY `id` ASC";
$cats = DB::getResult($sql);
foreach($cats as $cat){
?>
<label>
<input name="cat[]" type="checkbox" value="<?php echo $cat['id']; ?>" >
<?php echo $cat['categorienaam']; ?>
</label>
<?php
}
?>
</div>
If I understand your question correctly. You need 3 tables here
Table1 : projects id, name, ...
Table2: categories id, name, ...
Table3: projects_categories project_id, category_id, ...
The third table is needed to store the categories which are selected for projects
And inside your project page
You need to do the followings:
Select your project by its id
Select categories
Select categories related to the project ( from Table3 )
Here is example ( this is not a working code, just a showcase )
// your project
$project = DB::getResult("SELECT * FROM project WHERE id = :id "); // <== id here
// list of categories
$categories = DB::getResult("SELECT * FROM categories ");
// list of categories for this project
$project_categories = DB::getResult("SELECT category_id FROM projects_categories WHERE project_id = :project_id ");
$project_categories = (array) $project_categories; // in case DB::getResult is not array
<?php
foreach($categories as $category) :
?>
<label>
<input name="cat[]"
<?php if(in_array($category['id'], $project_categories)) echo 'checked="checked"'; ?>
type="checkbox" value="<?= $category['id']; ?>" >
<?php echo $category['name']; ?>
</label>
<?php endforeach; ?>
I'm not down voter. If you knew your problem, you probably mightn't post the question.
You don't include the details code. However for the part as far as I'm understanding,
You should run database UPDATE query inside foreach loop. You don't need to keep the html input inside the loop.
<?php
// check if checkbox is checked
if (!empty($_POST['cat'])){
foreach($_POST['cat'] as $cat){
// run update query here
$sql = "UPDATE `categories` SET `categorienaam` = $whatever WHERE `id` = $cat";
$result = DB::getResult($sql);
}
}
?>
<label>
<input name="cat[]" type="checkbox" value="<?php echo $RowOfApplicableSelectQuery['id']; ?>" >
</label>
$RowOfApplicableSelectQuery comes from the query that you are using to fetch and display your record primarily.
I hope it gives you a clue about what are you looking for.
Related
I have two tables for Categories and Subcategories.
Categories has (categorie_id and categories_name) columns;
SubCategories has (subCategorie_id, subCategorie_display, categorie_id, subCategory_name) columns.
I did a LEFT OUTER JOIN query to obtain all the records in Subcategories, and match the ones that have the same categorie_id. Here is my code:
("SELECT subCategorie_id, subCategorie_name, subCategorie_display, categories.categorie_name, subCategories.categorie_id
FROM subCategories
LEFT OUTER JOIN categories ON categories.categorie_id = subCategories.categorie_id");
Now i have a form in which the content is displayed. I wan't to be able to edit that form, and update the table with the updated content. This is my query:
("UPDATE subCategories
LEFT OUTER JOIN categories
ON categories.categorie_id = subCategories.categorie_id
SET subCategorie_display='$display', subCategories.categorie_id='$catID',subCategorie_name='$name'
WHERE subCategorie_id='$id'")
What i get from this query is a new row with the correct content, but the one i wanted to edit, stays like it was.
Here is my HTML:
<select id="choosecat" name="choosecat" required>
<?php foreach($categoriesAll as $categorie) {
if($subcat->categorie_id == $categorie->categorie_id) { ?>
<option selected value="<?php echo $categorie->categorie_id; ?>"><?php echo $categorie->categorie_name; ?></option>
<?php } else { ?>
<option value="<?php echo $categorie->categorie_id; ?>"><?php echo $categorie->categorie_name; ?></option>
<?php } }?>
</select>
How can i fix this?
Already searched other answers but none worked.
Ok, so i found the bug.
The problem was in my form action, the link was calling a wrong id.
So I'm making a portfolio website where I can post and edit blogs.
I'm making the blog edit page and I'm having trouble with combining two table together.
The first table is for the blogs and the second table holds all the different blog categories.
Here's how I'm getting the blog posts:
$qStr = "SELECT post_title, post_content, post_description, post_active, category_id FROM posts WHERE post_id = {$post_id}";
Here's how I'm getting the blog categories:
$qStr = "SELECT category_name FROM categories WHERE category_id = {$category_id}";
So in my edit blog post page I have a dropdown box that I need to show which category that blog post is in, and be able to change it. I have a category_id in my blog table. My question is how do I get the dropdown to show which category the post is under?
Right now my drop down code looks like this (note: Right now I'm just populating the dropdown with all the categories)
<div class="form-group">
<label for="categorySelect" class="col-lg-2 control-label">Category</label>
<div class="col-lg-3">
<select class="form-control" id="categorySelect">
<?php
foreach ($categories as $cat) {
echo("<option>{$cat['category_name']}</option>");
}
?>
</select>
</div>
</div>
You need to set up the selected attribute for select (option) element, something like this:
<?php
foreach ($categories as $cat) {
if ($cat['category_name'] == $category_of_your_post)
echo "<option selected=\"selected\">{$cat['category_name']}</option>";
else
echo "<option>{$cat['category_name']}</option>";
}
?>
BTW, I think that you have the wrong queries (if I understood correctly your question). Basically you need to retrieve the category_id of your post and perform an equality question against the array of categories:
$qStrBlog = "SELECT post_title, post_content, post_description, post_active, category_id FROM posts WHERE post_id = {$post_id}";
$qStrCat = "SELECT category_id, category_name FROM categories";
And then:
<?php
foreach ($categories as $cat) {
if ($cat['category_id'] == $blog_data['category_id'])
echo "<option selected=\"selected\">{$cat['category_name']}</option>";
else
echo "<option>{$cat['category_name']}</option>";
}
?>
You have two options:
Add a selected attribute with an if-condition in the foreach loop:
foreach ($categories as $cat) {
echo '<option';
if ($cat['category_id'] == $post['category_id']) echo ' selected';
echo '>'.$cat['category_name'].'</option>';
}
Or use an INNER JOIN on the post SELECT query:
$qStr = "SELECT p.post_title, p.post_content, p.post_description, p.post_active, p.category_id, c.category_name FROM posts INNER JOIN categories c ON c.category_id = p.category_id WHERE post_id = {$post_id}";
and throw the first option element as the "default currently selected" option:
<select class="form-control" id="categorySelect">
<option value="<?= $post['category_id'] ?>"><?= $post['category_name'] ?> (current)</option>
<?php
foreach ($categories as $cat) {
echo("<option>{$cat['category_name']}</option>");
}
?>
</select>
I've made the assumption that you've correctly loaded your result from the first SELECT query into $post. Note that if it's possible to not set a Category to a Post, you should use LEFT JOIN instead and wrap the first option element output contingent on $post['category_id'] not being empty.
I have this code and i want to display genres table (ID, name) from my database with checkboxes and this works. Then i want to put old values from genres that become from another book_genre(ID,ID_book, ID_genre) table where i keep ids old and this didn't work. In that while i put all genres from my table and then in that input i want to check where is my old genre from my book_genre table and this not work because my loop through 4 times in genres (1.Action, 2. Thriller, SF, Comedy) and in the book_genre he found 2 values (2,3). At first loop check 1Action with 2 and i want to check 1 Action but without nothing from book_genre and i don't know how... .
$id_book = $_GET['id'];
$qr_genres_old = 'SELECT genres.name,book_genre.ID_genre,book_genre.ID
FROM genres
INNER JOIN book_genre
ON book_genre.ID_genre = genres.ID
WHERE ID_book = "'. $id_book .'"
ORDER BY ID_genre ASC';
$res_genre_old = mysql_query($qr_genres_old);
$qr_select_genres = 'SELECT * FROM genres';
$res_select_genre = mysql_query($qr_select_genres);
while( $row_genre_new = mysql_fetch_assoc($res_select_genre) ){
$row_genre_old = mysql_fetch_assoc($res_genre_old);
?>
<div>
<input type="checkbox" name="ID_gen<? echo $id=$row_genre_new['ID']; ?>"
value="1" <? if($row_genre_old['ID_genre']==$row_genre_new['ID'] ) echo 'checked="checked"'; ?> />
<? echo $row_genre_new['name']; ?>
</div>
I have 3 SQL tables, "artists", "tracks" and "tracks_artists" (=linking table, because one track can have multiple artists, I'm restricted to use a linking table).
I now have an "edit page" where I want to show 4 selects where you can choose an arists, but with the track's artists already selected. So if an track has 2 artists, I want the first 2 selects to display the previously selected artists (alongside with all the others artists in my database) and the other 2 selects just to show all the artists in the select (with nobody selected).
I'm now using this, but this only works when the track has just one artist:
<select name="edit_artist">
<?php
$query_artist = "SELECT * FROM artists";
$result_artist = mysql_query($query_artist) or die(mysql_error());
while ($row_artist = mysql_fetch_array($result_artist)) {?>
<?php
$query_ta = "SELECT artistID FROM tracks_artists WHERE trackID = $trackID";
$result_ta = mysql_query($query_ta);
while ($row_ta = mysql_fetch_array($result_ta)){
if($row_ta[0]==$row_artist[artistID]){$selected = "selected='selected'";}
else {$selected ="";}
}
?>
<option value="<?php echo $row_artist[artistID]?>" <?php echo $selected;?>><?php echo $row_artist[name];?></option>
<?php }?>
</select>
You need to use the multiple attribute on your select
<select name="edit_artist" multiple="multiple">
If you want to submit this you'll need to make the edit_artist name an array using brackets
<select name="edit_artist[]" multiple="multiple">
I have an edit page that gets populated when accessed. The input values work fine, but I'm having a hard time ticking the category checkboxes. I get the information from two tables. One that displays all the categories and the other one that gets the categories associated with the item.
The following code doesn't work because the second while statement finishes its loop in the first round. Is there an appropriate way to do this?
<?php $check_cats = mysql_query("SELECT * FROM item_categories WHERE itemid = '$itemid'") or die(mysql_error()); ?>
<?php $result = mysql_query("SELECT * FROM categories ORDER BY cname") or die(mysql_error()); ?>
<?php while($row = mysql_fetch_array( $result )) { ?>
<input type="checkbox" id="<?php echo $row['cname']; ?>" name="cat[]" value="<?php echo $row['id']; ?>"
<?php while($check_cat_rows = mysql_fetch_array( $check_cats )) {
if ($check_cat_rows['catid'] == $row['id']) {
echo 'checked="yes"';
}
}
} ?>
My Two tables:
TABLE `item_categories`
`id`
`itemid`
`catid`
TABLE `categories`
`id`
`cname`
Your basic structure could use improvement. Rather than two separate queries, and two nested loops, you could be using a single query which JOINS to the two tables together. Part of the joined data would be the "checked" flag, which you can check for within the loop and output the appropriate html.
SELECT ..., categories.id AS checked
FROM item_categories
LEFT JOIN categories
ON (item_categories.catid = categories.id)
and then while looping:
while($row = mysql_fetch_assoc()) {
$flag = ($row['checked') ? ' checked="yes"' : ''
...
}
Your whole thing is structured incorrectly, you can't assume the two results will line up perfectly, and your loops are wrong. Try this:
SELECT *,
(case when id IN
(SELECT catid FROM item_categories WHERE itemid = '$itemid')
then 1 else 0 end) checked
FROM categories ORDER BY cname
Now you just run the one query and have a nice little $row['checked'] to use!
SELECT *,
(case when categories.id IS NOT NULL
then 1 else 0 end) checked
FROM item_categories
LEFT JOIN categories
ON (item_categories.catid = categories.id)
WHERE itemid = '$itemid'
Improved based on hybrid between marc B and mine... Only efficiency difference is that the query handles testing the validity of categories.id instead of the php
I don't really understand your question, but you want the check box to be checked when the page loads? In that case you have to add "checked" to the tag
<input type="checkbox" name="option2" value="Butter" checked>
Something like this?
<?php
$query = <<<query
SELECT
c.id,
c.cname,
ifnull(ic.catid, '', 'checked="yes"') as checked
FROM
categories c
LEFT JOIN item_categories ic
ON ic.itemid = '$itemid'
AND ic.catid = c.id
ORDER BY
c.cname
query;
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) { ?>
<input type="checkbox" id="<?=$row['cname'];?>" name="cat[]" value="<?=$row['id'];?>" <?=$row['checked'];?>>
<?
}
?>