Transferring data from int to string - php

A user puts in their information via a scroll down menu and selects an option a couple of times. When they do this, it is saved as a INT rather then a STRING. This is because some calculations need to be run on the informaton. So essentially it looks like this:
<select name="option1">
<option value="0">Chair</option>
<option value="10">Table</option>
<option value="20">Counter</option>
<option value="30">Toaster</option>
<option value="40">Oven</option>
<option value="50">Microwave</option>
</select>
<select name="option2">
<option value="0">Red</option>
<option value="2">Blue</option>
<option value="4">Green</option>
<option value="6">Yellow</option>
<option value="8">Orange</option>
These values are added together to make a sum. So a blue(2) table(10) would have a value of 12.
My problem arises when I want users to be able to edit this data. I want to display the data they currently have, however it is now saved as a INT. How would I make it so that the information is displayed back in text format based on a the value in the database. (ie. a value of 38 would output "Orange Toaster)?
*I am doing this in PHP but I am not necassarily looking for code, just an idea on how to do this.

You can save the options in database with the following table structure.
Table Name: selection_options
id(pk) option_name option_value category
1 chair 0 option1
2 Table 10 option1
3 Toaster 30 option1
.
.
6 red 0 option2
7 Orange 8 option2
.
.
so on
For creating the selection box option1 execute the query as below:
$option1Array = "SELECT * FROM selection_options WHERE category = 'option1'" ;
This will give you all the options available in the category option1
Do same for the option2 now your selection boxes will be changed as below:
<select name="option1">
<?php foreach($option1Array as $option1) {?>
<option value="<?php echo $option1['id']; ?>"><?php echo $option1['option_name']; ?></option>
<?php }?>
</select>
<select name="option2">
<?php foreach($option2Array as $option2) { ?>
<option value="<?php echo $option2['id']; ?>"><?php echo $option2['option_name']; ?></option>
<?php } ?>
</select>
Now you have to save this data in the database as below:
Table Name: users_selected_options
id user_id option1 option2
1 1 3 7
.
.
id: Primary key of the table
user_id: Foreign key of the user table
option1: Foreign key of the selection_options
option2: Foreign key of the selection_options
No you want to show what options user has selected fire a execute join query you will get the result.
SELECT uso . * , so.option_name, so1.option_name, (so.option_value + so1.option_value) AS total
FROM users_selected_options AS uso
JOIN selection_options AS so ON uso.option1 = so.id
JOIN selection_options AS so1 ON uso.option2 = so1.id
WHERE user_id =1
LIMIT 0 , 30
The above query will give you the records as below:
id user_id option1 option2 option_name option_name total
1 1 3 4 Toaster Orange 38
So now you can easily iterate over the result and show the appropriate output to the user.
This solution is might be lengthy for you but it will helps in future when you want to make the
application dynamic as.
Now you can easily provide the interface to add / edit the values
and options names.
2.If the option names and values are being changed
you don't have to worry about as we are getting the SUM directly by
the query.
3.Now you can able to save the multiple data against the
same user.
4.In future if you want to provide option3 for the
selection then you can do this change easily and you just have to
update the query not in the entire code.

To start you will need to find the remainder when divided by 10.
if($numberX%10 == 4) { echo "Green"; }

You need a table to save all of the options eg:
id info
-- ----
0 Red Chair
2 Blue Chair
...
But I might be better saved in 2 tables, why do you sum them up as one int?
OK since you said no need to add them up, try two tables:
id furniture
-- -------
10 ...
id color
-- -----
2 Blue
For non-table solution, you save above values into an array eg
$furniture = array(
10 => '...',
...
);
$colors = array(
2 => 'Blue',
...
);
It is not a good idea to make some ID value as 0

You can use math functions.
Use division that you can find remainder which would be your option2 and use subtraction (integer - remainder) = your option1

Related

PHP MySQL Options using JOIN

I have a table...
Table: forums
for_id for_name for_des for_mem
1 Forum 1 Description 1 mem1#email.com, mem2#email.com
2 Forum 2 Description 2 null
3 Forum 3 Description 3 mem1#email.com
Then i have another table: members
mem_id mem_name mem_email ...
1 Jane mem1#email.com
2 Jack mem2#email.com
3 Smith mem3#email.com
I am trying to create an HTML Options list. A multi select list that would display all columns in members and then select those in a forums when editing the information.
Example: Editing for_id = 1
It should display all users and select only users who are in for_id=1's for_mem
Currently i am using:
<? $data = explode(',',$row[for_mem]); ?>
<div class="col-md-9">
<select name="for_mem" id="" multiple class="form-control">
<?php foreach($data as $key => $value){?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
</div>
But it only displays the users in the for_mem for that Forum, not all users for them to select new ones to add. Also, just the email. Is there a way to display the name?
Please help. Thanks
This query can solve partly & then join both by union. first part is members that are exist in forums and second part members that are not exist for the forums.
I have added a field "checked" which is 1 for first part and 0 for second part used for identified which users to checked when display.
select for_id, for_name, for_mem, mem_name, mem_email, 1 checked from forums, members where locate(mem_email, for_mem) > 0
union
select for_id, for_name, for_mem, mem_name, mem_email, 0 checked from forums, members where locate(mem_email, for_mem) = 0;
may be there any syntactically mistake i haven't tested because of data unavailable. You can use group by or group concat for more. I advice to run it on mysql prompt first and see the result and modify as your need.

track selecting and unselecting an option When updating a table

I have three tables (I simplified it in this question) :
Table 1
id | Name
-----------
1 | John
2 | Smith
Table 2
id | Title
-----------
1 | Developer
2 | Web Developer
3 | A New Title
Table 3 (links between 1 and 2)
idName | idTitle
-----------
1 | 1
1 | 2
2 | 1
My problem is with the update page. My HTML is <select multiple> ... options ... </select> and I am using chosen to select and deselect options. I am trying to do this with PHP only.
Lets say that we have the following scenario:
The administrator wanted to remove the 'Developer' Title for 'John' and add 'New Title' for 'John'.
He/She will deselect Title id 1 and select id 3. When He/She submits the form I will get two select values: id 1 (the one that he selected) and id 2 (the one that was already there). But I will not get id 3 because he deselected it.
What I am struggling with is : when the user posted the new selected values the ones that were deselected were not submitted with the form. There is no way for me to track the ones that were deselected so I can delete them from my table. How do you update your table with the new changes then? Do you delete what is already existed in that table and add the ids again? Is there a better option?
UPDATE :
It seems #wander answer is less destructive than the other ones. However, #wonder did not explain how to compute step 4 in his answer. to distinguesh between new selected options, already existing selected options, and deselected options.
There's no need to submit the deselected one. e.g.
John has two titles(Developer and Web Developer) saved in database.
The administrator opens the page, selects 'New Title', deselects 'Developer' and clicks submit.
Now on server side, we can get
the title list of John stored in database: Developer and Web Developer
title list submitted from the users: Web Developer and New Title
We compare the two title lists and figure out: we shall delete Developer title and add New Title on John.
You could add hidden fields before the select menu which will cause 0/falsey values to also be sent to PHP.
<input type="hidden" name="stuff[]" value="0" />
<input type="hidden" name="stuff[]" value="0" />
<input type="hidden" name="stuff[]" value="0" />
<select multiple name="stuff[]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
This is similar to how you would send unchecked check boxes to the server:
https://stackoverflow.com/a/1992745/268074 (note the answer without JS)
You can dump your old values in a hidden select, so you'll get them when handling your form.
<!-- Invisible select field -->
<select multiple name="oldData[]" style="display:none;" aria-hidden="true">
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="3" selected>3</option>
</select>
<!-- Real select field -->
<select multiple name="data[]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Note: I was inspired by #Petah's answer, don't forget to upvote him :-).
Let's say we have $old_titles list with titles from db, and $new_titles titles from submit.
$add = array_diff($new_titles, $old_titles);
$delete = array_diff($old_titles, $new_titles);
$add - list of titles to add, $delete - list of titles to delete.
Add action:
$dbh->prepare('INSERT INTO table3(idName, idTitle) VALUES (:idName, :idTitle)');
foreach($add as $titleId) {
$dbh->execute(['idName' => $userId, 'idTitle' => $titleId]);
}
Delete action:
$dbh->prepare('DELETE FROM table3 WHERE idName = :idName AND idTitle = :idTitle');
foreach($delete as $titleId) {
$dbh->execute(['idName' => $userId, 'idTitle' => $titleId]);
}
Not sure why everybody keeps using hidden input fields to track the existing data. When you POST your form, you can just retrieve the original values from the database. That's a lot safer then depending on user input, because even hidden fields can be altered. I know it won't matter too much in this particular situation, but I think it's best to always use the best practise.
Now in PHP you have two options. The first would be to delete all relations and then add the new relations. The second would be to delete all that wasn't posted and add/update the relations that where.
Although the second options might seem to be the "least destructive" as you call it, it is the easiest and least error prone way. The only reason I would abandon this tactic is when I would store extra data in the relations table. EG: date added (when did somebody get into a function) or added by user.
How would you go about it (method 2)?
Keep in mind that I use a method that tries to continue whenever possible. Another approach would be to halt when false data is posted.
<?php
if (array_key_exists("relations",$_POST) && is_array($_POST["relations"])) {
//no duplicates, you dont want to store the same function twice
//only integers
$list = array_unique(array_filter($_POST["relations"], "is_int"));
$csv = implode(',', $list);
//get the userid hoewever you normally would
$userId = 1
//Delete existing
$query = "DELETE FROM Table3 WHERE idname=".$userId
//insert all posted functions
//I select the id's from the actual table so it will never inserted a none existing ID.
//this approach never has duplicates, so the duplicate check in the beginning is redundant now, but I leave it incase I change this.
$query = "INSERT INTO Table3 (idName, idTitle) SELECT ".$userId.", Id FROM Table2 WHERE id IN (".$csv.")".
//execute queries
} else {
//nothing posted, delete all relations
}
?>
Now if you only want to insert new data and remove none-existing
<?php
if (array_key_exists("relations",$_POST) && is_array($_POST["relations"])) {
//no duplicates, you dont want to store the same function twice
//only integers
$list = array_unique(array_filter($_POST["relations"], "is_int"));
$csv = implode(',', $list);
//get the userid hoewever you normally would
$userId = 1
//Delete existing
$query = "DELETE FROM Table3 WHERE idname=".$userId." AND idTitle NOT IN (".$csv.")"
//only insert new functions
//I select the id's from the actual table so it will never inserted a none existing ID.
//this approach never has duplicates, so the duplicate check in the beginning is redundant now, but I leave it incase I change this.
$query = "INSERT INTO Table3 (idName, idTitle) SELECT ".$userId.", Id FROM Table2 WHERE id IN (".$csv.") AND id NOT IN (SELECT idTitle FROM table3 WHERE idName=".$userId.")".
//execute queries
} else {
//nothing posted, delete all relations
}
?>
The only better way is the simplest one, remember KIS
on update you just need to do following
DELETE FROM table3 WHERE idName=??
and then insert all selected idTitles again in table3
This can be done in two SQL statement without knowing which value has been de-selected. Following these 2 steps
1) In your PHP, create a comma separated list of all selected option. For example: 2, 3.
2) Then execute the following statements:
DELETE FROM tblLink WHERE idName = $id AND idTitle NOT IN ($list);
INSERT IGNORE tblLink (idName, idTitle)
SELECT $id, id FROM tblTitle WHERE id IN ($list);
Note: for shake of simplification, I use $id and $list in my example. When you intent to use it, you should properly prepare and bind parameter properly. Make sure that idName and idTitle are primary key to make it work.
To my opinion you have to check what actions should be performed first. This is a small example that you can run here http://sandbox.onlinephpfunctions.com/code/0fa11b3c9f846e344c912f5a3e994eea5e9cac34.
First run a select on Table 3 for idName=1 the output would be an array of idTitle's (1,2). In the following example the $array1 is the output of such a select statement and $array2 is the array of new idTitle's for the same idName that will have to be updated or deleted or inserted. If for example the new idTitles for a user are more or less than the previous ones then there you should have some sort of control over it in order to decide what queries you will run to the database.
$array1 = array(1,7,3,5);// initial idTitles try it on the above link (php sandbox) with more or less values
$array2 = array(1,4,3,6);//new idTitles try it on the above link (php sandbox) with more or less values
$count1=count($array1);
$count2=count($array2);
$diff=$count1-$count2;
if ($diff==0) {
$result1=array_values(array_diff_assoc($array2,$array1));
$result2=array_values(array_diff_assoc($array1,$array2));
$countr=count($result1);// or $result2 it is the same since they have the same length
$cr=0;
while($cr < $countr) {
print_r("update tblname set val=$result1[$cr] where id=theid and val=$result2[$cr]\n");
$cr++;
}
}
if ($diff!=0) {
$result1=array_diff($array2,$array1);
$result2=array_diff($array1,$array2);
if(count($result2)>0) {
foreach($result2 as $r2) {
print_r("delete from tblname where id=theid and val=$r2\n");
}
}
foreach($result1 as $r1) {
print_r("insert into tblname where id=theid and val=$r1\n");
}
}
How I would do it would extend the 1st table
id | Name | Title IDs
1 | John | 1,2
2 | Smith | 1,3
Each time the row is updated I would simply just replace the title_ids fields and be done with it.

