I have this PHP page where the user can select and un-select items. The interface looks like this:
Now I'm using these code, when the user hit the save changes button:
foreach( $value as $al_id ){ //al_id is actually location id
//check if a record exists
//if location were assigned and leave it as is
$assigned_count = $this->AssignedLoc->checkIfAssigned( $tab_user_id, $al_id );
if( $assigned_count == 0 ){
//else if not, insert this new record
$this->insertAssigned( $tab_user_id, $company_id, $al_id );
}
}
Now my question is, how do I delete the un assigned locations? For example in the screenshot above, there are 4 assigned locations, if I'm gonna remove (or unassign) "Mercury Morong" and "GP Hagonoy" from the assigned locations, only two must remain. What are the possible solutions using PHP?
Thanks for any help!
Loop through the submitted values in the unavailable selection. If a row with that id/name/whatever identifier you use exists in the "assigned" section, then delete that row from the assigned section. Use you pre-existing checkIfAssigned function.
Related
I am building my wedding website and want to integrate an RSVP form using Gravity Forms. The issue I am running into is how to set certain guest that have +1's. I would like to show an additional guest entry (First Name, Last Name, Meal Option) when the initial First Name and Last Name has been populated. How would I go about doing this? Any help would be great! Thanks in advance!
Here is how I'd solve this problem:
First, you need to put everything in the DB, the easiest way would be to either do it manually or somehow loop through an array/CSV calling add_option($key, $value) Again, I would recommend a mobile/phone number as they'll be unique so you don't pull the wrong "John Smith". I'll assume you'll keep it basic with $key as the unique identifier and $value as boolean as to whether to show additional info. Interestingly, by default, if not found get_option($key) will return false and therefore not show your additional data, which I would assume you'd want anyway. If you'd rather it return true just pass true as the second argument.
Now for your answer:
Your URL is something like https://somesite.com/rsvp?id=1234.
function allowed_plus_one() {
$id = $_GET["id"];
$allowed = get_option($id);
return $allowed;
}
Then assumedly it'll be something like
if (allowed_plus_one()) {
// show form with plus one
} else {
// show form without
}
EDIT:
Keeping separate incase this has already been viewed.
You should also be checking for the existence of $_GET["id"] and behaving accordingly. eg:
if (isset($_GET["id"] && !empty($_GET["id"]) {
//do logic above
} else {
//here by mistake so don't show any form?
}
i am having a issue with codeigniter form validation.
my table is as
Sr#, name , dob,pic
$this->form_validation->set_rules('name','Duplicate Name','trim|required|is_unique[mcb.name]');
now when i am trying to edit any record with sr# and check the form validation for name (as i dont want duplicate name too) it gives error.
what i am trying to do is update the record but i don't want duplicate name too.
now for example if i edit the record and don't change name but change
DOB
but it shows the duplicate name.
i want to check duplicate name but not in row i am going to update.
Thanks
Solution is kind of easy I think (if I got your question right)
make two sets of rules
$this->form_validation->set_rules('name','Duplicate Name','trim|required|is_unique[mcb.name]');
$this->form_validation->set_rules('name','Duplicate Name','trim|required');
make a if() before using validation->run()
like this
if ( strtoupper($this->input->post('name')) == strtoupper($old_name) ) { //pass old name in hidden field or load it before this condition via model
$this->form_validation->set_rules('name','Duplicate Name','trim|strtoupper|required');
} else {
$this->form_validation->set_rules('name','Duplicate','trim|required|strtoupper|is_unique[mcb.name]');
}
//all other form_validation checks here
if ($this->form_validation->run()) {}
//edit
added strtoupper() so your unique values are really unique
I've currently Implemented a click counter if the value is set to null and you click on the link it will update to one. But when it's clicked for the second time it does not update it's stays as one.
public function getUpdateClick($Id) {
$Id=DB::escape_string($Id);
$i=1;
$click=0;
$click=$click+$i;
$updatesArray=array('clicks'=>$click);// the column which is going to be updated
$this->setTable('pages'); //setting the table name
$where="id=$Id";
$result=$this->update($updatesArray,$where); // query executed
}
$click=0;
$click=$click+$i;
Remove first line and store the value of $click as session variable or store it in database.
Every time the function is invoked the value of $click is set to 0 and then 1 in the next statement. If you want the value to persist, you should use an attribute in a table and increment it every time you click.
In your function, you should first check if there is a value for clicks on the pages tables for the specific page (where id=$id). If there isn't, then you can give clicks=1, otherwise get that value and add 1.
But here's how I'd do it: I'd edit the table by disabling ALLOW NULL in the clicks column, so that by default, when a new page is created, it's default value is 0. Then I'd use the code below:
public function getUpdateClick($Id){
$Id=DB::escape_string($Id);
//You can edit the 3 lines below to check the database
// in pages table in clicks column where id=$Id in your custom way
$fetching sql_result = mysql_query('SELECT * FROM pages where id=$Id')
$row = mysql_fetch_assoc($fetching sql_result);
$current_number_of_clicks = $row['clicks'];
$click= $current_number_of_clicks;
$click=$click+1;
$updatesArray=array('clicks'=>$click);// the column which is going to be updated
$this->setTable('pages'); //setting the table name
$set="click=click+1"; // column and value
$where="id=$Id"; //
$result=$this->update($updatesArray,$where); // query executed
}
} // I assume this closes your class
I am having a problem with a script that controls an order system that i am creating.
The system contains a search feature that queries a user supplied string against products held in a mysql table, if the string matches part of an items description, then that item is returned as a result.
Now, The problem is this.
If a user searches a string that returns say 5 results.
Then the user decides they would like to add 3 of one result and 2 of another result.
They then click on order, which submits the form.
Here is where the problem appears, as only the first result that the user has entered a quantity for is added to the order.
The order should contain two items, one with a quantity of 3 and another with a quantity of 2. It is not doing that and i have no idea what is going wrong here.
Here is the code that controls the instance of the user clicking on the order button:
if (!isset($_SESSION['order']))
{
$_SESSION['order'] = array();
}
if (!isset($_SESSION['quantity']))
{
$_SESSION['quantity'] = array();
}
$productQuantities=$_POST['quantity'];
if (isset($_POST['action']) and $_POST['action'] == 'Order' and $productQuantities > 0)
{
foreach($productQuantities as $productId=>$quantityS)
{
if ($quantityS > 0)
{
$_SESSION['order']["$productId"] = $productId;
$_SESSION['quantity']["$productId"] = $quantityS;
header('Location: .');
exit();
}
}
}
Can anyone see where I am making a mistake in this?
Any input would be greatly appreciated, and I will supply any additional code on request.
Thanks!
You create a loop, but in the first iteration of that loop with a quantity, you send a Location header and exit the script execution. Clearly no more products can be added to $_SESSION once you terminate the script and send the visitor to another page.
Don't prematurely end the loop, and only send a (proper!) location header after you've finished.
I am writing a PHP/MySQL application that maintains a masterlist of user preferences and I've gotten myself stuck trying to remove items from that list. Currently the application generates a list of items and marks a checkbox next to the ones a user has previously selected, the user can then change their selections (either adding or removing checkmarks) and resubmit. The form only submits supplyid's for items the user has checked.
I have the list sorted so that unmarked selections are shown first and I've got the code to insert/update items in the database working, but I'm having problems figuring out how to delete the items the user has unchecked (and which now do not return supplyid's).
At this point, I've written a MySQL query to return only results that were previously included on the list (as those are the only ones which could need to be removed.) What I need are the items in the array returned by the query that do not match any $_POST results. I've been successfully comparing the array to the $_POST results of items previously included, but I can see my logic is wrong in the part where I'm trying to get back the results which don't match. While I'm able to view which items match, I'm not sure how to eliminate them as possibilities. Am I going about this in the wrong way entirely?
$iduser = $_SESSION["iduser"];
$possibleresults = $_POST["possibleresults"];
$sql_onlist = "select supply.idsupply from supply, kit
where supply.class = 'basic'
and kit.iduser = '".$iduser."'
and supply.idsupply = kit.idsupply";
$possible_delete = $connection->query($sql_onlist);
//for each record we know is already in the database, check to make sure it has been checked, otherwise delete
for ($i=0; $i<$possibleresults; $i++) {
$count = 0;
$item_delete = $possible_delete->fetch_assoc();
if ($_POST['item_'.$i.'']) {
$idsupply = $connection->real_escape_string($_POST['item_'.$i.'']);
//if there is a match, increase the counter
if ($idsupply == $item_delete["idsupply"]) {
$count++;
//this does successfully return a count = 1 - idsupply = number for all rows which should have matches
echo "count = ". $count . " - idsupply = " . $idsupply;
}
//this statement doesn't work because it doesn't know which idsupply
if ($count < 1) {
$idsupply = $item_delete["idsupply"];
$sql_delete = "delete from kit
where idsupply = '".$idsupply."'
and iduser = '".$iduser."'";
$result_delete = $connection->query($sql_delete);
}
}
There are a couple different solutions to this problem; here's a few strategies I've used before.
-- Delete all the entries every time you update a user's preferences
Not terribly efficient, but it's easy to implement. Every time they save their preferences, first set all the values in the database to whatever the 'unchecked' value is. Then, save their preferences as normal.
-- Give unchecked boxes a value
If you put a hidden input element right before a checkbox and give it the same name as the checkbox, the hidden element will submit its value whenever the checkbox is not checked. E.g.,
<input type='hidden' name='box1' value='off' />
<input type='checkbox' name='box1' value='on' />
This will let you know which IDs to unset in the database.
There may be a more database-oriented solution as well, but I'd have to know more about your structure to suggest anything.
Holy moly, what a tangled mess... kinda painted yourself into a corner eh? No worries it happens to all of us. :)
So I think that once you have a truly working algorithm the code just kinda comes together around it. So lets analyze your problem:
Your main objective is to store a users settings.
You are using a form and checkboxes to both display the current settings and to allow the user to change their current settings. This is graceful enough.
Generate a list of the users POSTed settings (aka get the new settings from the POST array) and store those results in a dedicated data container like an array or a linked list.
Once you have a list of new settings, you need to use that list as a map to set/unset various fields within a database table.
Get a list of ALL of the users saved settings from the database storing that in a different data container
Do a case by case comparison, seeing if the variables match, record the results in yet another data container, or do an immediate write to the database.
Present the user with a human readable result of their operation.
NOTE: Incidentally, you probably already know this, but if you use isset($_POST['mychkbox1']) and it returns a positive value, then that checkbox was checked. If isset() returns false, the checkbox was not set, or does not exist. Like I said you probably already knew that, but I figured I toss it in there.
Good luck
h
I didn't quite understand your code, but I think you need something like this
$to_keep = array();
for ( $i=1 ; $i < 10 ; $i++ ) {
// Add ids of elements we want to save
$to_keep[] = $i;
}
if ($to_keep) {
mysql_query("DELETE FROM table WHERE id NOT IN (". implode(',', $to_delete) . ")");
}