How to get parent name from its child? - php

I am going to build a dynamic product category with their subs for my final year project. I tried the tree way but somehow it make me confused so i decided to make it as simple. I want to display Clothe's parent name.I want to display it like this
Can i do it so ?
Here is my PHP code
<form method="post" action="product_category_add_exec.php" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-level" class="control-label"> Parent Category</label>
<select class="form-control" name="admin_lid" required="">
<option></option>
<?php
$sql_pcat = "SELECT * FROM product_category";
$select_pcat = mysqli_query($db,$sql_pcat) or die (mysqli_error().$sql_pcat);
$x =1;
while($list_pcat = mysqli_fetch_array($select_pcat))
{
$product_cat_id = $list_pcat['product_cat_id'];
$parent_id = $list_pcat['parent_id'];
$product_cat_name = $list_pcat['product_cat_name'];
?>
<?php
if ($parent_id == 0)
{
?>
<option value = "<?php echo $product_cat_id;?>"><?php echo $product_cat_name; ?></option>
<?php
} else
{
$sql_cat = "SELECT * FROM product_category WHERE parent_id= $parent_id ORDER BY product_cat_name ASC";
$select_cat = mysqli_query($db,$sql_cat) or die (mysqli_error().$sql_cat);
$list_cat = mysqli_fetch_array($select_cat);
$product_cat_id = $list_cat['product_cat_id'];
$parent_id = $list_cat['parent_id'];
$product_cat_name = $list_cat['product_cat_name'];
?>
<option value = "<?php echo $parent_id;?>">--<?php echo $parent_id;?><?php echo $product_cat_name; ?></option>
<?php
}
?>
<?php
$x++;
}
?>
</select>
</div>
<div class="form-group">
<label for="recipient-category" class="control-label">Product Name </label>
<input type="text" class="form-control" id="recipient-category" name="product_cat_name">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-info">Add</button>
<button type ="reset" class ="btn btn-danger">Reset</button>
</div>
</form>
This is my database table
I want to make it appear like this

Related

displaying php in an echo nested in html

I'm trying to echo a piece of text with PHP nested in it, anyone know how? I've tried it like this:
function update_category(){
global $connection;
if (isset($_GET['edit'])) {
$edit_cat_key = $_GET['edit'];
$query = "SELECT * FROM categories WHERE cat_id = {$edit_cat_key }";
$edit_cat_query = mysqli_query($connection, $query);
echo '<form action="categories.php" method="post">
<div class="form-group">
<label for="new-cat-title">New name of category</label>
<input value="<?php if(isset($cat_title)){echo $cat_title;} ?>" type="text" class="form-control" name="new-cat-title">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Rename Category">
</div>
</form>';
while($row = mysqli_fetch_assoc($edit_cat_query)) {
$cat_title = $row['cat_title'];
$cat_id = $row['cat_id'];
}
}
}
It prints: <?php if(isset($cat_title)){echo $cat_title;} ?>
I've tried it like this:
<input value="' . <?php if(isset($cat_title)){echo $cat_title;} ?> . '" type="text" class="form-control" name="new-cat-title">
I'd do it like this (showing only the echo part inside the php code):
echo '<form action="categories.php" method="post">
<div class="form-group">
<label for="new-cat-title">New name of category</label><input value="';
if(isset($cat_title)){echo $cat_title;};
echo '" type="text" class="form-control" name="new-cat-title"></div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Rename Category">
</div>
</form>';
This echoes the two strings (in single quotes) and in between them the variable depending on the condition, keeping the double-quote pairs of the attribute values intact.
Echo is a php statement. You use it to display an output, as a string. What you need is, replacing echo ' with ?> and </form>'; with </form><?php. These closing(?>) and opening(<?php) tags let you decide which part of your document should be processed as PHP. So PHP will ignore the rest of the document.
Based on Johannes' answer I managed to figure out an answer. I need to close the PHP tags and instead of echoing the wished result, put it in the while loop.
Like this:
function update_category(){
global $connection;
if (isset($_GET['edit'])) {
$edit_cat_key = $_GET['edit'];
$query = "SELECT * FROM categories WHERE cat_id = {$edit_cat_key }";
$edit_cat_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($edit_cat_query)) {
$cat_title = $row['cat_title'];
$cat_id = $row['cat_id'];
?>
<input value="<?php echo $cat_title; ?>" type="text" class="form-control" name="cat_title">
<?php
}
}
}

All the checkboxes checked by previous user must be disabled

