i have an array.i need to get the item id check in the db and get the manufacturer.
after that I need to write every item to a HTML table where the tables are created for all manufacurers in the array.
example
Items in these 3 arrays are made by two seperate manufacturers.
Now I need help with the code to dinamicly create table for each manufacturer and add the corresponding item in the table
[0] => Array
(
[0] => 3 //item id
[1] => 1 //quantity
[2] => efg //some text
[3] => 50 //price
)
[1] => Array
(
[0] => 1
[1] => 1
[2] => bla bla bla
[3] => 10
)
[2] => Array
(
[0] => 5
[1] => 1
[2] => abe
[3] => 15
)
Sort your data in the SQL query. [best]
usort(), uasort(), uksort().
Related
Hello im trying to create a semi MVC project and im new to this so what ive done so far and what ill explain as my problem here may be completely bad on itself so sorry if that is the case.
View 1 tables.phtml
View 2 Calc.phtml
View 3 Result.phtml
Controller Tables.php
Model Tables.php
On my first view is an Form where you input 2 numbers to generate an multiplication table and it puts this in an variable called $aTempArray it looks like this:
Array (
[0] => Array ( [0] => 10 [1] => 6 [2] => 60 )
[1] => Array ( [0] => 7 [1] => 6 [2] => 42 )
[2] => Array ( [0] => 10 [1] => 6 [2] => 60 )
[3] => Array ( [0] => 7 [1] => 6 [2] => 42 )
[4] => Array ( [0] => 2 [1] => 6 [2] => 12 )
[5] => Array ( [0] => 1 [1] => 6 [2] => 6 )
[6] => Array ( [0] => 10 [1] => 6 [2] => 60 )
[7] => Array ( [0] => 6 [1] => 6 [2] => 36 )
[8] => Array ( [0] => 3 [1] => 6 [2] => 18 )
[9] => Array ( [0] => 5 [1] => 6 [2] => 30 )
)
On my second view it generates a form based on the array like this:
10 * 6 = ... <(this is a textbox)
7 * 6 = ... <(this is a textbox)
So when the user submits this second form on the second view I want it to OR send the $aTempArray, OR generate a new one like this:
Array (
[0] => Array ( [0] => 10 [1] => 6 ['answer'] => ... )
[1] => Array ( [0] => 7 [1] => 6 ['answer'] => ... )
)
However, I have no clue on how to send the $aTempArray becouse on the second submit it is empty becouse of the new controller/model that gets generated(or just reset becouse it reloads), and also I dont have a specific idea on how to get the sum data back when I submit the form on the second page. (the 10 * 6 for example becouse this is just text at this point.)
Ive tried a lot and found some things that do make this work but I want to know if there are better ways to do this.
1 is Saving the array in a session variable for example works but I want to avoid using sessions.
2 is making hidden input fields where I save the sums numbers without the answer like this: <input type="hidden" name="rebuildArray" value="10,6/7,6 etc">
And to recreate the array I do something like:
Str_split on /
Put in array
str_split on ,
Put this in arrays inside the main array.
Foreach over every item in the array
if((currentsubarray[0] * currentsubarray[1]) == answersarray[currentpos])
Answer is correct
else
answer is not correct
So can someone say if one of these ways is a semi correct way to do it or give me a push in a direction on how to so this correctly in an MVC stylish way.
And again, I just started with MVC so if this is completely wrong in general im sorry.
I have an array, I just print it as print_r($data) which looks like-
Array
(
[0] => Array
(
[0] => Title
[1] => Featured Image
[2] => Catagories
[3] => Tags
[4] => Content
)
[1] => Array
(
[0] => title 1
[1] => img1.jpg
[2] => cat 1
[3] => tag 1
[4] => post 1 content
)
[2] => Array
(
[0] => title 2
[1] => img2.jpg
[2] => cat2
[3] => tag 2
[4] => post 2 content
)
[3] => Array
(
[0] => title 3
[1] => img3.jpg
[2] => cat3
[3] => tag3
[4] => post 3 content
)
}
I have two tables-
1) sa_posts
2) sa_terms
In sa_posts table I want to store title, feature image, content and in sa_terms table I have to store categories and tags.
How is this possible using foreach or for loop?
Get array values by keys, and write insert query for below values
<?php
foreach($data as $d)
{
//sa_posts values title=$d[0], feature image=$d[1], content = $d['4']
//sa_terms values categories =$d[2], tags=$d[3]
}
?>
Foreach is your friend with arrays.
foreach($array as $element) {
//sql insert to sa_posts for $element[0], $element[1], $element[2]
//sql insert to sa_terms for $element[3], $element[4]
}
Personally, I've not seen an array whose first element describes the contents of the rest, though. If this isn't common practice, I'd consider using an associative array instead otherwise you'll be forever skipping the first element of your array and if you forget, you're going to end up with some weird data.
I have an array like:
Array
(
[0] => Array
(
[title] => Title 1
[value] => Value 1
[id] => 1428735262
)
[1] => Array
(
[title] => Title 2
[value] => Value 2
[id] => 2428735262
)
[2] => Array
(
[title] => Title 3
[value] => Value 3
[id] => 3428735262
)
)
With mustache I am able to iterate overall of them using:
{{#values}}
<h1>{{title}}</h1>
{{{value}}}
{{/values}}
But what if I only want to show specific value, for example only second element of the array (one with Title 2)?
I tried like:
{{#values}}
{{#2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/2428735262}}
{{/values}}
and:
{{#values.2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/values.2428735262}}
But none worked. Does anyone know how to get specific item from array ?
PS. I am using PHP version of mustache. Thanks
You can use this:
{{values.1.value}}
Read more at:
https://github.com/bobthecow/mustache.php/wiki/Variable-Resolution
I call again THIS function so I get some new $key is this and i want to add it to same array so I can check if it is same so unique it. Some thing like global array.
function getNodesInfo($node)
{
foreach ($result as $key => $value)
{
$items[]=$key;
}
echo ("-----."\n");
print_r($items."\n");
getNodesInfo($subNode);
}
It is my output
-------
Array
(
[0] => author
[1] => title
[2] => genre
[3] => price
[4] => publish_date
[5] => description
)
-------
Array
(
[0] => author
[1] => title
[2] => genre
[3] => price
[4] => publish_date
[5] => description
)
-------
Array
(
[0] => book
)
and I want my output looklike
Array
(
[0] => author
[1] => title
[2] => genre
[3] => price
[4] => publish_date
[5] => description
)
-------
Array
(
[0] => book
)
Use the below Algorithm for this.
Step 1 : Create a Blank array.
Step 2 : Each time when you get an array with data, Check whether that array exists in the Blank array or not.
Step 3 : If it does not exist, Add that array(with data) to Blank array. If it exists Do not add that array just discard it.
At the end you will get the unique values inside Blank array which you have created. It is a simple process
Given this array:
Array
(
[0] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[1] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[2] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
[3] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
)
The ssm_featured_post_id value corresponds to the value of the array items in the second array.
I want to order the first array items in the same order as the items in the second array
Array
(
[1] => 63
[0] => 70
[3] => 1
[2] => 49
)
so the result after sorting would be
Array
(
[0] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[1] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[2] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
[3] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
)
The simpler way would be to use usort and write a function that uses the second table to compare two values from first table.
You may want to check out array_multisort, particularly the third example given. The idea is that you create arrays based on the "columns" of the multidimensional array, then sort them simultaneously, and put the result back in the original array.