multiple foreach loops for different variables? - php

foreach ($textAr as $BachelorsDegrees)
{
foreach ($textArCert as $Certifications)
{
foreach ($textArDip as $Diplomas)
{
foreach ($textArAD as $AssociateDegrees)
{
foreach ($textArMD as $MastersDegrees)
{
foreach ($textArDD as $DoctorateDegrees)
{
foreach ($textArSD as $SpecialDegreePrograms)
{
what would be the best way to make the above code work (it's just a snippet of the entire code)? Each variable, for example $BachelorsDegrees, is a textarea box in the HTML form. I am trying to insert values from each textarea box into the database in individual rows. I got it working for one textarea box, but how to make it work for multiple textarea boxes?
Was thinking of doing an if else statement and an INSERT and UPDATE sql statement so that when one foreach loop worked, it would INSERT data into the database, then the loop would stop, then an else statement would go for the next foreach loop and an UPDATE query would update the next column for the same product instead of inserting. If I did multiple INSERT queries, I would be creating more rows for each variable, which I don't want. More rows for the values on each variable, but not rows for the actual variable. Each variable represents a column.
For example,
When the values from the BachelorsDegrees textarea get inserted into its column, the next loop will run (Certifications) into the next column. So I was thinking an UPDATE query would be effective. That way I don't have NULLS in every column.

If you have equal number of inputs for all the fields(Bachelorsdegree,certifications,etc), then you can take count of one input field into equation.e.g
$totalinputs=count($textAr);
for($i=0;$i<$totalinputs;$i++){
$bachelordegree=$textAr[$i];
$certificate=$textArCert[$i];
$diploma=$textArDip[$i];
.....
.....
//use above stored variables to insert/update into your respective table.
}

Related

PHP foreach not inserting whole array into mysql table

Hi I have created a simple foreach loop which is looping through an array and inserting the data into my db table. The problem I am having is it is only inserting 130 rows into my database even though the array contains 196.
Here is my code:
foreach ($currencyArray as $currency) {
if (count($currency) > 1) {
$currency_table = $db->prepare('
INSERT INTO currency_name (symbol, name, btc_value)
VALUES (:name, :symbol, :btc_value)
');
$currency_table->bindParam(':name', $currency[0]);
$currency_table->bindParam(':symbol', $currency[1]);
$currency_table->bindParam(':btc_value', $currency[2]);
$currency_table->execute();
}
}
I have made sure it isnt the if statement causing the problem. I added a count to inside the if statement and this count 197 records which is the amount of rows I am expecting in my db table, but it is only storing 130 rows?
If you also believe there is a faster way of inserting the array into the db table please feel free to share :)
I found out what the error was it, it was my mistake some of the names I am inserting into the database are longer than I have allowed

single sql database entry with muliple values into an array

the script querys database and retrieves a single entry that has mulitple numbers
SELECT jnum from database where x = y
output = 11111,22222,33333,44444
So i explode that on , and get $variable[0] = 11111 and $variable[1]= 22222
What i want to do is perform a query on another table using each of those numbers (numbers will be different each time and there may be any number of numbers).
is there a way to structure a foreach for each entry in the array or a while loop that counts so that i can query the database for each of the values i get from output above.
i don't know if i am conveying what 'im trying to do here very clearly so i apologize in advance.
i get a single entry for the database table and it contains a string (11111,22222,33333)
i explode on , and get the array variable[]
there will not always be 3 entries sometime there could be 5 or 7 or 10 or 1 but each one will be unique.
but for each value i want to query a db table and retrieve all the rows that have that single number($variable[]) as an entry.
Not sure if a loop count or a foreach statement would work. any ideas?
Well assuming these are values in a single column there is no need to look you can use WHERE ... IN:
SELECT * FROM the_other_table WHERE some_col IN ('11111','22222','33333')
Check out foreach loops - http://php.net/manual/en/control-structures.foreach.php
foreach ($variable as $value) {
$myquery = "some query using $value";
// then execute your query
}

mysql In clause to avoid loop

I have to retrieve the history of a user and I have 4 tables whose data depend on each other.I can retrieve the data using loops,but I instead used the "where IN ()" clause and I implode the output of the previous query.However,if the list I provide to "where IN()" is empty it return an error.Is it that IN() cannot be empty?
When imploding an array for the IN clause, i do one of two things
1: Check if you even need to run the query at all
if(!empty($some_array)) {
//run mysql query
}
else {
// if you need to do something if the array is empty, such as error or set some defaults, do it here
}
2: A value in the array initiliser which is not ever in the database (for example, if im selecting based on a auto incrememnt id, i use zero as a default array value to stop any issues with empty data sets, as zero will never be in my id column).
$some_array = array(0);
You can add an empty value to the start, such as IN (0,your values here)

Submitting form with 100 <select>s to database

I have a pretty basic page that shows every five minutes of the day (12:00, 12:05, 12:10, etc). Next to each time is a dropdown <select> with the numbers 1-9. The name of the <select> is the name of the time it corresponds to. The user selects one of the numbers, goes to the next, selects a number, etc. until they get to the bottom and hit submit.
In the database table, each time of the day has its own row, with another field for the number.
How can I submit this form and have it insert the number based on the time of day to the proper row? I can't wrap my head around this for some reason. I don't even need to do validation. This is not a live website.
I think the name of the select should be times[] and the value should be the time.
so in php you can loop through the times[] array and get the row that corresponds to its value. Then update the count accordingly.
<select name="times[]" multiple>
<option value="19:00">1</option>
<option value="19:05">2</option>
</select>
In php psuedo code
foreach ($_GET['times'] as time) {
*sanatize
select row where time = time
update count
}
Assuming you have a table like this:
MyValues
TimeOfDay varchar(50),
Value int
And your posted form data looks like this:
?19:00=1&19:05=2
You will loop through each posted form field and use the key and value of each field to generate an update statement like this:
foreach($_POST as $key => $value)
{
update MyValues set Value = $value where TimeOfDay = $fieldName
}
it seems #dm03514 is good options, but had rather use the $_POST than $_GET, and on the process you can do like this
for($=0;$i<count($_POST['times']);$i++){
#do some action with $_POST['times'][$i]
}

php+mysql : increase a cell's value without checking it?

I've got a form with checkboxes set to bitwise values (CB1=1,CB2=2,CB3=4,CB4=8), and I want to store their sum in a single cell (to avoid having empty fields). The checkboxes have the same name but different values, and the data is sent to the processing php script as a serialised string, ie name=value&name=value2&name3=value3&name=&name=value5
Currently I can separate the string and get the values into the proper cells rather efficiently/easily. But, I'm wondering if there is a way to insert the first value into the cell then add subsequent values to the same cell. I imagine it would look something like this:
foreach ( $qaPairs as $pair ) {
list($question , $answer) = explode('=', $pair);
// ^ splits Q1=A1 into $question=Q1 and $answer=A1
// this mysql_query is a modified version of what I'm currently using
mysql_query("UPDATE $table SET `$question`=`$question`+'$answer' WHERE `key`='$key';") or die(mysql_error());
// $question is also the name of the column/field
} // end foreach
I dunno if it makes a difference, but: there are other datatypes (besides bitwise integers, such as text) and other form types (like textfields).
P.S. I would rather not have to somehow check if there are multiple instances of the same name and then do addition.
P.P.S. I got the UPDATE idea from this question: MySQL Batch increase value?, and I tried it out, but it didn't update null values (which I need it to do).
Thanks in advance!

Categories