I have the following function,
<pre>
public function getFree($cp) {
global $glb_con;
try {
$sql = "SELECT p.ID, w.ID, p.fname, p.lname, w.desc FROM weight w
INNER JOIN comp t ON w.wcl = t.cmp AND w.wc = t.cm
INNER JOIN pers b ON w.ID = b.ID_l
INNER JOIN pn p ON b.ID = p.ID
AND t.cp=:cp group";
$query = $glb_connection->prepare($sql);
$query->bindValue(':cp',$cp);
$query->execute();
$wes = $query->fetchAll();
// First array Generated from sql1 query
$newCorr = array();
foreach ($wes as $option) {
$wclID = $option['desc'];
$nameF = $option['fname'];
$nameL = $option['lname'];
$id = $option['ID'];
$newCorr[$wclID][$id] = $nameL." ".$nameF;
}
$sql2 = "SELECT p.ID, e.enre w.ID, p.fname, p.lname, w.desc FROM weight w
INNER JOIN comp t ON w.wcl = t.cmp AND w.wc = t.cm
INNER JOIN pers b ON w.ID = b.ID_l
INNER JOIN pn p ON b.ID = p.ID
INNER JOIN t_ent e ON e.ID = p.ID
AND t.cp=:cp group";
$query = $glb_connection->prepare($sql2);
$query->bindValue(':ID_cmp',$ID_cmp);
$query->execute();
$wes1 = $query->fetchAll();
//the second array from sql2 query
$newCorrAc = array();
foreach ($wes1 as $option) {
$wc = $option['desc'];
$ent = $option['enre'];
$nameF = $option['fname'];
$nameL = $option['lname'];
$id = $option['ID'];
$newCorrAc [$wc] [$id] [$ent]= $nameL." ".$nameF;
}
//the form will generate after here here
$html_form = '';
if(count($newCorr) == 0){
$html_form .= '<p>'.boz(_('Not Found!')).'</p>';
} else {
$html_form .= '<form id="checkEnt" name="checkEnt" method="post" action="r.php?cp=<?=$cp?>">';
$html_form .= '<input type="hidden" value="checkEntrance" name="ent">';
$html_form .= '<table class="table zebra-striped table-bordered table-striped span8">';
$html_form .= '<tbody>';
foreach ($newCorr as $orgKey => $list) {
$html_form .= '<tr><td width="5%">';
$html_form .= '<h5>'.$orgKey.'kg</h5>';
$html_form .= '</td>
<td width="10%">
<label class="control-label" for="inputWei">Boxer</label>';
$html_form .= '<select class="input-xlarge" id="input" name="drop[0][]">';
$html_form .= '<option value="">Select</option>';
foreach ($list as $key => $value) {
$html_form .= '<option value='.$key.'>'.$value.'</option>';
}
$html_form .= '</select>
</td>
<td width="10%">
<label class="control-label" for="inputWei">Res</label>
<select class="input-xlarge" name="drop[1][]">';
$html_form .= '<option value="">Select</option>
<option value="en">Ent</option>
<option value="re">Res</option>
</select>
</td>
</tr>';
}
$html_form .= '<tr><td colspan="3">
<div class="modal-footer">
<button type=reset class="btn btn-danger">Reset</button>
<button class="btn btn-primary" ID="btnSaveBoxer">Save</button>
</div>
</td>
</tr>
</tbody>
</table>
</form>';
}
echo $html_form;
} catch(PDOException $e) {
addError($e->getMessage(), "MySql-Error", "error");
}
}
</pre>
what it does is exactly ...the First SQL returns array in FetchAll method, and after a bit of change the result will be like this...
// First Query Result array check this line
$newCorr[$wclID][$id] = $nameL." ".$nameF;
<pre>
Array
(
[4-6] => Array
(
[87] => haha lala
)
[8] => Array
(
[25] => sasa baba
[24] => mama fafa
[26] => tata lala
)
[5] => Array
(
[29] => papa oaoa
[27] => laha mana
[30] => salam sara
)
[2] => Array
(
[33] => tata kaka
[32] => lala sasa
[31] => Papa lama
[34] => wana michel
)
[6] => Array
(
[35] => zaza yaya
[37] => wana mata
[36] => Kaku luba
)
)
</pre>
from the above code as you can see, I generate dropdown form, which filled from the above array.
What problem I have faced is, after the customer enter the form data, when I need to populate the form as it is except the customer choice must be selected as default...
So, what i did is i use second SQL2, which generates the following array, this array is exactly what the customer enter it.
// Second Query Result Array
$newCorrAc [$wc] [$id] [$ent]= $nameL." ".$nameF;
<pre>
Array
(
[8] => Array
(
[26] => Array
(
[ent] => tata lala
)
)
[2] => Array
(
[31] => Array
(
[res] => papa lama
)
)
)
</pre>
The main question is, how do I be able to fill the forms from his/her previous enter data when the drop down populated again?
In short what i want to do is, the form is submited, and i have link for correction. if clicked it should show the same form except with the entered data previously, that can be get from database.
some Idea please?
So I dug through your code. When changing the form generation loop:
foreach ($list as $key => $value) {
$html_form .= '<option value='.$key.'>'.$value.'</option>';
}
to this:
// This will contain selected="selected" html code for your
// second column dropdown menu. This way seemed to be the clearest for me.
$typeEntSelected = '';
$typeResSelected = '';
foreach ($list as $key => $value) {
$html_form .= '<option value="'.$key.'"';
if (isset($newCorrAc[$orgKey][$key])) {
// Select last user input in Boxer dropdown
$html_form .= ' selected="selected"';
$userEntry = $newCorrAc[$orgKey][$key];
if (array_key_exists('ent', $userEntry)) {
$typeEntSelected = ' selected="selected"';
} elseif (array_key_exists('res', $userEntry)) {
$typeResSelected = ' selected="selected"';
}
}
$html_form .= '>'.$value.'</option>';
}
and a little bit lower, change your second column dropdown generation code from this
$html_form .= '<option value="">Select</option>
<option value="en">Ent</option>
<option value="re">Res</option>
</select>
</td>
</tr>';
to this. The second and third row in the block are changed.
$html_form .= '<option value="">Select</option>
<option value="en"' . $typeEntSelected . '>Ent</option>
<option value="re"' . $typeResSelected . '>Res</option>
</select>
</td>
</tr>';
it selects 'tata lala' in the 8kg Boxer field, and 'Papa lama' in the 2kg Boxer field. Now it also selects "ent" and res in the second field. The rest is unselected. Is is this behaviour that you're after?
Related
I have a multiple checkbox query, as below
<span style="width:276px;display:inline-block;"> <input type="checkbox" name="check_list[]" value="sub_category_id = '1''">Name1</span>
<span style="width:276px;display:inline-block;"> <input type="checkbox" name="check_list[]" value="sub_category_id = '2'">Name2</span>
<span style="width:276px;display:inline-block;"> <input type="checkbox" name="check_list[]" value="sub_category_id = '3'">Name3</span>
<span style="width:276px;display:inline-block;"> <input type="checkbox" name="check_list[]" value="sub_category_id = '4'">Name4</span>
then
<?php
$names = implode(' or ', (array)$_POST['check_list'.$i]);
echo $names;
?>
I got part of my query.
sub_category_id = '1'' or sub_category_id = '2' or sub_category_id = '3' or sub_category_id = '4'
Using chosen Jquery, I´m able to retrieve the MySQL data (Name1, Name2, Name3, Name4), as below:
<select name="sub_category_id[]" id="sub_category_id[]" multiple class="chosen-select" tabindex="8">
<option value="">Selecione...</option>
<?php foreach ($arrEstados as $value => $name) {
echo "<option value='{$value}'>{$name}</option>";
}?>
</select>
$names = $_POST['sub_category_id'];
$query=implode('sub_category_id=', $names);
echo $query;
But the 'or' is missing. I don´t have great skills in PHP, so I ask: Is this the right way to get my query? I´m not using PDO or mysqli.
change your html select element to follow this:
<select name="check_list[sub_category_id][]" id="sub_category_id[]" multiple class="chosen-select" tabindex="8">
<option value="">Selecione...</option>
<?php
foreach ($arrEstados as $value => $name) {
echo "<option value='{$value}'>{$name}</option>";
}
?>
</select>
doing this will create an array like this in your $_POST:
$_POST => Array
(
[check_list] => Array
(
[sub_category_id] => Array
(
[0] => 2
[1] => 3
)
)
)
then change your PHP like so:
<?php
$names = isset($_POST['check_list']) && isset($_POST['check_list']['sub_category_id']) ? $_POST['check_list']['sub_category_id'] : NULL;
$query = "SELECT * FROM tablename WHERE ";
if(!is_null($names)){
foreach($names as $i => $name){
if($i !== 0){
$query .= " OR ";
}
$query .= "sub_category_id = '$name'";
}
}
echo($query);
//Output: SELECT * FROM tablename WHERE sub_category_id = '2' OR sub_category_id = '3'
}
?>
you might need to change the query to fit your application, but this will help get in you in the right direction.
As Martin suggest de http://www.tutorialspoint.com/mysql/mysql-in-clause.htm turns the simple way to solve it. That´s what i did:
<select chosen still the same...>
$sub_category_1 = $_POST['sub_category_id'];
$query=implode(',', $sub_category_1);
$sql = "Select * from table WHERE sub_category_id IN($query)";
I have a mysql table with two columns; id and type. I'm trying to retrieve those values to use in a select list of values (aka drop down list). Outside of my html, this php works perfectly:
$sql = "SELECT * FROM `usertype`";
$query = mysqli_query($con, $sql);
while ($type_lov = mysqli_fetch_assoc($query)) {
echo '<pre>', print_r($type_lov,true), '</pre>';
};
Output from php above:
Array ( [id] => 1 [type] => System Admin )
Array ( [id] => 2 [type] => System Admin2 )
Array ( [id] => 3 [type] => System Admin3 )
Array ( [id] => 4 [type] => Account Admin )
Array ( [id] => 5 [type] => Account User )
To get it into the SELECT / OPTIONS tags, I attempted several things unsuccessfully. The two attempts that made the most sense to me (but that did not work) were:
<!--ATTEMPT 1-->
<select>
<?php while ($type_lov = mysqli_fetch_assoc($query)) {
foreach ($type_lov as $id=>$type) { ?>
<option value="<?php echo $id; ?>"><?php echo $type; ?></option>
<?php };
}; ?>
</select>
<!--ATTEMPT 2-->
<select>
<?php foreach ($type_lov = mysqli_fetch_assoc($query) as $id=>$type) { ?>
<option value="<?php echo $id; ?>"><?php echo $type; ?></option>
<?php }; ?>
</select>
Neither worked. What is the proper way to go about this?
You should read up on mysqli_fetch_assoc. It returns it's data as an associative array, meaning your table columns are used as indices for the array.
Furthermore, why are you coupling while with foreach in your first example? What's the thought process that you went on?
Anyway, this should help you on your journey:
<select>
<?php
while (($data = mysqli_fetch_assoc($query)))
{
echo '<option value="' . $data['id'] . '">' . $data['type'] . '</option>';
}
?>
</select>
Can anyone tell me how to get the 'value' of selected items from a multivalued select?
I have the following:
<select id="category" name="category[]" multiple="multiple">';
/* Select categories */
[...]
<option value"' . $row->id . '">' . $row->name . '</option>';
which would return:
<option value"1">itemName1</option>
Why is the code below giving me the selected text and not their values? What's wrong here?
$category = $_POST['category'];
if (count($category) > 0){
foreach ($category as $key => $value) {
echo $value . "<br>\n";
}
}
This is returning itemName1 and I need the actual value (1)
Thank you
Your HTML is invalid. You have:
<option value"1">itemName1</option>
while it should be:
<option value="1">itemName1</option>
mulple select will return a query like this (name = name and values = int)
name=1&name=4&name=99
so it return the selected values
the array would look like:
array(
name => array(
[0] => 1
[1] => 4
[2] => 99
)
)
ps if you fix the HTML error, you will most likely get a valid result
Here's my database:
id name parent_id
1 Computers NULL
2 Apple 1
3 Books 1
4 Music NULL
5 CDs 4
6 Records 4
My categories function:
public function showCategories($parent_id = 0){
if($parent_id == 0){
$sql = "SELECT * FROM categories WHERE parent_id IS NULL";
} else {
$sql = "SELECT * FROM categories WHERE parent_id =:parentid";
}
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':parentid', $parent_id);
$stmt->execute();
$categories = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
array_push($categories, array($row['id'] => $row['name']));
}
return $categories;
}
Here's my category page:
<?php
//Instantiate categories class
$categories = new categories($db);
$categoriesMain = $categories->showCategories(0);
?>
<html>
<head></head>
<body>
<form action="" method="post">
<?php //Get parent categories and put them into a select box ?>
<select name="categoriesMain">
<?php for($i=0;$i<count($categoriesMain);$i++){ ?>
<option value="<?php echo $i; ?>">
<?php echo $categoriesMain[$i]; ?>
</option>
<?php } ?>
</select>
<input type="submit" name="submit" value="submit"/>
</form>
<?php //if form submits then show sub categories ?>
<?php if(isset($_POST['submit'])){
$categoriesSub = $categories->showCategories($_POST['categoriesMain']);
for($i=0;$i<count($categoriesSub);$i++){
echo $categoriesSub[$i];
}
} ?>
</body>
</html>
Let me try to explain what im having trouble with. I think my whole design is out of place because it feels like that but im having a brain block at the moment.
In the function I'm returning an array like Array ( [0] => Array ( [1] => Computers ) [1] => Array ( [4] => Music ) ). If you think this is the wrong way to return it let me know. Ok then do you see CategoriesMain? I'm using a for loop to output this array and for option=value im echoing $i however this $i goes like 1, 2, 3, 4 however I want the value to be the value of the parent category e.g. 1, 4 so that I can collect the value using $_POST['cateogoriesMain'] in the next for loop where I'm displaying cateogriesSub where it will get the database rows for those with the parent_id = to whatever was selected in the selectbox previously. I hope it makes sense.
You should use the key of the array for the option value, like this:
<select name="categoriesMain">
<?php foreach ($categoriesMain as $k => $v) { ?>
<option value="<?php echo $k; ?>">
<?php echo $v; ?>
</option>
<?php } ?>
</select>
edit also change the following line inside the php function, instead of:
array_push($categories, array($row['id'] => $row['name']));
do
$categories[$row['id']] = $row['name'];
I have a multi-select box named Manufacturer Name that is displaying correctly, even clickable for multi search. When I run the search I am not getting any results from my database using the complex find conditions I set. I've tried to use Complex-Find-Conditions from the bakery, and other various websites but no avail.
//This my index.ctp
<?=$form->create('ManufacturersProductsLine', array('action'=>'index'));?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<?=$f->label('ManufacturersProductsLine.manufacturer_id', 'Manufacturer Name:'); ?>
<?=$f->select('ManufacturersProductsLine.manufacturer_id', $manufacturers, null, array('multiple'=>'multiple', 'style'=>'height:100px;'))?>
</td>
<td>
<?=$f->label('ManufacturersProductsLine.name', 'Product Name:'); ?>
<?=$f->text('ManufacturersProductsLine.name', array('style'=>'width:140px;'));?>
</td>
<td>
<label></label>
<input name="Search" class="blue" type="submit" value="Search Product Lines" />
</td>
</tr>
</table>
Commented out is things I have tried in my controller for setting the multiple find conditions.
//This is my manufacturers_products_lines_controller.php
function index() {
$conditions = array();
if (!empty($this->data)) {
$data = $this->data;
$conditions[] = "(LOWER(ManufacturersProductsLine.manufacturer_id) LIKE '%{$data['ManufacturersProductsLine']['manufacturer_id']}%')";
//$conditions = array("ManufacturersProductsLine.name" => array("Akuret", "Bridgestone", "American Radials") );
debug($this->data);
}
$this->Manufacturer->recursive = 1;
$this->set('manufacturersproductslines', $manufacturersproductslines = $this->paginate('ManufacturersProductsLine', $conditions));
$Manufacturer = getModel('Manufacturer');
$this->set('manufacturers', $Manufacturer->find('list'));
//$this->ManufacturersProductsLine->recursive = 0;
//$this->Post->find('all', array('conditions' => array('ManufacturersProductsLine.manufacturer_id' =>array(0,1,2,3,4,5,6,7,8,9,10,11,12,13))));
}
When I debug and search I am getting the following.
Array
(
[ManufacturersProductsLine] => Array
(
[manufacturer_id] => Array
(
[0] => 1
[1] => 2
)
[name] =>
)
)
try changing the query condition to this
$conditions[] = "ManufacturersProductsLine.manufacturer_id IN (" . implode( ',', $data['ManufacturersProductsLine']['manufacturer_id'] . ")";