update_batch in codeigniter - php

Here is my code that i used to update all the rows that am getting for that i used update_batch function but its not working properly
public function edit_project_involved($users)
{
foreach ($users as $v_user) {
$data=array('involved'=>1);
$v=$this->db->where('user_id',$v_user);
$query=$this->db->update_batch('tbl_users',$data,$v);
}
if($query)
{
return true;
}
}
my $users var_dump looks like this
C:\wamp64\www\spectra\application\models\Project_model.php:536:
array (size=3)
0 => string '40' (length=2)
1 => string '42' (length=2)
2 => string '37' (length=2)
am getting an error like this
One or more rows submitted for batch updating is missing the specified index.
Filename: C:/wamp64/www/spectra/system/database/DB_query_builder.php
Line Number: 2010
My table looks like this
user_id username involved
1 admin 0
36 siraj 0
37 faizal 0
38 nesru 0
40 jaseer 0
42 maltu 0
43 shahul 0
44 samsheera 0
var_dump($data) looks like this
C:\wamp64\www\spectra\application\models\Project_model.php:544:
array (size=3)
0 =>
array (size=2)
'user_id' => string '40' (length=2)
'involved' => int 1
1 =>
array (size=2)
'user_id' => string '42' (length=2)
'involved' => int 1
2 =>
array (size=2)
'user_id' => string '37' (length=2)
'involved' => int 1

You not followed the rules mentioned in documentation https://www.codeigniter.com/user_guide/database/query_builder.html
Try this (notice update_batch outside of loop and how the array made)
foreach ($users as $v_user) {
$data[] = array(
'user_id' => $v_user,
'involved'=> 1
);
}
$query = $this->db->update_batch('tbl_users',$data,'user_id');

Related

Looping over Joined query

