I have an "Events" table in MySQL :
EventsDate('id','event','start_date','end_date')
I'd like to check if multiple events have the same start date to show it differently in my HTML template.
My SQL request is :
SELECT * FROM EVENTSDATE where event='$id' and start_date>='$today' order by start_date asc
Now my foreach :
foreach ($upcomingDates as $value) { //$upcoming is the array with my sql request
}
How can I say : "if you find two rows with the same start_date, echo something"
I have a slightly different approach.
// Array to contain all values
$container = array();
// Loop through your existing array
foreach ($upcomingDates as $key => $value) {
// Check if the value is already in the container array
// If this is the case, its a duplicate.
if (array_key_exists($value['start_date'], $container)) {
$container[$value['start_date']]++;
echo $value.' is a duplicate with key '.$key;
}
// Add each value to the array
$container[$value['start_date']] = 1;
}
Another method is to use array_count_values()
foreach(array_count_values($upcomingDates) as $value => $c) {
if ($c > 1) {
echo $value.' is a duplicate';
}
}
Note that the second option won't work if your $upcomingDates is an array of arrays.
You can make an empty array before the for loop, and add each value in as a key. Then, on each iteration you can check that array for the key, like so:
$values = [];
foreach ($upcomingDates as $value) { //$upcoming is the array with my sql request
if(isset($values[$value])) //duplicate value found
//do something here
$values[$value] = 1;
}
Since you're ordering your events by start_date:
for ($i = 0, $length = count($upcomingDates); $i < $length; $i++) {
$date = $upcomingDates[$i];
if (isset($upcomingDates[$i + 1]) &&
$upcomingDates[$i + 1]['start_date'] == $date['state_date']) {
echo 'this and the next date are equal';
}
}
Try out GROUP BY
look here: GROUP BY
if you want to find duplicates, then you can directly get it from database
ex.
SELECT * FROM EVENTSDATE where event='$id' and start_date>='$today' GROUP BY start_date having count(start_date) > 1 order by start_date asc
or you can find duplicates from resulting array
return only duplicated entries from an array
Related
I am trying to create a number of arrays to add to a larger array which I will then index two dimensionally (for example my_array[i][j]).
I believe I am having some type of pointing issue... I am iterating through a loop.... at the top of each iteration I instantiate a new array with the same name. When I try and view the output each item in the array appears blank.
Is there a better way for me to instantiate the "temp" arrays to fill the larger (outer) array? Really appreciate any and all help, this community has helped me through so many questions :D
$main_array = array();
for($i = 0; $i < $member_count; $i++)
{
$temp_array = array();
//preform SQL query here
$sql_get_member_transactions = 'some SQL query to go to my DB';
foreach($sql_get_member_transactions as $row)
{
//cannot get array to update here
array_push($temp_array, $row['amount']);
}
array_push($main_array, $temp_array);
}
#***FULL CODE BELOW***
$club_transactions = array();
for($i = 0; $i < $member_count; $i++)
{
#create an array for each member in the club,
#add each memeber's array to a larger array once filled with deposit amounts
#here check if member had a deposit (later we will check ALL transaction types...)
#if no deposit on that date add a zero to their array
$temp_member_transactions = array();
$temp_member = my_members[$i];
$sql_member_transactions = "SELECT * FROM Transactions WHERE Date >= '$first_t_date' AND Date <= '$last_t_date' AND RelatedClubID = 'THH'";
$sql_get_member_transactions = mysqli_query($conn, $sql_member_transactions);
//here we will get all transactions that lie within the
//event-transaction-date-window... in otherwords, the date window
//in the year where members transactions occured
foreach($sql_get_member_transactions as $row)
{
//we only want to include the transaction amount
//in the member-specific array if it belongs to that member
//otherwise see "else"
if($row['T_Owner'] == $temp_member)
{
$z = $row['T_Amount'];
}
else
{
//set to 0 so that we still account for the transaction date
$z = 0;
}
array_push($temp_member_transactions, $z);
}
array_push($club_transactions, $temp_member_transactions );
I've got the following code which is within a sql loop to determine how many rows i output onto a spreadsheet. Basically without pasting the full thing as it's quite a lengthy statement, the top SQL statement will return 60 rows, which will contain the variables I enter into the original $data1 array.
$stmt2= $mysqV1->prepare("SELECT DISTINCT master_recipe, recipe, matl_id, comp_length, comp_width, comp_tck from components where recipe > 0 and matl_id > 0 order BY CAST(recipe AS UNSIGNED) ASC" );
foreach ($result2 as $key2=>$value2)
{
$data1[]=(array("Master Recipe"=>$master_recipe,"Recipe"=>$recipe,"Recipe Name"=>$recipe_name,"Material"=>$material,"Length"=>$comp_length,"Width"=>$comp_width,"Thickness"=>$comp_tck));
}
I then have a further nested loop (inside the original $result2 loop) which will determine how many elements i add to that array, as the value will change from record to record. I have tried to declare an array then use array push and array merge but neither of them seem to do what i want.
$temp7 = array($master_recipe);
$stmt7= $mysqV1->prepare("Select * from machine where master_recipe = ? order by route_header_id asc" );
$stmt7->execute($temp7);
$result7=$stmt7->fetchAll();
foreach ($result7 as $key7=>$value7)
{
$station_id = $value7['route_header_id'];
$time_taken = $value7['time_hrs'];
$a[] = (array("StationID"=>$time_taken));
array_push($data1,$a);
}
So what I would like this to do is add the contents of $a to the end of $data1 to give me 1 array value which then prints out to my spreadsheet(the print part is already working for the $data1 array) but it's not adding the $a to it.
Final result I would like to end up something like this for the value in $data1
$data1[]=(array("Master Recipe"=>$master_recipe,"Recipe"=>$recipe,"Recipe Name"=>$recipe_name,"Material"=>$material,"Length"=>$comp_length,"Width"=>$comp_width,"Thickness"=>$comp_tck,"$station_id1"=>$time_taken,"$station_id2"=>$time_taken2,"$station_id3"=>$time_taken3));
Put the row that you're adding to $data1 in the $a variable, then you can add new elements to that row before you push it into $data1.
foreach ($result2 as $value2) {
$master_recipe = $value2['master_recipe'];
$recipe = $value2['recipe'];
...
$a = array("Master Recipe"=>$master_recipe,"Recipe"=>$recipe,"Recipe Name"=>$recipe_name,"Material"=>$material,"Length"=>$comp_length,"Width"=>$comp_width,"Thickness"=>$comp_tck);
$temp7 = array($master_recipe);
$stmt7= $mysqV1->prepare("Select route_header_id, time_hrs from machine where master_recipe = ? order by route_header_id asc" );
$stmt7->execute($temp7);
while ($value7 = $stmt7->fetch())
{
$station_id = $value7['route_header_id'];
$time_taken = $value7['time_hrs'];
$a[$station_id] = $time_taken;
}
$data1[] = $a;
}
What if you change your initial set of $data1 to this:
$data1= array(
"Master Recipe"=>$master_recipe,
"Recipe"=>$recipe,
"Recipe Nme"=>$recipe_name,
"Material"=>$material,
"Length"=>$comp_length,
"Width"=>$comp_width,
"Thickness"=>$comp_tck
);
then, in your loop..
foreach ($result7 as $key7=>$value7)
{
$station_id = $value7['route_header_id'];
$time_taken = $value7['time_hrs'];
$data1[$station_id] = $time_taken;
}
I've got a small problem. I'm working on a little package/product-list.
If you're watching a Package, my website should show you which products are in there.
If a product is more than one time in it, the array should be deleted and the value of the leftover array should be + 1 (each deleted array).
So here's my code:
// $products_in_package has all products in it
// First of all, the products come from a db and don't have a count
// So i first give them a count of 1
foreach ($products_in_package as $product => $value) {
$products_in_package[$product]['count'] = intval(1);
}
foreach ($products_in_package as $product) {
$id_to_find = intval($product['ID']);
$product_count = intval($product['count']);
$found_id = 0;
// Now I try to find any ident products
// If found and over 1 time (beacouse he finds the first too of course)
// Then delete this array and count up the products count
for ($i=0; $i <= count($products_in_package); $i++) {
if(intval($products_in_package[$i]['ID']) === $id_to_find){
$found_id++;
if($found_id > 1){
$product_count = $product_count + 1;
$product['count'] = $product_count;
unset($products_in_package[$i]);
array_merge($products_in_package);
while($i > $products_in_package){
$i = 0;
}
}
}
}
}
What I'm getting is the correct multidimensional array but the count is still 1.
What's wrong with the code?
Everytime I try to log the code i'm getting the right integer. (No, I already tried to delete the chache)
But if I log the array out of the loops, I get always the count of 1.
$product is a copy of the array element, so when you do $product['count'] = $product_count you're assigning to a copy, not the original array.
You can fix this by using a reference in the foreach:
foreach ($products_in_package as &$product) {
I have database that contains scores which are stored daily. I want to average each months scores for each user. So far I have this:
DB structure:
id | name | tscore | added
int| string | float(100 or less)| date(2014-01-01 16:34:22)
Code:
while($row = mysql_fetch_assoc($getChartData)){ // Data from MySQL
$added_date = explode(' ',$row['added']); // Date formate 2014-01-01 16:34:22
$chartData[] = array(
'id' => $row['name'],
'tscore' => $row['tscore'],
'added' => $added_date[0] // Here I take the month only
);
}
if($_POST['range'] == 'month'){
foreach($chartData as $key => $value){
$added = explode('-',$chartData[$key]['added']);
$count = 1;
foreach($chartData as $key2 => $value2){
$added2 = explode('-',$chartData[$key2]['added']);
if($chartData[$key]['id'] === $chartData[$key2]['id'] && $added[1] === $added2[1]){ // if user is the same and the month is the same, add the scores together, increment counter, and unset 2nd instance
$chartData[$key]['tscore'] = ((float)$chartData[$key]['tscore'] + (float)$chartData[$key2]['tscore']);
$count++;
unset($chartData[$key2]);
}
}
$chartData[$key]['tscore'] = ($chartData[$key]['tscore']/$count); // Average all the scores for the month.
}
}
The problem is this method is deleting all the elements of the $chartData array. What can I try to resolve this?
You should try to solve it with MySQL. Try something like this (replace 'your_scores_table' with your table name):
SELECT
Score.name,
AVG(Score.tscore) AS `avg`,
CONCAT(YEAR(Score.added), '-', MONTH(Score.added)) AS `year_month`
FROM
your_scores_table AS Score
GROUP BY
Score.name ASC,
YEAR(Score.added) DESC,
MONTH(Score.added) DESC
;
Your logic is wrong. You are looping through the same array twice. Which means that the following if will always evaluate to true which means that array item will always get unset
//This will always be true
if($chartData[$key]['id'] === $chartData[$key2]['id'] && $added[1] === $added2[1]){
It may be simpler for you to create another array where you keep your scores. Something like
$aScores = array();
$count = 1;
foreach($chartData as $key => $value){
//Add score to a different array
$aScores[$value['name']]['tscore'] = (($aScores[$value['name']]['tscore'] + $value['tscore']) / $count);
$count++;
}
Also I would look into the MySQL AVG function. You could use that to save you having to do it in PHP
I have a table like this:
id
name
parent_id
I then want to select certain rows based on their id, so something like this:
SELECT *
FROM TABLE
WHERE id IN ('1', '5', '8', '9', '35')
I want to, from this query, also show the parent/child relationship, like:
id parent
-----------
1 0
5 1
8 0
9 8
35 9
So the final output would look something like this:
1
--5
8
--9
----35
Do I do this outside of mysql, i have tried using arrays, but can't figure it out, or
Do I do it inside MYSQL, which i don't know how to do that either.
Here is what I was able to come with which seems to be working great.
PS-Sorry about the formatting, can't figure it out :( (fixed?)
I grab my parent_id and id from MYSQL and put it into an arraly where the array keys are the id's and the values are the parents, so with in the while loop for mysql, something like this: $testarray[$id] = $parent_id;
Then I run it through the functions below, and it orders it just how I need it.
function retrieveSubTree($parent, $myarray) {
$tempArray = $myarray;
$array = array();
//now we have our top level parent, lets put its children into an array, yea!
while ($child = array_search($parent, $tempArray)) {
unset($tempArray[$child]);
//now lets get all this guys children
if (in_array($child, $tempArray)) {
$array[$child] = retrieveSubTree($child, $tempArray);
} else {
$array[$child] = true;
}
}//end while
return (!empty($array)) ? $array : false;
}
function retrieveTree($myarray) {
$array = array();
$counter = 0;
foreach ($myarray as $key => $value) {
$child = $key;
$parent = $value;
//if this child is a parent of somebody else
if (in_array($child, $myarray) && $parent != '0') {
while ($myarray[$parent] != '' && $myarray[$parent] != '0') {
$newparent = $myarray[$parent];
$parent = $newparent;
}
if (!array_key_exists($parent, $array)) {
$array[$parent] = retrieveSubTree($parent, $myarray);
}
} else {
//now make sure they don't appear as some child
if (!array_key_exists($parent, $myarray)) {
//see if it is a parent of anybody
if (in_array($child, $myarray)) {
$array[$child] = retrieveSubTree($child, $myarray);
} else {
$array[$child] = true;
}
}//end if array key
}//end initial in array
}//end foreach
return (!empty($array) ? $array : false);
}
$test = array(
'1'=>'15',
'2'=>'1',
'3'=>'1',
'4'=>'0',
'5'=>'0',
'6'=>'4',
'7'=>'6',
'8'=>'7',
'9'=>'2',
'10'=>'9'
);
print_r(retrieveTree($test));
Without changing your table structure, this requires recursion, which MySQL does not support. You'll have to do it elsewhere. You can write a recursive function in PHP to use, for example, breadth-first search to build your array. Here it looks like you are using parent_id of 0 to denote a top-level object. You can search over your results, and add to your array every object whose parent is zero, which will give you an array with 1 and 8. Then you can recurse: find all the results with a parent of 1, and add that as a subarray to 1; then find all the results with a parent of 8 and add those as a subarray of 8. Continue doing this for each level until you've run out of results.
As other posters pointed out, you can do this natively in MySQL if you can change the table structure.