When I try to get data from multiple collections code it is giving me data only from the first collection
i.e collections are project_0, project_1, project_2, project_3
for($i = 0; $i <= 3; $i++){
$dm->getClassMetadata('\Application\Document\Product')->setCollection('product_'. $i);
$record = $dm->getRepository('\Application\Document\Product')->findOneBy($condition);
print_r($record);
}
I tried to clear flush but noting is working. Please let me know the right way to do it?
Executing the code above will save the information only from the last collection in $record as you are overwriting the data in it with every iteration.
To fix it, you can create an array, let's say $records = array(); and then in each iteration you can do something like this:
array_push($records, $dm->getRepository('\Application\Document\Product')->findOneBy($condition));
After you're done, you'll have all the data in $records. I hope that helps.
Related
For a school assignment I'm trying to split stockitems with product details like colour and size in the title into groups of stockitems with different variants. I've got as far as having them all split, but I just can't figure out how to add this information to the $stockItem array. ($stockItem is inside the array $stockItemGroup which is inside the array $stockItemGroups).
When I try to add information to the array inside the loop, I cannot access that information outside the loop. If I use print_r on the entire array after this loop has completed the new information is not displayed.
for($i = 0; $i < count($stockItemGroup); $i++){
$stockItem = $stockItemGroup[$i];
$restString = str_replace($similarString, "", $stockItem['StockItemName']);
$colour = getColour($restString, $allColours);
$restVariant = getRestVariant($restString, $allColours);
$stockItemGroup[$i]['Colour'] = $colour;
$stockItemGroup[$i]['RestVariant'] = $restVariant;
$stockItemGroup[$i]['NewItemName'] = createNewItemName($colour, $restVariant, $stockItem['StockItemName']);
}
I have tried both in a foreach and a for loop (I read that a foreach does some copying, so I thought that might cause it). but to no avail.
I have also obviously tried
$stockItem['Colour'] = $colour;
$stockItem['RestVariant'] = $restVariant;
$stockItem['NewItemName'] = createNewItemName($colour,
$restVariant,
$stockItem['StockItemName']);
But that did not change anything either.
I am a total Php noob, so it might be very obvious, any help would be appreciated.
EDIT:
this loop is inside a method which is called in this loop:
$stockItemGroups = getStockItemGroups();
foreach ($stockItemGroups as $stockItemGroup){
addVariants($stockItemGroup);
//writeNewGroup($stockItemGroup);
}
foreach ($stockItemGroups as &$stockItemGroup){ Pass the array as a reference – RiggsFolly
I'd like to fill an array with data from my database. I created a for loop for that but I think I'm doing something wrong (I'm still very new to programming so sorry for that!)
for($i=0; $i<count($uploadprofile->getAgencies()); $i++){
foreach($uploadprofile->getAgencies() as $agency) {
$users[$i] = $agency->getAgencyUser();
}
}
dump(count($users));
The dump statement only counts 1 user even though there are supposed to be 3 users in there.
I need this array for then using the data in an other for loop afterwards:
for($i=0; $i<count($users); $i++){
foreach($users as $user){
$manager->addNotification($user->toArray()[$i], $notif);
}
}
I am sure this is really bad coding. It looks like way too many lines for something that simple. So I would be really glad about any advice rather than just a "downvoting"!
If more information about the entities are needed, I'd be happy to provide them!
Unless I'm missing something, you have too many loops, and I would stick with foreach. To build the $users array:
foreach($uploadprofile->getAgencies() as $agency){
$users[] = $agency->getAgencyUser();
}
To use it:
foreach($users as $user){
$manager->addNotification($user->toArray(), $notif);
}
But, if you won't actually need the $users array after this then just combine:
foreach($uploadprofile->getAgencies() as $agency){
$user = $agency->getAgencyUser();
$manager->addNotification($user->toArray(), $notif);
}
I am trying to create a multidimensional array at run-time that queries and stores several MySQL results. For instance, let us say we have a table like so (is fictional, so please don't pick at the example):
**store--tag--color--size**
1--101--blue--s
2--102--red--s
2--103--yellow -- m
3--104--blue--m
The need is to create an multi-d array that will store all the products per store. The result I would like is:
$storeArray = array[[[101],[blue],[s]],[[102,103],[red,yellow],[s,m]],[[104],[blue],[m]]];
here is the code I have:
$counter = 0;
foreach($storeIDs as $item){
$result = mysql_query('select whatever');
$rows = mysql_num_rows($result);
for($i=0;$i<$rows;$i++){
$tag[$i] = mysql_result();
$color[$i] = mysql_result();
$size[$i] = mysql_result();
}
$tempArray = array($tag,$,$color,$size);
$storeArray[$counter] = $tempArray;
unset($tempArray);
$counter++;
}
The problem is, even though I have unset $tempArray, the third loop which should capture just
[[104],[blue],[m]]
actually stores
[[104,103],[blue,yellow],[m,m]].
I've tried setting $tempArray to array(), or array(array()). The data from the second loop always spills over into ANY future iteration that is of smaller size.
How can I get $storeArray to look like the goal above?
Thank you
You're unsetting $tmpArray, but it looks like you may need to also unset $tag, $color, and $size as well. There are better approaches with a slightly different data structure, but as is, have you tried this?
unset($tempArray,$tag,$color,$size);
Effectively, you might set $tag[0] and $tag[1] in a loop that has two items. Then, for a loop that has one item you update $tag[0], but $tag[1] remains set.
Let me know if you want more details but:
I have an different number of inserts I need to make based on a POST form data that I created in a loop.
If I were to write it all out it would look like this:
$Scout1=$_POST['ScoutID1'];
$Scout2=$_POST['ScoutID2'];
and it keeps going until it reaches "x" I do have that number stored as
$ScoutCount
(so if the above code would post all the variables I brought over {$ScoutCount=2}
I can't find a way to do:
while (X>0){
$ScoutX=$_POST['ScoutIDX'];
X--;
}
how can I do this?
You might be looking for variable variables
But rather, I would recommend storing the data in an array, as opposed to individual variables. Then in a for loop, it could look like:
$scouts = array();
for ($i = 0; $i < 10; $i++)
{
$scouts[$i] = $_POST['ScoutID' . $i];
}
or something.
instead of having form fields called ScoutID1, ScoutID2 wtc name them
name="ScoutID[]"
then you will have a nice array with work with
//put scoutIDs into Array
$scouts = array();
for ($i = 1; $i <= $ScoutCount; $i++)
{
$scouts[$i] = $_POST['ScoutID' . $i];
}
Thanks - that may have seemed easy but I wasted a day trying to figure it out. thanks from the newbie to Php....
I am having the worst time trying to get this to work. In the following code, I am gathering data from a database query and trying to build a muti-dimensional array object that will keep totals and tally up some information in a specific way. The problem is that instead of getting a value that is incrementing as it should, the value seems to be suffering from the last value it was assigned problem. Here is the code:
$REVIEWS = array();
$USER_REVIEWS = array();
$USER_IMGREVS = array();
pseudo-code: loop here which iterates over the DB results creating
$date - which is into this function as its called for each day of month
$p1user - which is one of the users (there are 3) 'levels' of users
$hr - is the hour which is built from the transaction's timestamp
$hr = date('H', $row['P1TIMESTAMP']);
$p1user = $row['P1USER'];
$REVIEWS[$date] += 1;
$USER_REVIEWS[$date][$p1user][$hr] += 1;
$USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
print "PASS1<br/>\n";
print "Value of Total Reviews: [".$REVIEWS[$date]."]<br/>\n";
print "Value of User Reviews: [".$USER_REVIEWS[$date][$p1user][$hr]."]<br/>\n";
print "Value of Reviewed Images: [".$USER_IMGREVS[$date][$p1user][$hr]."]<br/>\n";
print "<br/><br/>\n";
So - the 'total reviews' increments by one, as it should, for each time i print this. SO far so good. The next two arrays will only print the last values they were assigned, and will not be added together like they should. Why not? I have attempted to do this another way by literally creating the arrays one by one and assigning them in whole to the array containing them - but that also does not seem to work. Any insights?
i don't know how you initilize your array, maybe this will help:
// replace this 2 lines:
$USER_REVIEWS[$date][$p1user][$hr] += 1;
$USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
// with this code:
if (!isset($USER_REVIEWS[$date]))
$USER_REVIEWS[$date] = array();
if (!isset($USER_REVIEWS[$date][$p1user]))
$USER_REVIEWS[$date][$p1user] = array();
if (!isset($USER_REVIEWS[$date][$p1user][$hr]))
$USER_REVIEWS[$date][$p1user][$hr] = 0;
$USER_REVIEWS[$date][$p1user][$hr] += 1;
if (!isset($USER_IMGREVS[$date]))
$USER_IMGREVS[$date] = array();
if (!isset($USER_IMGREVS[$date][$p1user]))
$USER_IMGREVS[$date][$p1user] = array();
if (!isset($USER_IMGREVS[$date][$p1user][$hr]))
$USER_IMGREVS[$date][$p1user][$hr] = 0;
$USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
Sir, I dont understand very well why your coed is not working, but in my first test, I would change these lines:
$count = 1;
$USER_REVIEWS[$count][$p1user][$hr] += 1;
$USER_IMGREVS[$count][$p1user][$hr] += $row['F5'];
$count++;
Please, check if this code helps you anyway.
Your print statements for those values rely on the value of $p1user:
print "Value of User Reviews: [".$USER_REVIEWS[$date][$p1user][$hr]."]<br/>\n";
print "Value of Reviewed Images: [".$USER_IMGREVS[$date][$p1user][$hr]."]<br/>\n";
If you want to print it for all users you should loop over all possible users rather than just using $p1user. Either that or add them up if you want their sum.
Edit: Something that was bugging me was your data structure. It doesn't seem to represent your data very well. In your loop why don't you build up useful information that you store at the base of the review array?