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.
Related
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.
Please can anyone suggest how I can go about achieving this in php I have two tables categories and article. The categories table has two fields id and names
article table contains id category_id title. One category can be tie to many article title in the article table. Please how can I retrieve all titles belonging to each categories in my article table without using the get variable.
The below code is retrieving just each categories but not pulling the article titles for each
Categories Class
public static function find_all_categoryid() {
return self::find_by_sql("SELECT a.title, b.names, b.id FROM article a LEFT OUTER JOIN article_categories b ON a.category_id=b.id GROUP BY a.category_id");
}
Index Page
<?php $cats = Categories::find_all_categoryid(); ?>
<?php foreach($cats as $cat): ?>
<h4><?php echo $cat->names; ?></h4>
<p><?php echo $cat->title; ?></p>
<?php endforeach; ?>
can anyone please assist. Thank you
i really want to do something like this http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_optgroup
but i have The main Categories and the subcategories in a DB, and i don't really know how to manage this in a while cycle. How can i do this ? Actually the cycle is something like that, but because of my necessities i can't use it anymore
<select class="form-control" id="subcategory" name="subcategory" required>
<?php
$categories="SELECT Name,Category FROM SubCategory ORDER BY Category ASC, Name ASC;";
$query_categories=mysqli_query($connh,$categories);
while($rows_categories=mysqli_fetch_array($query_categories)){
echo'<option>'.$rows_categories['Category'].'/'.$rows_categories['Name'].'</option>';
}
?>
</select>
p.s: Category is the main Category, Name is the subcategory
I want to have the main category into <optgroup> but because of one Category contains more than 1 subcategory, I don't know how to place <optgroup> in a way that it shows the main category just 1 time and not for each record.
In your case, need flag if current main category has added on your html, if I understand fields in your database, Name and Category has string values and you don't need two queries or two whiles but only check variable, like this:
<select class="form-control" id="subcategory" name="subcategory" required>
<?php
$categories="SELECT Name,Category FROM SubCategory ORDER BY Category ASC, Name ASC;";
$query_categories=mysqli_query($connh,$categories);
$currentMainCategory = null;
while($rows_categories=mysqli_fetch_array($query_categories)){
// check if your current category was diferent of row category
if($currentMainCategory != $rows_categories['Category']) {
// check if is not null (for close </optgroup>)
if(!is_null($currentMainCategory)) {
echo '</optgroup>';
}
// set your new current category for this loop
$currentMainCategory = $rows_categories['Category'];
echo '<optgroup label="'. $rows_categories['Category'] .'">';
}
// do not forget put value of this category if you needed
echo'<option>' . $rows_categories['Name'] . '</option>';
}
// then check again for close last optgroup if is opened
if(!is_null($currentMainCategory)) {
echo '</optgroup>';
}
?>
</select>
You're not too far off from accomplishing your basic goal, which is to display the categories and sub-categories in an optgroup.
You can either:
Query MySQL with two separate queries and combine them using PHP against both the SubCategory and Category tables.
Perform two foreach loops, each containing an optgroup for the respective Category and SubCategory results.
or
Query MySQL with one query using OUTER JOIN to retrieve one result set against both the SubCategory and Category tables (possibly even a UNION).
Perform two foreach loops, each containing an optgroup for the respective Category and SubCategory results, but only display the Category results if it came from the Category table and vice versa.
Here's some reference material:
MySQL Joins: https://www.sitepoint.com/understanding-sql-joins-mysql-database/
MySQL Union: Combining the results of two separate MySQL queries
PHP Foreach loop: http://php.net/manual/en/control-structures.foreach.php
PHP Array Merging: http://www.w3schools.com/php/func_array_merge.asp
<select class="form-control" id="subcategory" name="subcategory" required>
<?php
$categories="SELECT Name,Category FROM SubCategory ORDER BY Category ASC, Name ASC;";
$query_categories=mysqli_query($connh,$categories);
$category_name_old = '';
while($rows_categories=mysqli_fetch_array($query_categories)){
if($rows_categories['Category'] != $category_name_old) {
echo "<optgroup label=".$rows_categories['Category'].">";
echo "<option>".$rows_categories['Name']."</option>";
}
else {
echo "<option>".$rows_categories['Name']."</option>";
}
$category_name_old = $rows_categories['Category'];
if($category != $category_name_old) {
echo "</optgroup>";
}
}
}
?>
</select>
You can do like this
Create an array with your main categories, with subcategories as a sub-array.
<select class="form-control" id="subcategory" name="subcategory" required>
<?php
$option_list = array();
$categories="SELECT Name,Category FROM SubCategory ORDER BY Category ASC, Name ASC;";
$query_categories=mysqli_query($connh,$categories);
while($rows_categories=mysqli_fetch_array($query_categories)){
$option_list[$rows_categories['Category']][] = $rows_categories['Name'];
}
// this is what we're aiming for
// $option_list = array("Maincat1"=>array("subcat1","subcat2","subcat3"),"Maincat2"=>array("subcat4","subcat5"));
foreach($option_list as $maincat=>$subcats){
echo "<optgroup label='".$maincat."'>";
foreach($subcats as $subcat){
echo "<option value='$subcat'>$subcat</option>";
}
echo "</optgroup>";
}
?>
</select>
<?php
$conn = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password,$mysql_database) or die("Could not connect database");
$result = mysqli_query($conn,"select distinct Category from SubCategory");
echo '<select class="form-control" id="subcategory" name="subcategory" required>';
while($row = mysqli_fetch_assoc($result)){
echo '<optgroup label="'. $row['Category'] .'">';
$data = mysqli_query($conn,"SELECT Name,Category FROM SubCategory where Category = '".$row['Category']."'");
while($value = mysqli_fetch_assoc($data)){
echo '<option>'. $value['Name'] .'</option>';
}
echo '</optgroup>';
}
echo '</select>';
?>
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 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">