I have a table called product attribute in which I want to store products attribute name and its value one by one. For example of user selected price as attribute name and 100,200,300 as its values then I want to store price with 100, 200 and 300 with product id. I've array like this:-
[attribute_name] => Array
(
[0] => Color
[1] => weight
[2] => Brand
[3] => Price
)
[Attrvalue] => Array
(
[0] => Black
[1] => 2kg
[2] => 3kg
[3] => 4kg
[4] => Sony
[5] => Samsung
[6] => LG
[7] => 500
[8] => 1000
[9] => 2000
[10] => 9000
[11] => 9500
[12] => 10000
)
How to devide this array in key value pair like for color=>Black in php?
You can name your form inputs like this attr[attrname] eg
for single value attr
for mutiple value attr use
<select name="attr['price'][]" multiple>
<option>...</option>
</select>
This will provide you an array like
array('attr'
=>array('color'=>value),
=>array(
'price'=>array(
0=>value1,
1=>value2
)
)
);
foreach($data as $key=>$value)
{
$keys = "'".implode("',' ", $temp_tablev)."'";
$values = "'".implode("',' ", $temp_tablev)."'";
}
$sql = mysqli_query("INSERT INTO ($keys) VLAUES ($values)") or die(mysqli_error());
If you have a data structure that is handy in PHP, but mysql does not need to see the details, use json_encode and json_decode. Then put the result in a TEXT column.
Related
Variable $list stores an array like the one below.
Array
(
[0] => Array
(
[warehouse] => Array
(
[warehouseName] => Warehouse Europe
[warehouseType] => en detail
)
[products] => Array
(
[0] => Array
(
[productName] => APPAREL SHIRTS
[productCode] => 54059761696
[measuringUnit] => buc
[quantity] => 1
)
[1] => Array
(
[productName] => T-SHIRTS - SADAL
[productCode] => 54059764755
[measuringUnit] => buc
[quantity] => 0
)
........... more data ............
Using the following piece of code I print it on the display:
foreach($list as $item) {
foreach($item['products'] as $product) {
$wName = $item['warehouse']['warehouseName'];
$wType = $item['warehouse']['warehouseType'];
$pr =implode(',',$product);
printf("%s,%s,%s".PHP_EOL,$wName,$wType,$pr);
}
}
How can i search if quantity is more than 3, and if it meets the criteria than change it to ">2" before the information is passed to the browser?
Basically i need to keep the quantity confidential and only show ">2" if 3 or more products are retrieved from the array.
Thanks!
You can check whether the quantity is more than 2 and change the value you're going to display by using a ternary operator. So inside your inner foreach, before printing, you can do
$quantity = ($product['quantity'] > 2 ? '>2' : $product['quantity']);
Then you can print $quantity wherever you want it (as long as you're still inside your inner loop).
I have a form at http://97.74.37.64/ (visit it first) . It has only one textarea where visitor can fill upto 15000 number of mobile numbers. after submit it should be converted into an array (This one I have done).
Now, this array is like this below..
Array ( [0] => 9810000000 [1] => 9810000001 [2] => 9810000002 [3] => 9810000003 [4] => 9810000004 [5] => 9810000005 [6] => 9810000006 [7] => 9810000007
and so on..
Now, I want to change the key of the array to string "number" so it should be like
Array ( [number] => 9810000000 [number] => 9810000001 [number] => 9810000002 [number] => 9810000003 [number] => 9810000004 [number] => 9810000005 [number] => 9810000006 [number] => 9810000007
I want to do the above mentioned thing. Because I want to insert the mobile numbers into the MySQL table (one mobile number each row). This is the multiple insertion in MySQL table. My table name is srchlist with 2 fields id(its auto_increment & we don't need to mention or insert it) and number for which I am making array keys as number. So finally it should be inserted like below
id | number
---------------
1 9810000000
2 9810000001
3 9810000002
and so on entire array values should be inserted..
What about just organizing these values into an array of arrays? That way you can bulk insert them into your table or insert them as part of a loop if need be:
array(
array("number" => 1), array("number" => 2), array("number" => 3)
)
You can do something like this:
Controller:
// Array ( [0] => 9810000000 [1] => 9810000001 [2] => 9810000002 [3] => 9810000003 [4] => 9810000004 [5] => 9810000005 [6] => 9810000006 [7] => 9810000007)
$number = array('9810000000','9810000001','9810000002','9810000003',,'9810000004','9810000005','9810000006','9810000007');
foreach($number as $row)
{
$number1[]['number'] = $row;
}
$this->M_admin->numbers($number1); //call modal function
Modal:
function numbers($number)
{
// insert into db as batch
$this->db->insert_batch('numbers', $number);
}
I have an array of Towns that have no sorting whatsoever. I would like to sort by the [category][3] which is the province and then the [category][0] which is the region and display each Grouped Province with its regions and then towns underneath it. So the following array:
Array
(
[0] => Array
(
[name] => Name One
[id] => 1
[link] => http://mylink1.com
[category] => Array
(
[0] => Region 1
[1] => Town 7
[2] => Country
[3] => Province 2
)
)
[1] => Array
(
[name] => Name Two
[id] => 2
[link] => http://mylink2.com
[category] => Array
(
[0] => Region 1
[1] => Town
[2] => Country
[3] => Province 3
)
)
[2] => Array
(
[[name] => Name Three
[id] => 3
[link] => http://mylink3.com
[category] => Array
(
[0] => Region 1
[1] => Town 5
[2] => Country
[3] => Province 2
)
)
[6] => Array
(
[name] => Name Four
[id] => 4
[link] => http://mylink4.com
[category] => Array
(
[0] => Region 1
[1] => Town 1
[2] => Country
[3] => Province 1
)
)
)
... should end up looking like this:
Country (all the same)
Province 1
- Region 1
- - Town 1 name, id, link
Province 2
- Region 1
- - Town 5 name, id, link
- - Town 7 name, id, link
Province 3
- Region 1
- - Town 1 name, id, link
Province is the Primary Grouping factor, then sorted by Region, the Towns in no particular order but I guess Alphabetically would make sense.
I have managed to sort the array by Category using this reference: Sort Multi-dimensional Array by Value but cannot fathom how to sort any further without referencing the Province specifically in a loop by using its name. i.e.
/// shortened version..
foreach($array as $key=>$value)...
if($value == "Province 1") : do something here with these matches
... etc
Any help would be appreciated.
Take a look at the uasort() function:
http://www.php.net/manual/en/function.uasort.php
I don't think that you can do this in one step.
You can group your values like this:
$grouped = array();
foreach ($data as $group)
{
$grouped[ $group['category'][3] ][ $group['category'][0] ][ $group['category'][1] ] = array(/* ... */);
}
But you will still have to sort every array (and it's sub-arrays) using ksort().
You should check, whether you can get the values already presorted. If you, for example, are using a database to retrieve this values, it would be quite trivial to sort them in the database and bring them in the correct (grouped) structure in your PHP code.
looping through the array I've used a switch for the category I'm looking for and then built another array for each group. from there I can sort by region:
foreach($arr as $town){
switch($town['blogcats']){
case "Province 1" : $province1[] = $town;
break;
case...
etc
}
}
Then each new grouped array can be output:
foreach($province1 as $eachtown) {
echo $newtown['name'];
... and so forth.
}
Within the second loop sorting could be done on the Region.
Each Province is manually referenced in a list on my page which also gives me control over placement.
Howver, this answer could also be improved... but the above works without too much effort.
I am trying to get nested set data for an associative array. The array needs to be stored as a nested set in mysql database. Array i want to get nested set data for:
Link for nested set implementation
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
Array
(
[NEOPLASMS OF MATURE T CELLS OR NK CELLS] => Array
(
[Adult T-cell leukemia/lymphoma] => Array
(
[1] => Helper T cell
[2] => HTLV-1 provirus present in tumor cells
[3] => Adults with cutaneous lesions, marrow involvement, and hypercalcemia; occurs mainly in Japan, West Africa, and the Caribbean; aggressive
)
[Peripheral T-cell lymphoma, unspecified] => Array
(
[4] => Helper or cytotoxic T cell
[5] => No specific chromosomal abnormality
[6] => Mainly older adults; usually presents with lymphadenopathy; aggressive
)
[Anaplastic large-cell lymphoma] => Array
(
[7] => Cytotoxic T cell
[8] => Rearrangements of ALK
[9] => Children and young adults, usually with lymph node and soft-tissue disease; aggressive
)
[Extranodal NK/T-cell lymphoma] => Array
(
[10] => NK-cell (common) or cytotoxic T cell (rare)
[11] => EBV-associated; no specific chromosomal abnormality
[12] => Adults with destructive extranodal masses, most commonly sinonasal; aggressive
)
[Mycosis fungoides/Sézary syndrome] => Array
(
[13] => Helper T cell
[14] => No specific chromosomal abnormality
[15] => Adult patients with cutaneous patches, plaques, nodules, or generalized erythema; indolent
)
[Large granular lymphocytic leukemia] => Array
(
[16] => Two types: cytotoxic T cell and NK cell
[17] => No specific chromosomal abnormality
[18] => Adult patients with splenomegaly, neutropenia, and anemia, sometimes, accompanied by autoimmune disease
)
)
)
Array output i want:
Array
(
[0] => Array
(
[0] => NEOPLASMS OF MATURE T CELLS OR NK CELLS
[1] => 1
[2] => 26
)
[1] => Array
(
[0] => Adult T-cell leukemia/lymphoma
[1] => 2
[2] => 9
)
.............. and so on.
)
Here is where I am stuck at:
class nested_data
{
var $nc = 1;
var $map;
var $nest = array();
function make_nest()
{
array_walk($this->map, array($this,"fetch_nest_data"));
foreach($this->map as $map)
{
array_walk($map,array($this,"fetch_nest_data"));
}
}
function fetch_nest_data($val,$key)
{
$content = (is_array($val)) ? $key : $val;
$lft = $this->nc++;
$rgt= (is_array($val)) ? (count($val, COUNT_RECURSIVE) + $this->nc) : $this->nc++;
$this->nest[]=array($content,$lft,$rgt);
}
}
Thank you for your help !
What I've done is make a nested set manager which has methods or functions like addChild(), addSibling(), moveNodeUp(), moveNodedown(), setNodePosition(). From there you can just loop through an array, text file representation, whatever and turn them into a nested set pretty quickly. The hardest part is breaking apart all the little node movements.
I'm trying to build a multi array with this structure:
Array
(
[2] => Array //this is the user's unique ID
(
[name] => Jack
[location] => Somerville, Massachusetts, United States
[cars] => Array
(
[10] => Toyota //this is the car's unique ID
[11] => Porsche
)
)
[5] => Array
(
[name] => Luke
[location] => Schwelm, North Rhine-Westphalia, Germany
[cars] => Array
(
[1] => Honda
[2] => VW
[5] => Maserati
)
)
[1] => Array
(
[name] => Jabba
[location] => Denver, Colorado, United States
[cars] => Array
(
[3] => Tesla
)
)
)
I am using this foreach loop but am stuck in getting the cars array embedded within the search data array.
Each user may have more than one car so I would need to loop through all cars for each user, generate that array, and put it in the original foreach loop.
$search_data = array();
foreach ($query->result() as $row) {
$search_data[$row->id] = array(
'name' => $row->name,
'location' => $row->location,
'cars' => array($row->car_id), //this is where I need to insert another array
);
}
return $search_data;
Any suggestions how to do this?
Thanks for helping!
SAMPLE TABLE DATA
USER NAME LOCATION CARS
2 JACK A TOYOTA
2 JACK A PORSCHE
5 LUKE B HONDA
5 LUKE B VW
5 LUKE B MASERATI
1 JABBA C TESLA
It seems that you are creating the array through a database table. So can you please give 2 or more sample data. it'll be easier to give an answer if sample data is there.
Edit:
Well this might not be the best code but I think you'll be able to figure out a better way after seeing this
$id = $row['id'];
if (!isset($search_data[$id])){
$search_data[$id] = array();
}
$search_data[$id]['name'] = $row['name'];
$search_data[$id]['location'] = $row['location'];
if (isset($search_data[$id]['cars'])) {
array_push($search_data[$id]['cars'],$row['cars']);
}else{
$search_data[$id]['cars'] = array($row['cars']); //this is where I need to insert another array
}