I want all the records(i.e seats selected by the user) should be disabled but this code only fetches the last record and disables it.
<?php
$sqlcheck = "select * from booknow where hotel ='$test'";
$resultcheck = mysqli_query($conn,$sqlcheck);
while($rowcheck = mysqli_fetch_array($resultcheck))
{
$checkedcheck = $rowcheck['book'];
$expcheck=explode(",",$checkedcheck);
}
?>
<?php $i=1;
while ($i<=$tab)
{?>
<div class="TWO">
<div class="check"style="height:40px;width:120px;">
<div class="seconda">
</div>
<div class="secondb">
<input type ="checkbox"name="checky[]"class="tabtwo"style="width:30px;height:30px;"
value="<?php echo "Two-seater A".$i;?>"
<?php if(in_array("Two-seater A".$i, $expcheck)) {?>
onclick="this.checked=this.defaultChecked"<?php } else {echo "none";}?>>
</div>
<div class="secondc">
</div>
<label for="c1"style="margin-left:40px;"><b>A<?php echo$i?></b>
</label>
</div>
</div>
<?php
$i++;
}
?>
Try below code
while($rowcheck = mysqli_fetch_array($resultcheck))
{
$checkedcheck = $rowcheck['book'];
$expcheck[] =explode(",",$checkedcheck);
}

multiple checkbox check if value in database?

code:
<?php
$id = $_GET['id'];
$sql = "select * from admin_menu where id = '$id'";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
$menu_name = $row['menu_name'];
$menu_link = $row['menu_link'];
$priority = $row['priority'];
$admin_id = explode(",", $row['admin_id']);
}
if(isset($_POST['update']))
{
$admin_id = $_POST['admin_id'];
$chk="";
foreach($admin_id as $chk1)
{
$chk .= $chk1.",";
}
$menu_name = $_POST['menu_name'];
$menu_link = $_POST['menu_link'];
$priority = $_POST['priority'];
$sql = "update admin_menu set menu_name = '$menu_name', menu_link = '$menu_link', priority = '$priority', admin_id = '$chk' where id = '$id'";
$result = mysqli_query($link,$sql);
if($result == true)
{
$msg .= "<h3 style='color:green;'>update</h3>";
}
else
{
$msg .= "<h3 style='color:red;'>Error!</h3>";
}
}
?>
<form name="myform" method="post" >
<div class="row">
<label for="Producer_firstname">Admin Name</label>
<?php
foreach ($admin_id as $admin_id)
{
$chk = "";
if (in_array($chk, $admin_id))
{
$chk = 'checked="checked" ';
}
echo '<input type="checkbox" name="admin_id[]" value="'.$admin_id.'" '.$chk.'/><br/>';
}
?>
</div>
<div class="row">
<label for="Producer_firstname">Menu Name </label>
<input size="60" maxlength="255" name="menu_name" id="menu_name" value="<?php echo $menu_name; ?>" type="text" />
</div>
<div class="row">
<label for="Producer_lastname" >Menu Link </label>
<input size="60" maxlength="255" name="menu_link" id="menu_link" type="text" value="<?php echo $menu_link; ?>" />
</div>
<div class="row">
<label for="Producer_lastname" >Priority</label>
<select name="priority" id="priority">
<option value="<?php echo $priority; ?>"><?php echo $priority; ?></option>
<option value="">choose any one</option>
<option value="1">1</option>
<option value="0">0</option>
</select>
</div>
<div class="row buttons">
<button type="submit" name='update' id='update'>update Menu</button>
</div>
</form>
In this code I am fetching multiple checkbox value from table admin2 and I want when I update form value checkbox check if the value of checkbox is exist into database. How can I fix it ?
Thank You
Your code has few issues,
1. Update should be done before select query
2. List of admin not managed separately
3. Priority radio buttons not managed properly
Additional Suggestions,
1. Use prepare query statements
2. use implode for appending multiple values instead of foreach
3. print admin names before checkboxes
<?php
$id = $_GET['id'];
if(isset($_POST['update']))
{
$chk = implode(',', $_POST['admin_id']);
$menu_name = $_POST['menu_name'];
$menu_link = $_POST['menu_link'];
$priority = $_POST['priority'];
$sql = "update admin_menu set menu_name = '$menu_name', menu_link = '$menu_link', priority = '$priority', admin_id = '$chk' where id = '$id'";
$result = mysqli_query($link,$sql);
$msg = "";
if($result == true)
{
$msg .= "<h3 style='color:green;'>update</h3>";
}
else
{
$msg .= "<h3 style='color:red;'>Error!</h3>";
}
echo $msg;
}
$sql = "select * from admin_menu where id = '$id'";
$result = mysqli_query($link,$sql);
$row = mysqli_fetch_array($result);
$menu_name = $row['menu_name'];
$menu_link = $row['menu_link'];
$priority = $row['priority'];
$admin_id = explode(",", $row['admin_id']);
$admins = array('admin1', 'admin2', 'admin3', 'admin4', 'admin5', 'admin6', 'admin7', 'admin8');
?>
<form name="myform" method="post" >
<div class="row">
<label for="Producer_firstname">Admin Name</label>
<?php
foreach ($admins as $admin)
{
$chk = "";
if (in_array($admin, $admin_id))
{
$chk = 'checked="checked" ';
}
echo $admin.' <input type="checkbox" name="admin_id[]" value="'.$admin.'" '.$chk.'/><br/>';
}
?>
</div>
<div class="row">
<label for="Producer_firstname">Menu Name </label>
<input size="60" maxlength="255" name="menu_name" id="menu_name" value="<?php echo $menu_name; ?>" type="text" />
</div>
<div class="row">
<label for="Producer_lastname" >Menu Link </label>
<input size="60" maxlength="255" name="menu_link" id="menu_link" type="text" value="<?php echo $menu_link; ?>" />
</div>
<div class="row">
<label for="Producer_lastname" >Priority</label>
<select name="priority" id="priority">
<option value="1" <?php if($priority == 1) echo "selected='selected'"; ?>>1</option>
<option value="0" <?php if($priority == 0) echo "selected='selected'"; ?>>0</option>
</select>
</div>
<div class="row buttons">
<button type="submit" name='update' id='update'>update Menu</button>
</div>
</form>

