I have a function for dropdown list, which shows me the names for filtering results from two different queries. The one with the numeric value from 1-4 and the other with 10-110, some of the results have the same strings:
function filter_workhires($status) {
$status = array();
$status[1] = 'booked';
$status[70] = 'booked';
$status[2] = 'partiallyattended';
$status[90] = 'partiallyattended';
$status[3] = 'fullyattended';
$status[100] = 'fullyattended';
$status[4] = 'notattended';
$status[80] = 'notattended';
$status[10] = 'status_user_cancelled';
$status[20] = 'status_session_cancelled';
$status[30] = 'status_declined';
$status[40] = 'status_requested';
$status[50] = 'status_approved';
$status[60] = 'status_waitlisted';
$status[110] = 'status_not_set';
return $status;
}
In this form, I get double names for examle booked. How to combine the statuses according to the strings they show?
return array_unique($status);
That's about it :)
Output
Array
(
[1] => booked
[2] => partiallyattended
[3] => fullyattended
[4] => notattended
[10] => status_user_cancelled
[20] => status_session_cancelled
[30] => status_declined
[40] => status_requested
[50] => status_approved
[60] => status_waitlisted
[110] => status_not_set
)
Change it around so the description is the array key and the ID of the status is the value. When you build the array and you hit a duplicate you can then add multiple ID's to the value in some kind of structure like a comma separated string. So you end up with:
$status['booked'] = '70';
$status['partiallyattended'] = '2, 90';
$status['fullyattended'] = '3, 10';
...
When someone chooses an item you explode the value and get all the ID's that relate to that status.
Related
I have an multidimensional array made from database query and is like that:
Array
(
[0] => Array
(
[0] => -8.63296022565696
[x] => -8.63296022565696
[1] => 41.1584289069069
[y] => 41.1584289069069
[2] => 0
[seq] => 0
[3] => 2
[seq2] => 2
[4] => -8.63306031211831
[next_x] => -8.63306031211831
[5] => 41.1584543235506
[next_y] => 41.1584543235506
[6] => -8.64195115878864
[alert_x] => -8.64195115878864
[7] => 41.1599295066425
[alert_y] => 41.1599295066425
[8] => 54e728edafac1
[route] => 54e728edafac1
[9] => 54e728edafac1
[routeid] => 54e728edafac1
[10] => 2
[counttargetinter] => 2
[11] => passeio
[type] => passeio
[12] => 1355
[arcid] => 1355
)
All the values are repeated because have a key number and a key name.
Example: The value '-8.63296022565696' are in key "0" and "X".
How I can remove the duplicated?
This is how i made the array:
$query = "SELECT * FROM foo;";
$startRows = pg_query($connection, $query);
$startInfo = array();
while($list = pg_fetch_array($startRows)) {
$startInfo[] = $list;
}
Of course you can't mess with the generate JSON string to deal with the dups. You solve it during the creation of the array itself before encoding. Looking at the structure, this seems to be the problem of fetching both numeric and column indices.
Since you haven't posted any codes related to actually creating this JSON string, just use this basic idea on how to get rid of them.
If you intent do remove those numeric indices, you'll probably need to use fetch_assoc() flavours of your database API, so that in turn, you'll only get the column name indices instead of having them both.
Here's the idea:
$data = array(); // initialization of the container
while($row = your_fetch_assoc($result)) { // use assoc() instead to exclude those numeric indices
$data[] = $row;
}
echo json_encode($data);
Depending on what API you're using, if its PDO, either use -->fetch(PDO::FETCH_ASSOC) or just ->fetchAll(PDO::FETCH_ASSOC) without the need of a loop. If its MySQLi, then just use ->fetch_assoc()
EDIT: At last your codes, as I have suspected you're using _array() function which results associative and numeric indexed rows.
$query = "SELECT * FROM foo;";
$startRows = pg_query($connection, $query);
$startInfo = array();
while($list = pg_fetch_assoc($startRows)) {
$startInfo[] = $list;
}
Use pg_fetch_assoc() instead of _array() so that you'll get the associative indices only.
Like Ghost:
You can use pg_fetch_row() to get numeric indices or pg_fetch_assoc() to get field name.
$query = "SELECT * FROM foo;";
$startRows = pg_query($connection, $query);
$startInfo = array();
while($list = pg_fetch_row($startRows)) {
$startInfo[] = $list;
}
Try this:
foreach($walkroute as $routepoints){
foreach($routepoints as $key => $value){
if (is_int($key)){
unset($routepoints[$key]);
}
}
}
You have to modify it due to the fact that i don't know, what the structure and names of your array actually are.
I can not wrap my head around how to do the following. I have two different lenght and different dimensional arrays that I want to combine so that the result array will have arrays1 + array2 cell that did not match.
This should help you understand what I mean
$dbquery_results1[0][] = array("1","5","b99");
$dbquery_results1[1][] = array("4","12","www");
$dbquery_results1[2][] = array("10","32","ccc");
$dbquery_results1[3][] = array("7","142","xx");
$dbquery_results2[0][] = array("c","10");
$dbquery_results2[1][] = array("as","1");
$dbquery_results2[2][] = array("fe","7");
$dbquery_combination[0][] = array("1","5","b99","as");
$dbquery_combination[1][] = array("7","142","xx","fe");
What I would like to get done
$i=0;
while(!empty($dbquery_results1[$i][0])) {
If($dbquery_results1[$i][0] == $dbquery_results2[any of the dimensions here][2])
{
$dbquery_combination[] = $dbquery_results1[$i][]+ $dbquery_results2[x-dimension][2];
} $i++;
}
So only if array1 has at [][0] same value as array2 [any dimension][1] will array1 + the array2 cell that is different be saved at array3.
Sorry that I am not too good expressing myself. Thanks a lot in advance.
So if you don't have the choice you can do it like this :
<?php
$dbquery_results1[0] = array("1","5","b99");
$dbquery_results1[1] = array("4","12","www");
$dbquery_results1[2] = array("10","32","ccc");
$dbquery_results1[3] = array("7","142","xx");
$dbquery_results2[0] = array("c","10");
$dbquery_results2[1] = array("as","1");
$dbquery_results2[2] = array("fe","7");
foreach($dbquery_results1 as $line_r1)
{
foreach($dbquery_results2 as $line_r2)
{
if($line_r1[0] == $line_r2[1])
{
$line = $line_r1;
$line[] = $line_r2[0];
$dbquery_combination[] = $line;
}
}
}
print_r($dbquery_combination);
?>
And the result is :
Array (
[0] => Array ( [0] => 1 [1] => 5 [2] => b99 [3] => as )
[1] => Array ( [0] => 10 [1] => 32 [2] => ccc [3] => c )
[2] => Array ( [0] => 7 [1] => 142 [2] => xx [3] => fe )
)
I think you are complicating the problem. I suppose that your array are the result of SQL request ? If it's the case, you should prefer to use joint SQL request.
//Coment Answer : Are they on the same server ? If yes, you can make request on multiple database at the same time by specifying full qualification.
Please help me on the PHPEXCEL. it shows only one letter.
this is my php code:
$sql_question = "SELECT * FROM tna_question WHERE title_id = '$tid' ORDER BY section_id";
$result_question = mysql_query($sql_question, $db);
$category = array();
while ($row = mysql_fetch_assoc($result_question)) {
$arr1 = $row['question'];
$arr = array_push($category ,$arr1);
$category_count++;
}
$arr3[] = $category;
the result from the sql query is an array:
Array ( [0] => gfhgfh [1] => gfhfg [2] => fggfdg [3] => fds [4] => asd [5] => fghgfh [6] => Policy Wordings / Coverage [7] => Risk Assessment / Survey & Underwriting [8] => Policy Wordings / Coverage [9] => Risk Assessment / Survey & Underwriting )
when i use this line:
$objPHPExcel->setActiveSheetIndex()->fromArray($category, NULL, 'C7');
it gives me only the first letter from each row
but if i make this one:
$objPHPExcel->setActiveSheetIndex()->fromArray($arr3, NULL, 'C7');
it'll give all the data in one row.
but The output that i want is like this:
You can use the below code to get the desired result :
foreach($arr3 as $k => $v){
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $k, $v);
}
Note : Changing the column value will make it go left and right only. In case you want to shift the entire thing down then replace $k by $k+$val where $val is the number of rows you want to shift down.
I have two PHP arrays. One contains a group name and another contains a pay wage value.
$group_wages_array = Array ( [0] => 1 [1] => 4 [2] => 1 [3] => 3 );
This means there are four employees on the schedule. Two are assigned to group 1, another to group 4 and the last to group 3.
The second array is as follows:
$tot_wages_array = Array ( [0] => 500 [1] => 44 [2] => 80 [3] => 11.25 );
This is a sample array of each employee's wage. Both arrays are constructed in order as values are added in a mysql while loop as it pulls the info from the database.
Later on down the line, I combine the two arrays to get one array where the key is the group number and the value is the total wages for that group:
$combined_group_wages = array_combine($group_wages_array, $tot_wages_array);
This works like a charm EXCEPT for when more than one employee is assigned to the same group. These arrays are built in a mysql while loop as it loops through each employee's info:
array_push($tot_wages_array, $totemp_wages_sch); // Add their wage to the array
array_push($group_wages_array, $emp_data['group_id']); // Add their group to the array
Instead of just pushing the data to the array, I need to do this... I know the english but I don't know how to code it:
If $emp_data['group_id'] exists as value in $group_wages_array, add nothing to this array but get the key. Add $totemp_wages_sch to $tot_wages_array where key = group_wages_array key
I know it sounds more like an SQL query but I have to keep the keys and values in order so that they can be combined later in the page. If I can get this to work right, The arrays shown in the example would be:
$group_wages_array = Array ( [0] => 1 [1] => 4 [2] => 3 );
$tot_wages_array = Array ( [0] => 580 [1] => 44 [2] => 11.25 );
$combined_group_wages = array_combine($group_wages_array, $tot_wages_array);
$combined_group_wages = Array ( [1] => 580 [4] => 44 [3] => 11.25 );
...I've got to make this work using PHP. Any ideas?
I came up with a solution based on a combination of two of the answers submitted below. Here it is if it can help someone:
if(in_array($emp_data['group_id'], $group_wages_array)){
$key = key($group_wages_array);
$tot_wages_array[$key] += $totemp_wages_sch;
} else {
array_push($group_wages_array, $emp_data['group_id']);
array_push($tot_wages_array, $totemp_wages_sch);
}
This should do it:
$group_wages_array = array(1, 4, 1, 3);
$tot_wages_array = array(500, 44, 80, 11.25);
$combined_group_wages = array();
for ($i=0; $i<count($group_wages_array); $i++) {
$group = $group_wages_array[$i];
if (array_key_exists($group_wages_array[$group], $combined_group_wages)) {
$combined_group_wages[$group] += $tot_wages_array[$i];
} else {
$combined_group_wages[$group] = $tot_wages_array[$i];
}
}
print_r($combined_group_wages);
Yields:
Array
(
[1] => 580
[4] => 44
[3] => 11.25
)
But I recommend that you just switch to using objects to better represent your data.
If I could see the entirety of the code that would help a lot, but here's your English converted to php. Show me more code and I can perfect it, until then try this ->
if(in_array($emp_data['group_id'], $group_wages_array)){
$key = key($group_wages_array);
$tot_wages_array[$key] = $totemp_wages_sch;
} else {
array_push($group_wages_array, $emp_data['group_id']);
}
I am trying to insert multiple rows in a MySQL table from PHP arrays. I managed with with help of other members to get set of values in a pair of brackets but when i try to insert this i get "Error: Column count doesn't match value count at row 1" I donot know where am i going wrong. my codes are as below: (The number of values i get vary according to user input)
$docno1=array();
$serialno = array();
$acc_name = array();
$debit = array();
$credit = array();
for ($i=1;$i<=$rowcount;$i++)
{
//echo 'Accountname'.$i.' :'.($_GET['accname'.$i]).'<br>';
$docno1 [] = ($_GET['docno']);
array_unshift($docno1,"");
unset($docno1[0]);
$serialno [] = $i;
array_unshift($serialno,"");
unset($serialno[0]);
$acc_name[] = ($_GET['accname'.$i]);
array_unshift($acc_name,"");
unset($acc_name[0]);
$debit[] = ($_GET['DrAmount'.$i]);
array_unshift($debit,"");
unset($debit[0]);
$credit[] = ($_GET['CrAmount'.$i]);
array_unshift($credit,"");
unset($credit[0]);
}
$sum_dr = array_sum ($debit);
$sum_cr = array_sum ($credit);
echo ' values of $multi<br>';
$multi = array(
($docno1),
($serialno), //Array for a row of fields
($acc_name),
($debit),
($credit),
($docno1)
);
print_r($multi);
$new = array();
foreach($multi as $key=>$value) {
$new[] = "'".implode("','", $value)."'";
}
echo '<br>Values of $new <br>';
print_r($new);
$query = "(".implode("), (",$new).")";
echo $query.'<br>';
mysql_query("INSERT INTO docitems (`docno`,`itemno`,`accountname`,`debit`,`credit`, `picrefno`) VALUES ".$query.";") or die('Error: ' . mysql_error());
echo "Inserted successfully";
die;
The results i get are :
values of $multi
Array
(
[0] => Array
(
[1] => 3434
[2] => 3434
)
[1] => Array
(
[1] => 1
[2] => 2
)
[2] => Array
(
[1] => Lemon
[2] => Kidney Beans
)
[3] => Array
(
[1] => 20
[2] => 10
)
[4] => Array
(
[1] => 0
[2] => 0
)
[5] => Array
(
[1] => 3434
[2] => 3434
)
)
Values of $new
Array
(
[0] => '3434','3434'
[1] => '1','2'
[2] => 'Lemon','Kidney Beans'
[3] => '20','10'
[4] => '0','0'
[5] => '3434','3434'
)
('3434','3434'), ('1','2'), ('Lemon','Kidney Beans'), ('20','10'), ('0','0'), ('3434','3434')
Error: Column count doesn't match value count at row 1
mysql_query("INSERT INTO docitems (`docno`,`itemno`,`accountname`,`debit`,`credit`, `picrefno`) VALUES ".$query.";") or die('Error: ' . mysql_error());
You are trying to insert something into 6 fields, so that $query string must have 6 values in it, or you get this error.
You have a lot of $query's that are 2 values. And that's not 6
It looks to me as if you are mapping your array the wrong way round. You're trying to add two records with six fields each, but what you're actually putting into the SQL statement are six records with two fields each.
This is why MySQL is complaining -- because you've told it you want to update six fields, but in each of the records you've given it, you've only specified two fields.
You need to build your array differently.
I assume that $docno1, $serialno, $acc_name, $debit and $credit will always all have the same number of array elements (it appears from your code that you are assuming this, so I'll follow you in your assumption).
In that case, you need to build your array something like this:
$multi = array();
foreach($docno1 as $key=>value) {
$multi[] = array(
$docno1[$key],
$serialno[$key], //Array for a row of fields
$acc_name[$key],
$debit[$key],
$credit[$key],
$docno1[$key])
}
Replace the block in your code where you set $multi with this, and your program should work.
Look at what print_r($multi) looks like now, and you'll see the difference.
(note, there are more efficient ways of writing your whole program than this, but I've focused on giving you a drop-in replacement for this specific bit, to help show you where you were going wrong, rather than simply rewriting the whole program for you)
Hope this helps.
If the error is occurring when trying to insert a row to your table, try specifying the list of fields, in the insert query -- this way, the number of data in the values clause will match the number of expected columns.
Else, MySQL expects six columns : it expects the specific inserts -- for which you didn't specify a value.