I created a dynamic table row provide as in http://bootsnipp.com/snippets/OeZkZ#comments
i am facing problem in inserting into database using loop.
the first row is taking fields as the its 0 var
{
1
}
$count = 1;
if(isset($_POST['byMonth']))
{
for( $i = 1; $i <= $count; $i++ )
{
$year = $_POST['year'.$i];
$month = $_POST['month'.$i];
$actual = $_POST['amount'.$i];
//$date = $_POST['entry'.$i];
echo $que = "insert INTO worker_by_month(uid,year,month,actual_amount,date) VALUES ('".$uid."','".$year."','".$month."','".$actual."','".$entry."')";
$mysqli->query($que);
$count = $_POST['items'];
}
}
this is what i have done.
Related
In case i need to replace table value with 500 rows contains random numbers. the problem is how to looping to get the ID 1 - 500, it is possible ?
<?php
$first = sprintf("%06d", mt_rand(1, 999999));
$second = sprintf("%06d", mt_rand(1, 999999));
$third = sprintf("%06d", mt_rand(1, 999999));
for($x = 1; $x <= 500; $x++) {
$sql = "UPDATE `random_num` SET
`rand_1` = '$first',
`rand_2` = '$second',
`rand_3` = '$third'
WHERE id = '$x';";
}
?>
whats wrong with your code?
just move the random number generation inside the for.. loop and u are done
<?php
function s_random()
{
return sprintf("%06d", mt_rand(1, 999999));
}
for($x = 1; $x <= 500; $x++) {
$sql = "UPDATE `random_num` SET
`rand_1` = '".s_random()."',
`rand_2` = '".s_random()."',
`rand_3` = '".s_random()."'
WHERE id = '".$x."'";
}
if you do it this way, that will be 500 calls to the DB and each row will have different random number
if you want the same random number in each row, just use the in keyword
"UPDATE `random_num` SET
`rand_1` = '".s_random()."',
`rand_2` = '".s_random()."',
`rand_3` = '".s_random()."'
WHERE id IN ('".implode(',',range(1,500))."'";
you can also create the query in one go
<?php
function s_random()
{
return sprintf("%06d", mt_rand(1, 999999));
}
$sql = "INSERT into random_num (id,rand_1,rand_2,rand_3) VALUES";
$val = [];
$i = 1; //begin ID
do{
$val[] = "($i, ".s_random().",".s_random().",".s_random().")";
$i++;
}while ($i < 500); //end ID
echo '<pre>';
$sql .= implode(',',$val);
$sql .= ' ON DUPLICATE KEY UPDATE rand_1 = VALUES(rand_1), rand_2 = VALUES(rand_2), rand_3 = VALUES(rand_3)';
echo ($sql);
In Mongodb after inserting few data insertion is getting slow, i am using batch_insert for insertion. While inserting i need to check some conditions also.
To insert 20k data itself takes more than 1 hour.
In temporary_table i am having 1L data
$batchSize = 20;
$documents = array();
$count = count($pending_contacts_data);
$count =1;
$temporary_data = array();
for($i=0;$i<$count;$i++){
$multiple_temporary_data = $this->mongo_db->select('*')->where(array('contact_id'=>(int)4,'status'=>1))->limit(10000)->get('temporary_table');
$temporary_data = array_merge($temporary_data,$multiple_temporary_data);
}
$count1 = count($temporary_data);
$documents = array();
for ($i=0; $i<$count1; $i++)
{
$obj_id[] = $temporary_data[$i]->_id;
$test_email = $this->mongo_db->select('*')->where(array('encrypted_email'=>$temporary_data[$i]->encrypted_email))->get('email_table');
if(empty($test_email)){
$document = array('email_id'=>$temporary_data[$i]->email,
'encrypted_email'=>$temporary_data[$i]->encrypted_email,
'encrypted_key'=>$temporary_data[$i]->encrypted_key,
'encrypted_iv'=>$temporary_data[$i]->encrypted_iv,
'status'=>(int)1,
'opend_supression_status'=>''
);
array_push($documents, $document);
if ((($i % $batchSize) === 0)) {
$insert = $this->mongo_db->batch_insert('opend_contacts_email_new1',$documents);
$update_temporary =$this->mongo_db->where_in('_id',$obj_id)->set(array('status'=>13))->update_all('temporary_data');
$documents = array();
}
}
}
What went wrong with your code is after the 500th element each new item acted as single batch insert. And $documents are just array of one element for the rest of loop. U should have used the modulus operator %
$count = 20000;
$batchSize = 50;
for ($i=1; $i<=$count; ++$i){
$test_email = $this->mongo_db->select('*')->where(array('email_id'=>$temporary_data[$i]->email_id))->get('email_table');
if(empty($test_email)){
$document = array('email_id'=>$temporary_data[$i]->email,
'encrypted_email'=>$temporary_data[$i]->encrypted_email,
'encrypted_key'=>$temporary_data[$i]->encrypted_key,
'encrypted_iv'=>$temporary_data[$i]->encrypted_iv,
'status'=>(int)1,
'suppression_status'=>''
);
array_push($documents, $document);
if (($i % $batchSize) === 0) {
$insert = $this->mongo_db->batch_insert('email_table',$documents);
$documents = array();
}
}
}
I want to update my MySQL table. When I type the ID as a number works, but when using a variable instead, it does not work.
What I am trying to do is order elements of an html table by column.
I have e.g. 4 Columns:
$colname = array("Column1", "Column2", "Column3", "Column4");
I get the IDs of the elements already sorted from the URL variable:
$strTaskIds = $_GET["taskIds"];
// for example: $strTaskIds = "3;1;32_4;5_6;36_34;7"
Now I split the string into a 2D-Array and update the MySQL table:
$arrTaskIds = explode("_", $strTaskIds);
for($i = 0; $i < count($arrTaskIds); $i++) {
$arrIdsPerCol = explode(";", $arrTaskIds[$i]);
for($j = 0; $j < count($arrIdsPerCol); $j++) {
$sql = "UPDATE tasks SET col='$colname[$i]', rank=$j WHERE id=$arrIdsPerCol[$j]";
}
if($conW->query($sql) === TRUE) {
$error = 0;
} else {
$error = 1;
}
}
When I write a number E.G 7 instead of the variable $arrIdsPerCol[$j] it works.
Writing (int)$arrIdsPerCol[$j] does not work either.
The reason i gave you no error message is that there was none. It just looked like the MySQL table is not updating.
After starring at my code for quite a long time a found the problem.
I placed the query() code in the outer loop. But i needed it in the inner loop.
Problem solved:
$arrTaskIds = explode("_", $strTaskIds);
$error = 0;
for($i = 0; $i < count($arrTaskIds); $i++) {
$arrIdsPerCol = explode(";", $arrTaskIds[$i]);
for($j = 0; $j < count($arrIdsPerCol); $j++) {
$sql = "UPDATE tasks SET col='$colname[$i]', rank=$j WHERE id=$arrIdsPerCol[$j]";
if($conW->query($sql) === TRUE) {
} else {
$error = 1;
}
}
}
I want to display the last modified variable outside the for loop
Code:
for( $i = 1; $i <= 100; $i++ )
{
$pm_discussion = $_POST['pm_discussion'.$i];
$pm_update = $_POST['pm_update'.$i];
$pm_reports = $_POST['pm_reports'.$i];
$pm_informed = $_POST['pm_informed'.$i];
$pm_complete = $_POST['pm_complete'.$i];
}
**echo $pm_discussion;**
For example:
$i inside the for loop having values from 1 to 6.
Then it should display $pm_discussion = $_POST['pm_discussion'.$i];
The above $i should be 6.
Try following code.
$temp='';
for( $i = 1; $i <= 100; $i++ )
{
$pm_discussion = $_POST['pm_discussion'.$i];
$pm_update = $_POST['pm_update'.$i];
$pm_reports = $_POST['pm_reports'.$i];
$pm_informed = $_POST['pm_informed'.$i];
$pm_complete = $_POST['pm_complete'.$i];
if($pm_discussion!='')
$temp = $pm_discussion;
}
echo $temp;
Try this, it will alsays store the last non empty value to the variable -
for( $i = 1; $i <= 100; $i++ )
{
if (!empty($_POST['pm_discussion'.$i])) {
$pm_discussion = $_POST['pm_discussion'.$i];
}
if (!empty($_POST['pm_update'.$i])) {
$pm_update = $_POST['pm_update'.$i];
}
if (!empty($_POST['pm_reports'.$i])) {
$pm_reports = $_POST['pm_reports'.$i];
}
if (!empty($_POST['pm_informed'.$i])) {
$pm_informed = $_POST['pm_informed'.$i];
}
if (!empty($_POST['pm_complete'.$i])) {
$pm_complete = $_POST['pm_complete'.$i];
}
}
echo $pm_discussion;
I have a for loop like so:
for((int) $i = 0; $i < $number_of_updates; $i++)
{
//query here
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);
$points = 1;
//some functions to execute here... and then
//store results in session to show on other page
$output = $points.' times '.$name;
$_SESSION['item'][] = $output;
}
And then I show my results on view page like so:
foreach (array_unique($_SESSION['item']) as $output)
{
echo ('<p>'.$output.'</p>');
}
It echoes my results out of the loop like so:
1 times foo
1 times foo
1 times bar
1 times foo
etc...
Now the question. How do I sum those results up so they don't duplicate? Instead they are shown like so:
3 times foo
1 times bar
User array_count_values
for(/*(int) <- this is not neccessary (c++ background?) */ $i = 0; $i < $number_of_updates; $i++)
{
// your code
$_SESSION['item'][] = $name;
}
foreach(array_count_values($_SESSION['item']) as $name => $times)
echo ('<p>'.$times.' times '.$name.'</p>');
Of course counting the values directly is more memory and time efficient. This version is only neccessary if you somehow need to preserve the order of the elements.
Why didn't you store in your array directly the sum?
for((int) $i = 0; $i < $number_of_updates; $i++)
{
//query here
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);
$points = 1;
//some functions to execute here... and then
//store results in session to show on other page
$_SESSION['item'][$name] +=$points;
}
Try this:
for((int) $i = 0; $i < $number_of_updates; $i++)
{
//query here
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);
$points = 1;
//some functions to execute here... and then
//store results in session to show on other page
//$output = $points.' times '.$name;
//$_SESSION['item'][] = $output;
if( isset( $_SESSION['count'][$name] )){
$prev = $_SESSION['count'][$name];
}else{
$prev = 0;
}
$_SESSION['count'][$name] = $points + $prev ;
}
print_r( $_SESSION['count'] );