PHP Multiple Checkbox;

I have problem, I am trying to create a form with a few checkboxes, each assigned a different value, i just can update value first and last, between not working, can you help me?
Check.php
<div class="top-on">
<div class="top-on1">
<p class="text-center"> <?php echo $row['username'];?></p>
<br>
<select class="form-control col-sm-12" name="edit_level">
<?php
global $pdo;
$sql = $pdo->query("SELECT * FROM level");
while ( $row_c = $sql->fetch(PDO::FETCH_ASSOC) ) {
?>
<option <?php if($row_c["level"]==$row["level"])
{
echo "selected=\"selected\"";
}?>
value="<?php echo $row_c['level']?>"> <?php echo $row_c['name'];?> </opition>
<?php } ?>
</select>
</div>
<label style="float: right;">
<input type="checkbox" class="checkbox" name="idlevel[]" value="<?php echo $row['id'];?>"> </label>
<div class="clearfix"> </div>
</div>
page Control.php
function edit_level(){
global $pdo;
$sql1="SELECT * From user ";
$stmt1 = $pdo->query($sql1);
if(isset($this->btnlevel))
{
for($i=0;$i<$stmt1->rowCount();$i++){
$elve=$this->idlevel[$i];
$sql ="UPDATE user SET level='$this->editlevel' WHERE id='".$elve."'";
$upt = $pdo->prepare($sql);
$upt->execute();
}
}
}

Get all url GET values and some random values after insert in db