I've got a query which outputs the following data:
Treatment ID --------- Treatment_Name ---------- Customer_type
1 Treatment 1 Ladies
1 Treatment 1 Mens
3 Treatment 2 Ladies
4 Treatment 3 Mens
I need to loop over this and output a similar structure to this:
<div class="mens ladies"> <!-- these are read from the customer_type column -->
<h4>Treatment 1</h4> <!-- this is read from the treatment_name column -->
</div>
So basically grouping by the Treatment name (or ID) and then outputting all of the customers related to this treatment.
Coming from a ColdFusion background I assumed this would be easy with a Query of Queries etc. however I've struggled with coming up with a PHP solution.
I've tried to do a while loop and only show data if the treatment_name has changed which seems to work fine but I'm not sure how to display the customer_types related to this.
Any ideas? Thanks.
Edit
Here is a copy of the query returned from the db using a mysqli_fetch_all:
array (size=22)
0 =>
array (size=3)
0 => string 'Mens' (length=4)
1 => string '1' (length=1)
2 => string 'treatment 1' (length=11)
1 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '1' (length=1)
2 => string 'treatment 1' (length=11)
2 =>
array (size=3)
0 => string 'Kids' (length=4)
1 => string '2' (length=1)
2 => string 'treatment 2' (length=11)
3 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '2' (length=1)
2 => string 'treatment 2' (length=11)
4 =>
array (size=3)
0 => string 'Mens' (length=4)
1 => string '2' (length=1)
2 => string 'treatment 2' (length=11)
5 =>
array (size=3)
0 => string 'Mens' (length=4)
1 => string '3' (length=1)
2 => string 'treatment 3' (length=11)
6 =>
array (size=3)
0 => string 'Kids' (length=4)
1 => string '3' (length=1)
2 => string 'treatment 3' (length=11)
7 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '3' (length=1)
2 => string 'treatment 3' (length=11)
8 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '4' (length=1)
2 => string 'treatment 4' (length=11)
It continues in the same format...
Hope this answers your question:
<?php
$link = mysqli_connect("hostname","username","password","database","port") or die("Error " . mysqli_error($link));
$query = "SELECT treatment_name,Customer_type from Treatments" or die("Error at.." . mysqli_error($link));
$result = $link->query($query);
while($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
//I have used this sample array: Try and make your result set this way
$data = array(
array('treatment_name'=>'Treatment1','Customer_type'=>'Mens'),
array('treatment_name'=>'Treatment1','Customer_type'=>'Ladies'),
array('treatment_name'=>'Treatment2','Customer_type'=>'Mens')
);
foreach ($data as $key => &$value) {
$temp[$value['treatment_name']][$key] = $value['Customer_type'];
}
//echo "<pre>";
//print_r($data);
//print_r($temp);
//die;
foreach($temp as $key => $value){
echo "<div class = \"";
foreach($value as $key2 => $value2)
echo " ". $value2. " ";
echo "\"><h4>".$key."</h4></div></br>";
}
?>
//Output
<div class = " Mens Ladies "><h4>Treatment1</h4></div></br><div class = " Mens "><h4>Treatment2</h4></div></br>
You can use the commented lines for checking the data and adjusting accordingly.
By the way, this code directly prints the output as html, so your browser will render it. Right click the page and view source if you want the content!

Var_dump shows value but returns null mysql query

Using PHP and MYSQL I do a left join on 2 tables. I then use var_dump to show the results.
$sql = "SELECT pro_table.pro_id, pro_table.sport_id, pro_table.pro_name,
results_table.pro_id, results_table.year, results_table.result_rank, results_table.result_score
FROM pro_table
LEFT JOIN results_table
ON pro_table.pro_id=results_table.pro_id
WHERE sport_id LIKE '$dropdownrecord'";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
var_dump($record);
this outputs
array (size=13)
0 => string '276' (length=3)
'pro_id' => null
1 => string '14' (length=2)
'sport_id' => string '14' (length=2)
2 => string 'Bradley Wiggins' (length=15)
'pro_name' => string 'Bradley Wiggins' (length=15)
3 => null
4 => null
'year' => null
5 => null
'result_rank' => null
6 => null
'result_score' => null
I am not sure why my pro_id returns a 276 yet also says null?
My Problem was that I had both columns in both tables named the same think. I renamed one column and now it is working property.
Try something like this :
for($i=0;$record[$i] = $mysql_fetch_array($myData); $i++){
var_dump($record[$i]);
}

Retrieving specific data from sql query

Let's say I have the following query, wich gives me the amount of coupons downloaded and redeemed, grouped by date: [EDIT](I can't modify this query)[/EDIT]
$sql = "SELECT DATE(datetime) ,
SUM(CASE WHEN datetime!='' THEN 1 ELSE 0 END) As downloaded ,
SUM(CASE WHEN used = 1 AND DATE(datetime) != '' THEN 1 ELSE 0 END) AS redeemed
FROM Promotion_Redeem
WHERE datetime BETWEEN '2013-10-01' AND '2013-10-04'
GROUP BY DATE(datetime)";
How can I get the sum of downloaded and the sum of redeemed? this should be within $sql, somewhere... below is the result for this query:
row number 1
array (size=3)
0 => string '2013-10-01' (length=10)
1 => string '126' (length=3)
2 => string '11' (length=2)
row number 2
array (size=3)
0 => string '2013-10-02' (length=10)
1 => string '106' (length=3)
2 => string '5' (length=1)
row number 3
array (size=3)
0 => string '2013-10-03' (length=10)
1 => string '228' (length=3)
2 => string '12' (length=2)
row number 4
array (size=3)
0 => string '2013-10-04' (length=10)
1 => string '149' (length=3)
2 => string '9' (length=1)
[EDIT]bove you can see I get 4 rows, each one whith an array... I want, for instance, the sum of the third field from each of these arrays... In my example, this would equals to 37 (that means, 37 coupons redeemed)[/EDIT]
I got this data structure after using this:
while ($row = mysql_fetch_row($result)) {
echo "row number ".++$i;
var_dump($row);
}
Thanks in advance
Modify your php loop like so:
$downloaded=0;
$redeemed=0;
while ($row = mysql_fetch_row($result)) {
echo "row number ".++$i;
$downloaded+=$row[1];
$redeemed+=$row[2];
var_dump($row);
}
echo "Downloaded: $downloaded<br>";
echo "Redeemed: $redeemed<br>";

