My question is that , I want my output is shown in the select tag, in the form of
Dropdown list(treeview Structure) and also i wanted to select multiple categories
with the help of Using html tag i.e, Checkbox.
<?php
function getCategory($parent_id){
$con = connect_db();
$sql = "select ocd.category_id,ocd.name, occ.parent_id from oc_category_description ocd, oc_category occ where ocd.category_id=occ.category_id and parent_id='".$parent_id."' ORDER BY name";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0)
{
echo "<ul>";
while ($row = mysqli_fetch_object($result))
{
echo "<li>".$row->name.'('.$row->category_id.')'."</li>";
getCategory($row->category_id);
}
echo "</ul>";
}
}
$parent_id = 0;
getCategory($parent_id);
?>
Here the output:-
enter link description here
You can show your results in a Dropdown list with these lines of code.Hope this is what you wanted.
if (mysqli_num_rows($result) > 0)
{
echo '<select name="nameUwanted">';
while ($row = mysqli_fetch_object($result))
{
echo "<option value='". $row->name ."'>" .$row->name.'('.$row->category_id.')'."</option>" ;
getCategory($row->category_id);
}
echo "</select>";
}
}
Related
I currently have three tables. One is housekeeping_employee, second one is housekeeping_benefit, and the third one is the housekeeping_employeebenefits. housekeeping_Employeebenefits is my table where a benefit is assigned/inserted to an employee. Here's my code for displaying the housekeeping_employee in a dropdown, displaying the housekeeping_benefit in checkboxes, and lastly the submit button for the employeebenefits to insert the benefits into the employee.
<?php
if(!isset($_POST['insert']))
{
echo "<br>";
echo "<form method ='POST' action='' >";
$ctr = 0;
$employee = mysqli_query($conn, "SELECT * FROM housekeeping_employee") or DIE("Could not Select");
echo "<select class='indent' name = 'Employee'>";
echo "<option>Select</option>";
while($listEmployee = mysqli_fetch_array($employee))
{
echo "<option value = ".$listEmployee['employeeid'].">".$listEmployee['firstname']."</option>";
}
echo "</select>";
echo "<br><br>";
echo "<div class='indent'>";
$benefits = mysqli_query($conn, "SELECT * FROM housekeeping_benefit")or DIE("SELECT Query not working.");
if(mysqli_num_rows($benefits))
{
while($row = mysqli_fetch_array($benefits))
{
$ctr++;
echo "<h6><input type = 'checkbox' name = 'benefits".$ctr."' value ='".$row['benefitid']."' >".$row['benefitname']." </input></h6> <br>";
if($ctr == 5){
echo"<br>";
$ctr=0;
}
}
echo "<br><input type='submit' class='btn btn-info' name='insert' value='Submit'/>";
}
echo "<input type='hidden' name='ctr' value='".$ctr."'/>";
echo "</form>";
}
else
{
$employeeid = $_POST['Employee'];
for($a = 1; $a <= $_POST['ctr'] ; $a++)
{
if(isset($_POST['benefits'.$a]))
{
$benefitid = $_POST['benefits'.$a];
//echo "INSERT INTO `housekeeping_employeebenefits`(`employeeid`,`benefitid`) VALUES('$employeeid','$benefitid')";
mysqli_query($conn, "INSERT INTO `housekeeping_employeebenefits`(`employeeid`,`benefitid`) VALUES('$employeeid','$benefitid')") or DIE("Insert query is not working");
echo "Employee to Benefits successful!";
}
}
}
?>
I did a join query so I can see the benefits under the employee. Here's my query
SELECT * FROM housekeeping_employee A,housekeeping_benefit B,housekeeping_employeebenefits C where (A.employeeid = A.employeeid and B.benefitid = C.benefitid) and A.employeeid = 2
My friend suggested that I use an onclick which I've never heard or used before so my main question is really far from what I have at the moment but here it is. So from my original code I plan to remove the code that displays all checkboxes of benefits and instead replace it with only displaying the checkboxes of benefits that the employee has. How do I do a trigger that will display the benefit checkboxes under the employee when I choose/click an employee from the dropdown?
EDIT:
If onclick is not what I need, what syntax/command do I need?
code:
<?php
if(isset($_POST['save']))
{
$checkbox1=$_POST['company_name'];
$chk=implode(',',$checkbox1);
$sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."'";
$value = mysqli_query($link,$sql);
if($value == true)
{
$msg .="<h5 style='color:green'>Successfull</h5>";
}
else
{
$msg .="<h5 style='color:red'>Error!</h5>";
}
}
?>
<?php
extract($_POST);
$sql = mysqli_query($link,"select * from placement order by company_name ASC");
while ($row = mysqli_fetch_array($sql))
{
echo "<li>
<input type='checkbox' name='company_name[]' id='company_name' value=".$row['image_name']."> ".$row['company_name']."<br/>
</li>";
}
?>
Here I want to checked checkbox if the string is already in mysql database like image1,image2,image3. So, How can I do this ? please help
Thank You
Check in your while loop and assign the checked value if condition is satisfied. Like shown in below code.
while ($row = mysqli_fetch_array($sql))
{
$isCheked = "";
if($row['isCheked']) {//test if the values is checked in the db
$isCheked = "checked";
}
echo "<li><input type='checkbox' ".$isCheked." name='company_name[]' id='company_name' value=".$row['image_name']."> ".$row['company_name']."<br/></li>";
}
Try like this.Use ternary operator(:?) for checking whether the name is set or not.
while ($row = mysqli_fetch_array($sql))
{
$name = $row['image_name'];
$check = isset($name)?"checked":"";
echo "<li>
<input type='checkbox' name='company_name[]' id='company_name' value='".$name."'".$check." > ".$row['company_name']."<br/>
</li>";
}
Okay, so I'm working on a PHP site, I have descriptions of the product under the image, what I'm need to do is limit the amount of characters on the page and add a click here or.. For the viewers to be directed to that products page to see the full description. FYI very new to PHP, here is what I have so far. So question is do I use PHP or javascript or both and how do I do that ?
<?php
// Include need php scripts
require_once ("Includes/simplecms-config.php");
require_once ("Includes/connectDB.php");
include ("Includes/header.php");
if (!empty($_GET['cat'])) {
$category = $_GET['cat'];
$query = mysqli_query($db, "SELECT * FROM products WHERE category = '".$category."'");
} else {
$query = mysqli_query($db, "SELECT * FROM products");
}
if (!$query) {
die('Database query failed: ' . $query->error);
}
?>
<section>
<div id="productList">
<?php
$row_count = mysqli_num_rows($query);
if ($row_count == 0) {
echo '<p style="color:red">There are no images uploaded for this category</p>';
} elseif ($query) {
while($products = mysqli_fetch_array($query)){
$file = $products['image'];
$product_name = $products['product'];
$image_id = $products['id'];
$price = $products['price'];
$desc = $products['description'];
echo '<div class="image_container">';
echo '<a href="viewProduct.php?id=' . $image_id . '"><p><img src="Images/products/'.$file.'" alt="'.$product_name.'" height="250" /></p>';
echo $product_name . "</a><br>$" . $price . "<br>" . $desc;
echo '</div>';
if (is_admin()){
echo "<a href='deleteproduct.php'><button>delete</button></a>";
}
}
} else {
die('There was a problem with the query: ' .$query->error);
}
mysqli_free_result($query);
?>
</div>
</section>
<?php include ("Includes/footer.php"); ?>
using strlen and substr we can achieve this
$length = 150
$x = 'string';
if(strlen($x)<=$length)
{
echo $x;
}
else
{
$y=substr($x,0,$length) . '...';
echo $y;
}
Hi so the here is my question,
I have a Database containing Categories and Subcategories.
I have two dropdown boxes (select). I want them both to be populated by using PHP/MYSQL.
My Categories have been generated:
<select name="prod_cat">
<?php
include("php/dbconnect.php");
$sql = "SELECT * FROM categories";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_array($result))
{
echo '<option value="'. $row["cat_name"] .'">'. $row["cat_name"] .'</option>';
}
}
else{echo "No categories were found!";}
mysqli_close($conn);
?>
</select>
I want the subcategories to load when the category has changed.
My SQL will look something like this:
SELECT subcat_name FROM subcategories WHERE cat_name = '$prod_cat'
I need to get the value from the categories dropdown and store it as a variable.
In the past I have done something similar in javascript:
var subcat=document.forms["form"]['subcat'].value;
and then changed the value of another dropdown by calling an onChange(); function like:
document.getElementById('subcat').innerHTML='<option value=""></option>';
I hope someone can point me in the right direction! Should I be looking into AJAX, JavaScript or can it all be done with PHP?
Thank you.
add an id on category select box , like this -
<select name="prod_cat" id="prod_cat">
<?php
include("php/dbconnect.php");
$sql = "SELECT * FROM categories";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_array($result))
{
echo '<option value="'. $row["cat_name"] .'">'. $row["cat_name"] .'</option>';
}
}
else{echo "No categories were found!";}
mysqli_close($conn);
?>
</select>
for subcategory -
<select id='sub_cat' name='sub_cat'></select>
write your script something like this -
<script>
$("#prod_cat").change(function(){
//get category value
var cat_val = $("#prod_cat").val();
// put your ajax url here to fetch subcategory
var url = 'www.example/fetch_subcategory.php';
// call subcategory ajax here
$.ajax({
type:"POST",
url:url,
data:{
cat_val : cat_val
},
success:function(data)
{
$("#sub_cat").html(data);
}
});
});
</script>
add code in ajax calling page -'www.example/fetch_subcategory.php'
<?php
$prod_cat = $_POST['cat_val'];
$sql = "SELECT subcat_name FROM subcategories WHERE cat_name = '$prod_cat'";
$result = mysqli_query($conn, $sql);
$msg ='';
if (mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_array($result))
{
$msg =.'<option value="'. $row["sub_cat_name"] .'">'. $row["sub_cat_name"] .'</option>';
}
}
else{$msg .="No categories were found!";}
echo $msg;
mysqli_close($conn);
?>
Do something like this, and hope it will work for you.
My select consists of two fields and the text field only picks up one of them. Here is my code:
<script>
function ProdValue(data) {
document.getElementById("ProdName").value = data.value;
}
</script>
<select name="ProdName" id="ProdName" onchange ="ProdValue(this)">
<?php
$sql = "Select * from tblProduct";
if ($result = mysqli_query($conn, $sql));
while ($row = mysqli_fetch_assoc($result))
{
echo "<option value='". $row['Brand']."', '".$row['ProductName']."'>".$row['Brand']." ".$row['ProductName']. '</option>';
}
?>
</select>
How can I get ProductName too?
Since your option value contains brand and productname separated by comma, you could do:
function ProdValue(data) {
var optval = data,
prod_name = optval.split(",")[1];
console.log( prod_name ); //here is your product name
document.getElementById("ProdName").value = optval;
}
use this instead. If both columns are actually defined as NOT NULL, CONCAT() will be quite enough:
<?php
$sql = "Select CONCAT(Brand, ProductName) as CombineColumn from tblProduct";
if ($result = mysqli_query($conn, $sql))
{
while ($row = mysqli_fetch_assoc($result))
{
echo "<option value='". $row['CombineColumn']."'>".$row['CombineColumn']."</option>";
}
}
?>