I have a custom form wich have ~6 custom intuts and 1-20 can be random how can i get random inputs with $_GET and put them in col atributes:
<form method="GET" action="products.php" enctype="multipart/form-data">
<li>
<label>Name</label>
<input required class="scurtI" type="text" name="prod_name" value="<?= $_GET['prod_name'] ?>" placeholder="">
</li>
<li>
<label>Description</label>
<input required class="scurtI" type="text" name="prod_descr" value="<?= $_GET['prod_descr'] ?>" placeholder="">
</li>
<li>
<label>Meta - Title</label>
<input required class="scurtI" type="text" name="prod_meta_title" value="<?= $_GET['prod_meta_title'] ?>" placeholder="">
</li>
<li>
<label>Meta - Description</label>
<input required class="scurtI" type="text" name="prod_meta_desc" value="<?= $_GET['prod_meta_desc'] ?>" placeholder="">
</li>
<li>
<label>Meta - Keywords</label>
<input required class="scurtI" type="text" name="prod_meta_keys" value="<?= $_GET['prod_meta_keys'] ?>" placeholder="">
</li>
<li>
<label>Price</label>
<input required class="scurtI" type="text" name="prod_price" value="<?= $_GET['prod_price'] ?>" placeholder="">
</li>
<li>
<label>Currency</label>
<select name="prod_curency" class="focusSelect">
<option value="RON">RON</option>
<option value="USD">USD</option>
<option value="EUR">EUR</option>
</select>
</li>
<li>
<label>Units</label>
<input required class="scurtI" type="text" name="prod_units" value="<?= $_GET['prod_units'] ?>" placeholder="">
</li>
<li>
<label>Category</label>
<select name="prod_ctgy" class="focusSelect">
<?php
$sql = "SELECT * FROM `categorii` WHERE `cat_id`";
$connect = mysqli_query($db_connect, $sql);
while (($item = mysqli_fetch_array($connect)))
{
?>
<option
value="<?= $item['cat_id'] ?>"><?= str_repeat(' ', 6 * $item['section_level']) ?><?= $item['section_name'] ?></option>
<?php
}
?>
</select>
</li>
<li>
<label>Manufacturer</label>
<select name="prod_manu" class="focusSelect">
<?php
$sql = "SELECT * FROM `manufactures` WHERE `id`";
$connect = mysqli_query($db_connect, $sql);
while (($item = mysqli_fetch_array($connect)))
{
?>
<option
value="<?= $item['manu_code'] ?>"><?= $item['manu_name'] ?></option>
<?php
}
?>
</select>
</li>
<li>
<label>Atributes</label>
<select name="prod_atr_id" class="focusSelect">
<?php
if (isset($_GET['prod_atr_id'])){
echo "<option selected value=".$_GET['prod_atr_id'].">Selected</option>";
}
$sql = "SELECT * FROM `products_atributes` WHERE `prod_atr_id`";
$connect = mysqli_query($db_connect, $sql);
while (($item = mysqli_fetch_array($connect)))
{
?>
<option
value="<?= $item['prod_atr_id'] ?>"><?= $item['prod_atr_name'] ?></option>
<?php
}
?>
</select>
<button class="btn btn-sm btn-primary" type="submit" name="next_add_product">Next</button>
</li>
<?php
if(isset($_GET['prod_atr_id']))
{
list_atributes($_GET['prod_atr_id']);
?>
<li>
<label>Main Image </label>
<input type="file" name="prod_image" style="">
</li>
<li>
<input class="btn btn-sm btn-primary" type="submit" name="prod_add_new" value="Add Product">
</li>
</form>
<?php
}
if(isset($_GET['prod_add_new']))
{
//$prod_name = $_GET['prod_name']
foreach($_GET as $key => $value){
echo $key . " : " . $value . "<br />\r\n";
}
//print_r($all_gets);
}
?>
function list_atributes($id)
{
global $db_connect;
$atribute_detect = $id;
if ($atribute_detect > 0){
$sql = "SELECT * FROM `products_atributes` WHERE `prod_atr_parent` = '".$atribute_detect."'";
$connect = mysqli_query($db_connect, $sql);
$count = mysqli_num_rows($connect);
if ($count == 0)
{
$sql = "SELECT * FROM `products_atributes` WHERE `prod_atr_id` = '".$atribute_detect."'";
}
if ($count > 0)
{
$sql = "SELECT * FROM `products_atributes` WHERE `prod_atr_parent` = '".$atribute_detect."'";
}
$connect = mysqli_query($db_connect, $sql);
while (($item = mysqli_fetch_array($connect)))
{
echo "
<li>
<label>".$item['prod_atr_name']."</label>
<input required class='scurtI' type='text' name='".$item['prod_atr_code']."' value='".$_GET[$item['prod_atr_code']]."' >
</li>
";
}
}
}
And now i get this restults
prod_name : sadfasd
prod_descr : asdfasd
prod_meta_title : ghdf
prod_meta_desc : gs
prod_meta_keys : asdf
prod_price : 235243
prod_curency : RON
prod_units : 1243123
prod_ctgy : 16
prod_manu : 70123825
prod_atr_id : 1
26126933 : 1243ghz
40320861 : 2000mb
47789694 : 64bit
prod_image : log_masacru.png
prod_add_new : Add Product
How can i get this values, for all with prefix prod_ i have special cols, and for that with numbers i must put them all in a single col and after i must use them to extract as id=40320861 value=2000mb, etc. How can i build the code to add in db random number of $_GET values in a single column
My understanding of the question is that you need to save the following GET variables :
26126933 : 1243ghz
40320861 : 2000mb
47789694 : 64bit
in a single db column where the keys are randomly generated ( unknown).
ANSWER:
Save the attributes in a multidimensional array, for this set their name values in the html to something like prod_attr_value[][$RANDOM_NUMBER], this will make the attributes values in the array $_GET['prod_attr_value'] .
Then to save in these values in a single db column, you can simply json_encode($arr).
hth
UPDATE:
<input required class='scurtI' type='text' name='".$item['prod_atr_code']."' value='".$_GET[$item['prod_atr_code']]."' >
The above creates a $_GET var with a key having the name (not value) of the value of $item['prod_atr_code'].
Changing the name to something like prod_attr_values_arr[][$item['prod_atr_code']], will on sending the request create a array $GET['prod_attr_values_arr']

Categories