combine 2 mysql queries into options of a select field

totally confused by this! I have need to dynamically generate a dropdown box from holidays in a db. Basically, because there are more than one visit to each location, I need a user visible UI - which I want to set as the holiday start date.
So a typical dropdown option would look like: Benidorm 01/01/15
So, I can get the holiday name and id from Table X in the db. However, the start date is in a separate db table (table Y) and is Identifiable from the product_id and field_id.
I'm really confused by how I can a) extract the 2 pieces of info, and then combine into an option while I run through a fOREACH to create the field options.
OH, and to confuse matters, I need to pre-select an option IF a Get variable is set (Which will be the holiday name and start date.
Hope this makes sense? I've spent about 3 hours on this so far. Can anyone help?
Thanks.
TABLE HOLIDAY
holiday_id
holiday
TABLE DATE
date_id
holiday_id
date
$query = "SELECT holiday h
FROM holiday
LEFT JOIN date d
ON h.holiday_id = d.holiday_id";
//Not sure what your connection method is but fetch your rows here for example
//We put the rows in $rows
$get = $_GET['yourgetname'];
<select name="holiday">
<?php foreach($rows as $row) { ?>
<option value="<?php echo $row['holiday'].$row['date']; if($get == $row['holiday'].$row['date']) { ?> selected <?php } ?>"><?= $row['holiday'].$row['date']; ?></option>
<?php } ?>
</select>

PHP Page populated by MySQL dropdowns

I am trying to write a script to display records from an SQL database, but based on three variables which are set by three dropdown boxes on the page which are auto-populated from the database.
This is for a Learning Management System I am working on to provide effective feedback from online learning tests.
The code I am currently working on is below, along with the pseudo code which I hope will explain my requirements. The problem I am having is I cannot get the dropdown boxes to populate based on the criteria I have set in my pseudo code.
Any help is very much appreciated,
Thank you
John
// I used this article for the structure of the following script:
http://forums.devarticles.com/mysql-development-50/drop-down-menu-populated-from-a-mysql-database-1811.html
// Dropdown Box 1 - Choose the course - Show entries from the column "Name" from table "mdl_scorm". Once an option has been selected set variable $coursechoice to the value in the "id" column of the "mdl_scorm" table
// Dropdown Box 2 - Choose the user - Show entries from the columns "firstname" + "lastname" from table "mdl_user" IF the number shown in the "id" column of table "mdl_user" is present in the "userid" column of table "mdl_scorm_scoes_track" AND IF $coursechoice is present in the "scormid" column of table "mdl_scorm_scoes_track". Once an option has been selected set variable $userchoice to the value in the "id" column of table "mdl_user"
// Dropdown Box 3 - Choose the attempt - Show entries from the column "attempt" from table "mdl_scorm_scoes_track" IF $coursechoice is present in the "scormid" column of the table "mdl_scorm_scoes_track" AND IF $userchoice is present in the "userid" column of table "mdl_scorm_scoes_track". Once an option has been selected set variable $attemptchoice to the value in the "attempt" column from table "mdl_scorm_scoes_track"
// Submit button displays the records from table "mdl_scorm_scoes_track" which have a value in the column "scormid" which matches $coursechoice AND have a value in the column "userid" which matches $userchoice AND have a value in the column "attempt" which matches $attemptchoice
$sql="SELECT name FROM mdl_scorm";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<SELECT NAME=course>
<OPTION VALUE=0>Choose the course
<?=$options?>
</SELECT>
<?php
$sql="SELECT username FROM mdl_user";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<SELECT NAME=user>
<OPTION VALUE=0>Choose the user
<?=$options?>
</SELECT>
<?php
$sql="SELECT attempt FROM mdl_scorm_scoes_track";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<SELECT NAME=attempt>
<OPTION VALUE=0>Choose the attempt
<?=$options?>
</SELECT>
<?php
$finalresult = SELECT element, value FROM mdl_scorm_scoes_track WHERE scormid=$coursechoice AND userid=$userchoice AND attempt=$attemptchoice
while ($testrows = mysqli_fetch_array($finalresult)){
echo $testrows['value'];
I think the recommendation below will get your dropdowns working... To actually pull information dynamically based on the dropdowns will require a form, a submit button and some parsing of the $_POST variable.
One step at a time, lets get your dropdowns working.
First, don't forget to close your options with </option> after .$thing:
$options.="<OPTION VALUE=\"$id\">".$thing."</OPTION>";
and
<OPTION VALUE=0>Choose the course</OPTION>
In addition, you are only selecting the name, if you want the id you need to select that too.
$sql="SELECT name, id FROM mdl_scorm";
and finally, to use the results of the query as an associative array you need mysql_fetch_assoc
while ($row=mysql_fetch_assoc($result)) {
That last was was probably what was tripping you up most.
Hope that helps.

Let user create table based on existing table

After registering at my site, I want the user to select some elements from a table that already exists in my DB, and add them to "their" column in another table.
Say this is the existing table in a MySQL DB:
ID Item Color
1 Car Red
2 Apple Green
3 Trophy Gold
4 Suit Black
I would want the user to fill out a form where they:
Are presented with a dropdown list, to choose items from (based on the existing table).
When they have chosen up to x amount of items, they submit their "inventory" which..
Adds the selected items to their own column in a table that holds "user_inventory"
So this second table (user_inventory) should look something like this:
User_id item_id1 item_id2 item_id3
1 4 3 1
2 3 1 4
3 2 4 1
4 2 4 3
I don't expect you to write the code for me or anything, but I would be thrilled if you could answer these questions:
Is this possible?
Can you direct me to a similar type of thread or article that helps me write the code?
If you can not direct me anywhere, please help me in whichever way you see fit.
It is possible.
Firstly, read from the "existing" db table to create the select menu, example:
<select name="item">
<?php
$sql = "SELECT * FROM existing";
$query = mysqli_query($mysqli, $sql);
while ($result = mysqli_fetch_array($query)) {
$item = $result['item'];
echo "<option value='$item'>$item</option>";
}
?>
</select>
This will create the "Item" select menu, I'm sure you can figure out how to do the other menus by yourself, all that is left now is to submit and store into the new table which I assume you already know how to do.
if(isset($_POST['yoursubmitname'])) {
$item = $_POST['item'];
/// RUN THE INSERT COMMAND HERE //
}

Categories