I have 2 tables in my DB - Categories and Products.
In products table, I am using a foreign key field named category_id which contains comma separated category ids to which that product belongs to.
Now I am using a multi select drop down using which the admin can select multiple categories for which that product belongs to.
Now while editing the product, I want to display all the categories from categories table along with the selected categories for that particular product should be marked as selected.
My query is something like below but my problem is that it repeats the categories values multiple times. I dont want to repeat the values. Any help would be appreciated.
<?PHP
$query_product = "SELECT * from cmco_products where product_id=$product_id";
$res_product = mysqli_query($conn, $query_product) or die(mysqli_error($conn));
$numrows = mysqli_num_rows($res_product); //check if the record existis into the DB
if($numrows > 0) //if the record exists into the DB
{
$row_product = mysqli_fetch_object($res_product);
$cat_id = explode(",", $row_product->category_id);
?>
<select class="form-control show-tick" name="multi_categories_id" id="multi_categories_id" multiple>
<?php
$query_categories = "SELECT * from cmco_categories order by date_created desc";
$res_categories = mysqli_query($conn, $query_categories) or die(mysqli_error($conn));
$x = 1;
while($row_categories = mysqli_fetch_array($res_categories))
{
foreach($cat_id as $key => $val)
{
$vals[$key] = trim($val)."<br />";
$qry_cat = "SELECT * from cmco_categories where category_id =".trim($val);
$res_cat = mysqli_query($conn, $qry_cat) or die(mysqli_error($conn));
$row_cat = mysqli_fetch_array($res_cat);
?>
<option value="<?php echo $row_categories['category_id']; ?>" <?php if($row_categories['category_id'] == trim($val)) { echo "selected";} ?>><?php echo $row_categories['category_name']; ?></option>
<?php
}
}
?>
</select>
<?PHP
}
?>
Thanks
Related
I have an order form where the product is selected from a database with the method "select" and "option". I show screenshot and code
Code:
<div class="form-group">
<select class="form-control" name="productName[]" id="productName<?php echo $x; ?>" onchange="getProductData(<?php echo $x; ?>)" >
<option value="">-- Selecciona --</option>
<?php
$productSql = "SELECT * FROM product WHERE active = 1 AND status = 1 AND
quantity != 0";
$productData = $connect->query($productSql);
while($row = $productData->fetch_array()) {
$selected = "";
if($row['product_id'] == $orderItemData['product_id']) {
$selected = "selected";
} else {
$selected = "";
}
echo "<option value='".$row['product_id']."'
id='changeProduct".$row['product_id']."' ".$selected."
>".$row['product_name']."</option>";
} // /while
?>
</select>
</div>
I would like to transform this way of selecting a product (droping-down list using "select") for an order form in a field that can be input the first letters of the name of the product or reference and I can see in the drop-down list those references that match instead of all references in table. I know that "input" and "post" should be used and in the query to the database something like:
// Conexión a la base de datos y seleccion de registros
$con=mysql_connect("localhost","xxxxx_xxxx","xxxxxxx");
$sql = "SELECT * FROM product WHERE referencia, product_name, position_store
like ‘%$buscar%’ ORDER BY id DESC";
mysql_select_db("xxxxxxx_stock", $con);
$result = mysql_query($sql, $con);
The idea is to do what I show in the screenshot:
Once the matches appear, you can select the one that interests you. I'm not sure how to structure the changes to convert the initial code into what I'm looking for.
I appreciate any help.
I have a list of categories associated with a post in a database. Here is an example of what the categories look like in the database (PID = PostID and CATID = Category ID).
PID CATID
1 34
1 12
1 15
I have a php form that displays a list of all categories next to an array of checkboxes using name = "catid[]". I need to know the best practice to query the database and and write PHP code so that it pre-check the boxes for categories 12, 15, and 34 using the CHECKED attribute in each appropriate form checkbox for the POST with an PID of 1.
Here is the function that produces the list of categories.
function categoryTree($pid, $cat_type, $parentid = 0, $sub_mark = ''){
global $db;
$query = $db->query("SELECT * FROM categories WHERE parentid = $parentid and cat_type = '$cat_type' ORDER BY parentid, category ASC");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
if ($row['parentid'] == 0) {
echo '<div><label><span class="font-md">'.$sub_mark.$row['category']."</span></label></div>";
} else {
echo '<div class="checkbox" style="padding-left:15px;padding-top:5px;padding-bottom:5px;"><label><input name="catid[]" type="checkbox" value="'.$row['catid'].'" class="checkbox style-0"><span>'.$sub_mark.$row['category']."</span></label></div>";
}
categoryTree($cat_type, $row['catid'], $sub_mark.' ');
}
}
}
Thanks in advance.
Assuming you have the array of checked categories ids as $checkedCategories you could do something like this.
<?php foreach ($categories as $category) : ?>
<input name="catid[]"
value="<?php echo $category['id']; ?>"
<?php echo in_array($category['id'], $checkedCategories)? 'checked':'' ?>>
<?php echo $category['name']; ?>
<br>
<?php endforeach; ?>
I have a table with column names id, name, category, title,and news. Once select query executes I get all the required data from table but i need to split up this data into two div according to the category they belong. there are two different categories, say A and B.
I need to show all the data with a column category name A in one div and same with category B.
$sql= "SELECT id, title ,news ,category FROM news ";
$query=mysql_query($sql, $ex)or die(mysql_error()) ;
while($row=mysql_fetch_assoc($query)){
$title=$row['title'];
if($row['category']==' Latest News'){
echo $row['title'];
}
}
My code doesn't work. Any idea?
You can first separate out category wise data into separate arrays.
$cat1 = array();
$cat2 = array();
$sql= "SELECT id, title, news, category FROM news";
$query = mysql_query($sql, $ex) or die(mysql_error());
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_assoc($query))
{
if($row['category'] == 'A')
{
$cat['title'] = $row['title'];
$cat['news'] = $row['news'];
$cat1[] = $cat;
}
else
{
$cat['title'] = $row['title'];
$cat['news'] = $row['news'];
$cat2[] = $cat;
}
}
}
Then you can use foreach to print the data into separate div
if(count($cat1) > 0)
{
foreach($cat1 as $category1_data)
{
echo "<div class='cat1'>";
// data of category A
echo "</div>";
}
}
if(count($cat2) > 0)
{
foreach($cat2 as $category2_data)
{
echo "<div class='cat2'>";
// data of category B
echo "</div>";
}
}
I have 2 tables one 'brand' that stores all the brand names and 'brand_category' that stores all the brand names with their corresponding categories. I have a multiple select box that loads all the items in the 'brand' table when the page loads, but I need to highlight the items that are already submitted by the user previously in the 'brand_category' table with the rest of the brands that are left so that the user knows what are the brands left to be entered in the 'brand_category' table. Below is the code but it doesn't work. Please help to resolve this issue
<div class="selectbox">
<label id="brand" class="brand_label">Brand:</lable>
<?php
echo "<select name='brand' class='cat_brands' multiple>"
<option value='0'>None</option>";
$query = mysql_query("SELECT id, name FROM brand");
while($br_query = mysql_fetch_assoc($query)){
$query_select = mysql_query("SELECT * FROM brand_category WHERE brand_id='".$br_query['id']."'");
while($brnd_select = mysql_fetch_assoc($query_select)){
if($brnd_select['brand_id']==$br_query['id']){
echo "<option style='background-color: red' value='".$br_query['id']."'>".$br_query['name']."</option>";
}
}
}
?>
</div>
The above code only displays the brands entered in the 'brand_category' table rest of the brands are not displayed. I need to display them also.
You forgot to mention else condition inside while loop.Here is the code
<div class="selectbox">
<label id="brand" class="brand_label">Brand:</lable>
<?php
echo "<select name='brand' class='cat_brands' multiple>"
<option value='0'>None</option>";
$query = mysql_query("SELECT id, name FROM brand");
while($br_query = mysql_fetch_assoc($query)){
$query_select = mysql_query("SELECT * FROM brand_category WHERE brand_id='".$br_query['id']."'");
while($brnd_select = mysql_fetch_assoc($query_select)){
if($brnd_select['brand_id']==$br_query['id']){
echo "<option style='background-color: red' value='".$br_query['id']."'>".$br_query['name']."</option>";
}else{
echo "<option value='".$br_query['id']."'>".$br_query['name']."</option>";
}
}
}
?>
</div>
If I have understood your query correctly you are trying to pull out all the brands and to make the ones in the brands_category table to display as selected.
You should better be using MySQL PDO extensions rather than this obsolete method
There are better ways to implement this, e.g. by having just one table or by using a Join query, anyway... this should work.
<select name='brand' class='cat_brands' multiple>
<option value='0'>None</option>
<?php
$query = mysql_query("SELECT id, name FROM brand");
while($br_query = mysql_fetch_assoc($query))
{
$query_select = mysql_query("SELECT * FROM brand_category WHERE brand_id='".$br_query['id']."'");
if(mysql_num_rows($query_select) > 0)
{
while($brnd_select = mysql_fetch_assoc($query_select))
{
echo "<option style='background-color: red' value='".$br_query['id']."' selected='selected'>".$br_query['name']."</option>";
}
}
else
{
echo "<option style='background-color: yellow' value='".$br_query['id']."'>".$br_query['name']."</option>";
}
} ?>
</select>
Am facing troubles in this code, i just want to get all data from table row if the user selected "show all" from the select drop menu.
here is the select menu !
so, this menu grab data from this table, but if he selects All, what is the suitable code to echoing in between option value :)
<b>speciality:</b> <select id="main_mav" name="speciality">
<option value="none">Select speciality:</option>
<option value=""> All specialities </option>
<?php
$result = mysql_query('SELECT speciality FROM visits') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['speciality'].'">'.$row['speciality'].'</option>';
}
?>
</select><br />
That's the Submit form !
if ($region=="All regions" ){
$region=$_POST['""'];
}
else ( $region=$_POST['region']);
$date1 =$_POST['from_date'];
$date2 = $_POST['to_date'];
$product=$_POST['product'];
$speciality=$_POST['speciality'];
$type=$_POST['visit_type'];
sql="SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
$result=mysql_query($sql); ## This line is new.
$num=mysql_numrows($result);
$row = mysql_fetch_array($result);
What's the correct code to enter if user selected " show all in drop menu " ?!
You really need to sanitize your inputs, at least with mysql_real_escape_string!
On to your actual question: just check if $speciality is empty, and generate a different query without the (speciality ='$speciality') condition.
Since your HTML referenced 'specialties' and your PHP referenced 'regions' I'm gonna just stick with 'regions', but here's the idea.
if ($region=="All regions" ){
$sql = 'SELECT id, customer_name, seller_1_name, seller_2_name, FROM visits';
} else {
$region = mysql_real_escape_string($_POST['region']);
$date1 = mysql_real_escape_string($_POST['from_date']);
$date2 = mysql_real_escape_string($_POST['to_date']);
$product = mysql_real_escape_string($_POST['product']);
$speciality = mysql_real_escape_string($_POST['speciality']);
$type = mysql_real_escape_string($_POST['visit_type']);
$sql = "SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
}
$result = mysql_query($sql); ## This line is new.
$num = mysql_numrows($result);
$row = mysql_fetch_array($result);