PHP show data in checkbox - php

How do I go about showing checkboxes that are in the jokecategory table as being selected, and those that arent to be shown but not checked. This is what I currently have:
<div class="control-group">
<?php
$sql = 'SELECT jokecategory.joke_id, jokecategory.category_id, category.name
FROM jokecategory
INNER JOIN category ON category.id = jokecategory.category_id
WHERE joke_id= :joke_id';
$stmt = $dbConnection->prepare($sql);
$stmt->bindValue(':joke_id', $id);
$stmt->execute();
?>
<fieldset>
<legend class="control-label">Categories:</legend>
<?php foreach ($stmt as $row) { ?>
<div class="controls">
<label for="category<?php echo($row['category_id']);?>">
<input type="checkbox" id="category<?php echo $row['category_id']; ?>" name="checkbox[]" value="<?php echo $row['category_id']; ?>" checked>
<?php echo($row['name']); ?></label>
</div>
<?php } ?>
with (data within the category table):
<?php $sql = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql) as $data) { ?>
<div class="controls">
<label for="category<?php echo($data['id']);?>">
<input type="checkbox" name="categories[]" id="category<?php echo($data['id']); ?>" value="<?php echo($data['id']);
?>">
<?php echo($data['name']); ?></label>
</div>
<?php } ?>
</fieldset>
</div>
Its in two segments at the moment as I was not sure on how to show the output the data into one merged group. so it goes like:
Joke id: xxxxx
joke_text: xxxxxx
author_name: xxxxx
categories
1: [x]
2: [x]
3: [ ]
4: [ ]

I am not sure, but - is $data["category_id"] really an array? If so, you could use the keypair in that array like this:
<?php foreach ($data['category_id'] as $id => $checkbox) { ?>
<input type="checkbox" id="<?php echo $id; ?>" name="checkbox[]" />
<?php } ?>

probably it would be like below check your sql query and add category name to fetch and display eg. category_name used below
and make some changes as below
<fieldset>
<legend class="control-label">Categories:</legend>
<?php foreach ($data as $checkbox) { ?>
<div class="controls">
<label for="category<?php echo($checkbox['category_id']); ?>">
<input type="checkbox" name="categories[]" id="chk_category<?php echo($checkbox['category_id']); ?>" value="<?php echo($checkbox['category_id']); ?>">
<?php echo($checkbox['category_name']); ?></label>
</div>
<?php } ?>
</fieldset>

I am not sure that I completely understand the question, but have you tried
foreach ($data['category_id'] as $id => $checkbox)

Related

convert array object from database to string