How to group array but not create new object?

I have an array that looks like this:
array (size=21)
0 => string '2' (length=1)
1 => string '' (length=0)
2 => string '20' (length=2)
3 => string '19' (length=2)
4 => string '14' (length=2)
5 => string '13' (length=2)
6 => string '' (length=0)
7 => null
8 => null
9 => string '20' (length=2)
10 => null
11 => string '10' (length=2)
12 => string '' (length=0)
13 => null
14 => string '13' (length=2)
15 => null
16 => string '' (length=0)
17 => null
18 => null
19 => string '' (length=0)
20 => string '20' (length=2)
And I would like to create a new array from this array by grouping rows with the same string. e.g.
2 => string '20' (length=2) with 20 => string '20' (length=2) and with 9 => string '20' (length=2)
and
5 => string '13' (length=2) with 5 => string '13' (length=2)
etc.
and order the new created array rows based on how many times the string occures there.
Order need to be DESC from the most occurrences to the last like a classic top something chart (The most present strings are first and the least are low)
So, the modified array will look like this:
array (size=21)
0 => string '20' (length=2)
1 => string '13' (length=2)
...
I also need somehow to handle null results e.g. 17 => null to be not incorporated at all in the final array modified result.
This should do the trick:
// Filter the "null results" first
$myarray = array_filter($myarray, create_function('$arg', '
return !is_null($arg);
'));
$occurrences = array_count_values($myarray);
// EDIT: arsort preserves the key => value correlation
arsort($occurrences, SORT_NUMERIC);
var_dump(array_keys($occurrences));
Try this.
$result = array_count_values($a);
arsort($result);
$result = array_keys($result);

Count() returns different number of entries than var_dump()

Hey guys I'm tired and can't figure this one out so any help would be appreciated.
I have an array called $Row that I get from database table.
When I run var_dump($Row); I get the following:
array
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string 'erik' (length=4)
'username' => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
'email' => string '' (length=0)
4 => string '0' (length=1)
'date_join' => string '0' (length=1)
5 => string '0' (length=1)
'date_mod' => string '0' (length=1)
6 => string '1' (length=1)
'active' => string '1' (length=1)
7 => string '1' (length=1)
'admin' => string '1' (length=1)
8 => string '0' (length=1)
'deleted' => string '0' (length=1)
When I run echo count($Row); I get value 18.
Count and var_dump are right next to each other, there is no modification of $Row.
Question: Why does Count() return 18 entried when there are only 8 of them inside $Row shown by var_dump()? I guess I just don't understand count()... I checked http://php.net/manual/en/function.count.php, but still don't get it...
Edit: I understand whats wrong know, thanks everyone. Another question. How can I remove the ones that are in a string, for instance I want this kind of a table:
array
0 => string '1' (length=1)
1 => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
4 => string '0' (length=1)
5 => string '0' (length=1)
6 => string '1' (length=1)
7 => string '1' (length=1)
8 => string '0' (length=1)
* I'm using mysql_fetch_array() to retrieve the data and put it into a table.
You are having mixed array integer and associative and having 18 elements. This is I think returned from mysql_fetch_array which by default return associative and numeric indexed array.
Because your strings are also values in $Row they just seem to be indented because of the string char.
That means
'id' => string '1' (length=1)
'username' => string 'erik' (length=4)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
// ...
are also in your array
becuase you're getting back a mix of associative and numeric result set from you db ( you can precise which one you really want with FETCH_ASSOC or alike as a parameter to you dbvendor_query() function ).
There is very well 18 elements in your array.
I think that applying count to this:
0 => string '1' (length=1)
'id' => string '1' (length=1)
returns 2 instead of 1!
0 and id are two different items of the array!
This taken for the others 8 elements make the count return 18
Actually, there are indeed 18 elements in your array:
indices 0 through 8 (9 elements in total)
id, username, ... another 9 elements
9 + 9 = 18
If you only want the numeric indices, and supposing you're using MySQL:
$Row = mysql_fetch_array($result, MYSQL_NUM)
Source: http://php.net/manual/en/function.mysql-fetch-array.php

Categories