I am not able to loop through the same query twice as follow, what am I missing?
$query = "SELECT ..."
while ($a = tep_db_fetch_array($query)) {
while ($a2 = tep_db_fetch_array($query)) {
// stuff
}
}
Thus my question, how to loop through the same query two times one inside the other?
If you know the data is not going to be excessively huge, then you can use get_records_sql() instead. This will return an array, indexed by the first field in the SELECT. You can then do what you want with this array (loop through it multiple times, split, pop, shift, etc)
Related
I have two arrays. One array $People currently creates number of 44 individuals. Lets just assume currently its
$People = array('1','2',...,'44');.
I have another array of 15 elements.
$Test = array('A','B',...'O');
Now I want to be able to assign the test randomly to each individual. I know how to do this using random function in php.
Where it has got tricky for me and what I need help with is how can I even out the test array. What I mean by this is since there are currently 44 individuals (this array will grow in future), what I want is 14 test versions to have 3 individuals and 1 version would have 2. So I want the test array to even out. I also want it to handle as growth of $People array.
Ex: Test Version D will have individual '4', '25'. Every other version has three random individuals.
Few ideas I came up with are things like running random integer on $Test array and which ever gets highest/lowest gets 2 individuals and rest three. This would give a problem when I increase the size of $People array; to deal with that I though about using modulus to figure out what will be even number of $Test beforehand.
I can do this and other ideas but am pretty sure there has to be a better way to do this.
Clarifying your situation:
You want to distribute the values inside $People randomly amongst your $Test array. The problem you stated you are having is that the amount of values in $People isn't always perfectly dividable by the amount of values in $Test, and you aren't sure how to go about implementing code to distribute the values evenly.
Proposed solution:
You could obtain the values in a foreach loop randomly 1 by 1 from a shuffled version of $People and put them in a new array called $Result. You would also have a conditional checking if you have extracted all the values from the shuffled $People array: if($count>=$arrayCount) where $arrayCount=$count($shuffledPeople);. If you have obtained all the values, you first make the $bool value false (in order not to iterate through the while loop anymore, and then you break; out of the foreach loop.
$Result =[];//the array containing the results
$shuffledPeople = $People;
shuffle($shuffledPeople);//mixing up the array
$arrayCount = count($shuffledPeople);//finding the total amount of people
$count = 0;
$bool = TRUE;
while ($bool)
{
foreach($Test as $value)
{
$Result[$value][] = $shuffledPeople[$count];
$count++;
if ($count>=$arrayCount)
{
$bool = FALSE;
break;
}
}
}
To view the results, all you would need to do is:
foreach ($Result as $key => $value)
{
echo "{$key}: <br>";
if (is_array($value))
{
foreach ($value as $something)
{
echo "-->{$something}<br>";
}
}
else
{
echo "-->{$value}<br>";
}
}
I believe that this is what you want to do...
Assume that you have $people and $test arrays. You want to know how many people per test...
$ppt = ceil(sizeof($people)/sizeof($test));
Now, $ppt is the people per test. The next step is to shuffle up the people so they are randomly assigned to the tests.
shuffle($people);
Now, we can chunk up the people into sub-arrays such that each sub-array is assigned to a test. Remember, the chunks are random now because we shuffled.
$chunks = array_chunk($people, $ppt);
Now, everyone in $chunks[0] will take $test[0]. Everyone in $chunks[1] will take $test[1]. Everyone in $chunks[2] will take $test[2].
i'm making a grocerylist system that takes into account amount of recipies you throw at it.
based on that, it combines values and outputs a grocerylist for you.
i'm looping a mysql query and create arrays like this:
foreach sql loop{
$ingredients = array(
"id"=>array($id),
"name"=>array($name),
"amount"=>array($amount),
"unit"=>array($unit)
);
foreach ($ingredienser as $key => $value) {
foreach ($value as $index => $v) {
echo $key."=";
echo $v;
echo "<br><hr>";
}
}
}
That whole ordeal outputs a list like this if i throw 1 recipe at it:
id=150
name=Cherrytomater
amount=300
unit=Gram
id=151
name=Kyllingfilet
amount=4
unit=Stykk
if i throw 2 recipies it throws the next recipe under it.
However, i need it to do a certain set of things.
merge values if the name is similar
ignore the above merge if unit is different (its a shopping list after all)
output a combined list of ingredients based on the recipies and amount of recipies i throw at it.
To further explain; the sql equivalent of what im trying to achieve is :
$dupe = "2" //amount of similar recipies
SELECT i.id, i.ingred, r.enhet, r.id, SUM(r.mengde * $dupe) total, r.enhet FROM oppskriftingred r left join ingrediens i on i.id = r.ingrediens WHERE r.oppskriftid IN $vars2 GROUP BY i.ingred, r.enhet ORDER BY i.ingred
However, the sql approach wont work as the SUM doesn't differentiate between different recipies.. if $ dupe is 4 for one recipe and 2 for another, its 4 for both.
So how can i do this in php with arrays? Any help is greatly appreciated!
Your "this is what I need list" has one problematic point:
1. merge value if the name is SIMILAR
The rest is quite simple. You have an array of ingerdients $ingredients. You need to loop through this and compare every entry to a result array, to figure out if your points 2 and 1 match it to a existing or new entry. I would give the new array a key, that makes matching easy. Let's say the unit and the name as a string: 'Gram-Cherrytomater'
$result = array();
foreach ($ingerdients as $item) {
$newKey = $item['unit'].'-'.$item['name'];
if (isset($result[$newKey]) {
// add amount to existing item
$result[$newKey]['amount'] += $item['amount'];
} else {
// add new item
$result[$newKey] = $item;
}
}
// now loop trough the result and display whatever you want
The problem is with the SIMILAR thing. I do not know, how you want to compare to strings that are similar. You should do this when getting the $newKey. Do something with $item['name'] to make it "similar".
You could use some php function like similar_text(), levenshtein() or soundex(). I do not know how good this works for your language (Danish?).
I have the following array:
$masterlist=[$companies][$fieldsofcompany][0][$number]
The third dimension only exists if the field selected from $fieldsofcompany = position 2 which contains the numbers array. Other positions contain regular variables. The 3rd dimension is always 0 (the numbers array) or Null. Position 4 contains numbers.
I want to cycle through all companies and remove from the $masterlist all companies which contain duplicate numbers.
My current implementation is this code:
for($i=0;$i<count($masterlist);$i++)
{
if($masterlist[$i][2][0][0] != null)
$id = $masterlist[$i][0];
for($j=0;$j<count($masterlist[$i][2][0]);$j++)
{
$number = $masterlist[$i][2][0][$j];
$query = "INSERT INTO numbers VALUES('$id','$number')";
mysql_query($query);
}
}
Which inserts numbers and associated IDs into a table. I then select unique numbers like so:
SELECT ID,number
FROM numbers
GROUP BY number
HAVING (COUNT(number)=1)
This strikes me as incredibly brain-dead. My question is what is the best way to do this? I'm not looking for code per se, but approaches to the problem. For those of you who have read this far, thank you.
For starters, you should prune the data before sticking it into the database.
Keep a look up table that keeps track of the 'number'.
If the number is not in the look up table then use it and mark it, otherwise if its in the look up table you can ignore it.
Using an array for the look up table and with keys being the 'number' you can use the isset function to test if the number has appeared before or not.
Example pseudo code:
if(!isset($lookupTable[$number])){
$lookupTable[$number]=1;
//...Insert into database...
}
Now that I think I understand what you really want, you might want to stick with your two-pass approach but skip the MySQL detour.
In the first pass, gather numbers and duplicate companies:
$duplicate_companies = array();
$number_map = array();
foreach ($masterlist as $index => $company)
{
if ($company[2][0][0] === null)
continue;
foreach ($company[2][0] as $number)
{
if (!isset($number_map[$number])
{
// We have not seen this number before, associate it
// with the first company index.
$number_map[$number] = $index;
}
else
{
// Both the current company and the one with the index stored
// in $number_map[$number] are duplicates.
$duplicate_companies[] = $index;
$duplicate_companies[] = $number_map[$number];
}
}
}
In the second pass, remove the duplicates we have found from the master list:
foreach (array_unique($duplicate_companies) as $index)
{
unset($masterlist[$index]);
}
i have two arrays i.e$ar1=array("Mobile","shop","software","hardware");and$arr2=arry("shop","Mobile","shop","software","shop")
i want to compare the elements of arr2 to arr1 i.e
foreach($arr2 as $val)
{
if(in_array($val, $arr1))
{
//mycode to insert data into mysql table
variable++; // here there should be a variable that must be increamented when ever match found in $arr2 so that it can be saved into another table.
}
$query="update table set shop='$variable',Mobile='$variable'.......";
}
the $variable should be an integer value so that for later use i can use this variable(shop i.e in this example its value should be 3) to find the match.
My question is how can i get the variable that will increamented each time match found.
Sorry, I don't fully understand the purpose of your code. You can use array_intersect to get common values and array_diff to get the unique values when comparing two arrays.
i want to compare the elements of arr2 to arr1 i.e
Then you are essentially doing the same search for shop three times. It's inefficient. Why not sort and eliminate duplicates first?
Other issues. You are comparing arr2 values with the ones in arr1, which means the number of repetation for "shop" will not be 3 it will be one. Doing the opposite might give you the number of repetation of arr1[1] in arr2 =3.
There are multitude of ways to solve this problem. If efficiency is required,you might wish to sort so you don't have to go beyond a certain point (say s). You can learn to use indexes. Infact the whole datastructure is revolved around these kinds of things - from quick and dirty to efficient.
Not sure I understand the connection between your two arrays. But you can use this to count how many items are in your second array:
$items = array("shop","Mobile","shop","software","shop");
$count = array();
foreach($items as $item)
{
if(isset($count[$item]))
{
$count[$item]++;
}
else
{
$count[$item] = 1;
}
}
print_r($count); // Array ( [shop] => 3 [Mobile] => 1 [software] => 1 )
So I've created a form wich, by the post function, send me several values.
$_POST[groups] sends me, using 4 different checkboxes values 1, 2, 4 and 8.
$_POST[retailgroup] does the same, but only the values 1, 2 and 4.
The problem is that I want these to add up together and then send the sum of the values to a MySQL table.
I've managed to do one using the following foreach loop:
foreach ( $_POST as $val ) {
foreach ( $val as $key=>$final_val ) {
$group = $group+$final_val;
}
}
The problem starts when I need to have these two apart from each other since they are going into separate columns in the table.
To be clear I've assigned different groups different values (always taking the prev value times two, just like counting binary) like 1, 2, 4 and 8. By adding them together I can determine membership in the groups by doing a subtraction "backwards". Since there are different kinds of groups I want them into two separate fields of the table.
I hope everything is clear.
Any idea on how to do this?
you want to use:
$groups_sum = array_sum($_POST['groups']);
$rgroups_sum = array_sum($_POST['retailgroups']);
This will be faster and clearer than a foreach loop in this case.
try
foreach ( $_POST[groups] as $key=>$final_val ) {
//do stuff for first group
}
foreach ( $_POST[retailgroup] as $key=>$final_val ) {
//do stuff for second group
}
and after you can make whatever you want with the variables got from loops
$groups = array_sum($_POST['groups']);
$retailGroups = array_sum($_POST['retailgroup']);
You can check membership like this instead of subtracting backwards:
$groupAdmin = 4;
if($groups & $groupAdmin) {
// is admin
}