I'm currently coding a blog to get experience with php.
In the edit.php I want to give the category of a post if a post has one.
This is the edit.php:
<div class="categories-heading">
<?php if (!empty($entry->c_Id)): ?>
<div class="category-heading">
<h3>Category: <?php echo e($categoryFromId); ?></h3>
</div>
<div class="category-heading">
<h3>change category</h3>
</div>
</div>
<form method="post">
<div class="categories-post-edit">
<?php foreach($categories as $cat): ?>
<div class="category-post-edit">
<input type="radio" name="category" value="<?php echo e($cat->id); ?>">
<label> <?php echo e($cat->category); ?></label>
</input>
</div>
<?php endforeach; ?>
</div>
</form>
<?php else: ?>
<div class="category-heading">
<h3>choose category</h3>
</div>
</div>
<form method="post">
<div class="categories-post-edit">
<?php foreach($categories as $cat): ?>
<div class="category-post-edit">
<input type="radio" name="category" value="<?php echo e($cat->id); ?>">
<label> <?php echo e($cat->category); ?></label>
</input>
</div>
<?php endforeach; ?>
This is the part of the function edit() in the PostsAdminController.php which is relevant for this:
if(!empty($_POST['title']) AND !empty($_POST['subheading']) AND
!empty($_POST['content'])
AND !empty($_POST['category'])) {
$entry->title = $_POST['title'];
$entry->subheading = $_POST['subheading'];
$entry->content = $_POST['content'];
$entry->c_Id = $_POST['category'];
$this->postsRepository->update($entry);
$savedSuccess = true;
}
$categoryFId = $this->categoryRepository->oneCategory($entry->c_Id);
$categoryFromId = $categoryFId['category'];
var_dump($categoryFromId);
$this->render("post/admin/edit", [
'entry' => $entry,
'savedSuccess' => $savedSuccess,
'categories' => $categories,
'categoryFromId' => $categoryFromId
]);
}
And this is the function oneCategory in the CategoryRepository.php that interacts with the database.
public function oneCategory($id)
{
$table = $this->getTableName();
$model = $this->getModelName();
$stmt = $this->pdo->prepare("SELECT `category` FROM `$table`
WHERE id = :id");
$stmt->setFetchMode(PDO::FETCH_CLASS, $model);
$oneCategory = $stmt->fetch(PDO::FETCH_CLASS);
return $oneCategory;
}
oneCategory() gives out an array no matter if I put SELECT * or SELECT category in the query, that's why I put $categoryFId['category'] into a variable after.
It works, but I think there must be a more easy or quicker way (like special functions or so), I just didn't manage to find what I search for

How to get parent name from its child?

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

View comma separated values of one column in multiple rows or in multiple values

I am creating a shopping website in which I have saved multiple values in one column in my database.
The column name is "availablesize" and the stored values are "S,M,L,XL".
Now I want to view these three values in separate rows.
Here's my code:
<div> <!-- Added by edit -->
<div> <!-- Added by edit -->
<?php
require_once 'dbconfig.php';
if(isset($_GET['id']) && !empty($_GET['id']))
{
$id = $_GET['id'];
$stmt = $DB_con->prepare('SELECT productid,productname,availablesize FROM products WHERE productid=:uid');
$stmt->execute(array(':uid'=>$id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
}
else
{
header("Location: index.php");
}
?>
<input type="radio" id="red" name="requiresize" value="<?php echo $availablesize;?>" checked>
<label for="red"><span class="sizespan"><?php echo $availablesize;?></span></label>
</div>
<div>
<input type="radio" id="blue" name="requiresize" value="<?php echo $availablesize;?>">
<label for="blue"><span class="sizespan"><?php echo $availablesize;?></span></label>
</div>
<div>
<input type="radio" id="black" name="requiresize" value="<?php echo $availablesize;?>">
<label for="black"><span class="sizespan"><?php echo $availablesize;?></span></label>
</div>
<div>
<input type="radio" id="black" name="requiresize" value="<?php echo $availablesize;?>">
<label for="black"><span class="sizespan"><?php echo $availablesize;?></span></label>
</div>
</div>
Not sure why multiple explodes were listed in previous answer/comment. Seems like unnecessary redundant use of a function call.
<?php
$sizes = explode("," , $availablesize);
$small = $sizes[0];
$med = $sizes[1];
$large= $sizes[2];
$xlarge = $sizes[3];
Now, the variables listed above will contain "S", "M", "L", and "XL".
OR, you could simply display:
<?php echo $sizes[0]; ?>
<?php echo $sizes[1]; ?>
<?php echo $sizes[2]; ?>
<?php echo $sizes[3]; ?>
Just a matter of preference in how you want to display them. By creating specific variables, you'd be able to use them elsewhere if you want.
<?php echo explode(',', $availablesize)[0];?> = S
<?php echo explode(',', $availablesize)[1];?> = M
<?php echo explode(',', $availablesize)[2];?> = S
<?php echo explode(',', $availablesize)[3];?> = XL

IMPLODE data by checkbox

i have this Interface where the user can add a flower
The user need to add a flower to the database and he can make them in different occasions a categories (the check boxes). Now to implode the data i made this code:
$occasions = implode( ';' , $_POST['reg_occassions'] );
$categories= implode( ';' , $_POST['reg_categories'] );
$query =
"INSERT INTO tbl_flower (flower_type, website_description,florist_description,name,image,enabled,florist_choice,categories,occasions)
VALUES ('$flowertype', '$websitedescription', '$floristdescription','$name', '$upfile', '$enabled','$floristchoice', '$categories','$occasions')";
In the database they save as follows :
id flower name occasions categories
1 Rose Birthday;Valentines Bouqet;flower arrangment
Now the user can edit a flower too.This is the interface
PROBLEM!:
i dont have an idea how can i make the checkboxes again and show him which one he checked and he can change the data. I need to know how can i show the checkboxes and which one he checked will be checked too .
Thank and I really appreaciate if someone can help me.
EDIT Answer: This is the interface of the checkboxes.
You can do something like:
$results = "Bouqet,flower arrangment"; //results from your database
$resultsArray = explode(",", $results); //split the comma
$checkValue = array("Bouqet", "flower arrangment", "other"); //your checkbox values
$html = "";
foreach($checkValue as $val)
{
if(in_array($val,$resultsArray ))
$html .= '<input type="checkbox" name="flower" value="'.$val.'" checked>'.$val.'<br>';
else
$html .= '<input type="checkbox" name="flower" value="'.$val.'">'.$val.'<br>';
}
echo $html;
Check Demo Here
EDIT: I am making this edit because of your comments, you need to do something like:(not tested)
<div class="table-responsive col-md-4">
<table>
<thead>Occasions</thead>
/**flower occassions **/
$flowerCategoriesQry "SELECT categories FROM tbl_flower where id = 1"; //you need to change the where clause to variable
$flowerResults = mysqli_query($conn, $flowerCategoriesQry)
$categoriesRow = $flowerResults->fetch_assoc();
$resultsArray = explode(",", $categoriesRow['categories']);
/**All Occassions **/
$query544="SELECT name FROM tbl_occasions";
$results544 = mysqli_query($conn, $query544);
while ($row = $results544->fetch_assoc()) { ?>
<div class="col-md-12">
<label class="checkbox" for="checkboxes">
<?php
if(in_array($row['name'],$resultsArray ))
{
?>
<input type="checkbox" name="flower" value="<?php echo $row['name'];?>" checked> <?php echo $row['name'];?> >
<?php }
else{
?>
<input type="checkbox" name="flower" value="<?php echo $row['name'];?>" ><?php echo $row['name'];?> >
<?php echo} $row['name']?> </label>
</div> <?php }?>
</table>
This is the answer. Thanks to the Flash!
session_start();
$conn = ConnectToSql();
?>
<div class="table-responsive col-md-6" style="border:1px solid blue">
<div class="col-md-6">Categories</div>
<div class="col-md-6">Occasions</div>
<hr>
<div class="col-md-6">
<?php
/**flower occassions **/
$flowerCategoriesQry= "SELECT categories FROM tbl_flower where id ='$_SESSION[flower]'";
$flowerResults = mysqli_query($conn, $flowerCategoriesQry) ;
$categoriesRow = $flowerResults->fetch_assoc();
$resultsArray = explode(";", $categoriesRow['categories']);
/**All Occassions **/
$query544="SELECT name FROM tbl_categories";
$results544 = mysqli_query($conn, $query544);
while ($row = $results544->fetch_assoc()) { ?>
<label class="checkbox" for="checkboxes">
<?php
if(in_array($row['name'],$resultsArray ))
{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" checked> <?php echo $row['name'];?>
<?php
}
else{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" ><?php echo $row['name']; } ?>
</label>
<?php
}
?>
</div>
click here for code

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();
}
}
}

Categories