Insert into mysql data - php

How will I insert into mysql using php if my data comes like this?
array(
[template] => template2
[quote] => 001
[date] => 01 December 2016
[referenceno] =>
[q_cust_name] => 0
[q_cat_name] => Array
(
[0] => drugs
[1] => oil
)
[p_name] => Array
(
[0] => Paracetamol
[1] => liquid nitrogen
)
[quot_per_unit] => Array
(
[0] => 600/gram
[1] => 500/liter
)
[quot_des] => Array
(
[0] => Cures fever. Use only in case of high fever
[1] => Hazardous Product.
)
[quot_price] => Array
(
[0] => 150.00
[1] => 7500.00
)
[quot_req_qty] => Array
(
[0] => 10
[1] => 10
)
)

$x="INSERT INTO quotation (user_id, quote, date, referenceno, q_cust_name, q_cat_name, p_name, quot_per_unit, quot_req_qty, quot_price, quot_dis, quot_des, incident_charge, tax, quote_total, status) VALUES";
for($i=0;$i<count($_REQUEST['q_cat_name']);$i++)
{ $x.="('', '".$_REQUEST['quote']."', '".$_REQUEST['date']."', '".$_REQUEST['referenceno']."', '".$_REQUEST['q_cust_name']."', '".$_REQUEST['q_cat_name'][$i]."', '".$_REQUEST['p_name'][$i]."', '".$_REQUEST['quot_per_unit'][$i]."', '".$_REQUEST['quot_req_qty'][$i]."', '".$_REQUEST['quot_price'][$i]."', '".$_REQUEST['discount']."', '".$_REQUEST['quot_des'][$i]."', '".$_REQUEST['incident_charge']."', '".$_REQUEST['tax']."', '".$_REQUEST['quote_total']."', 'draft'),";
}
echo $x;

Related

PHP Get values from nested array

I am new to PHP and Arrays, I am trying to get the values from an array. But no matter how I'm trying to do it, I can't get the value. What am I doing wrong?
The Array:
Array ( [playerinfo] => Array ( [rank] => Godfather [cash] => € 8,520,530 [weapon] => M-16 (29000) [health] => Array ( [width] => 100 [color] => green ) [wealth] => Too rich to be true [protection] => Bulletproof Humvee [plf] => Huge [plane] => Concorde [crew] => None [pbf] => Large [ship] => None ) [character] => Array ( [crime] => Array ( [0] => 120 [1] => 69 ) [gta] => Array ( [0] => 400 [1] => 70 ) [drugs] => Array ( [0] => 120 [1] => 2528 ) [airport] => Array ( [0] => 2700 [1] => 2529 ) [oc] => Array ( [0] => 86400 [1] => 1442364 ) [tr] => Array ( [0] => 10800 [1] => 1640016011 ) [plf] => Array ( [0] => 7200 [1] => 6712 ) [kill] => Array ( [0] => 3600 [1] => 1640019611 ) ) )
The way I tried to get the info:
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
foreach ($AccData as $playerinfo) {
echo $playerinfo['playerinfo']['rank'].'<br/>';
echo $playerinfo['character']['gta'].'<br/>';
}
EDIT:
The json string
{"playerinfo":{"rank":"Boss","cash":"€ 5,923,712","weapon":"M-16 (4500)","health":{"width":"100","color":"green"},"wealth":"Too rich to be true","protection":"Bulletproof Humvee","plf":"Huge","plane":"Concorde","crew":"None","pbf":"Large","ship":"None"},"character":{"crime":[120,122],"gta":[400,369],"drugs":[120,2582],"airport":[2700,2582],"oc":[86400,1640020450],"tr":[10800,1640016850],"plf":[7200,3935],"kill":[3600,1640020450]}}
Anyone knows how to do this ? For example I need the Concorde from plane in a variable and the time values from gta in a variable. And some more from this string.
So your first Part is okay and you rank you can just display like the first part as well
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['plane'].'<br/>';
echo $AccData['character']['gta'][0].'<br/>';
but the character is on the same level as playerinfo so you need to access it from AccData. also gta is an array like health, so you have to specify which value you want to show, the first so 0 or second which is 1

search and unset array in array by value in php

My array look like this :
[cart_seller] => Array
(
[3] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
)
)
[4] => Array
(
[# 801] => Array
(
)
)
)
[cart_product] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
[id] => 1705
[name] => ทดสอบสินค้า
[image] => p1534937865-VASAVAT LAB N MEDIA LOGO W.png
[price] => 1111
[option] => หมวด1เลือก1 หมวด2เลือก1
[amount] => 1
)
[# 801] => Array
(
[id] => 801
[name] => โบว์แพรแถบ ร.9 ชนมพรรษา 84 พรรษา ปีพุทธศักราช 2554
[image] => p1498062217-ส.jpg
[price] => 90
[option] =>
[amount] => 1
)
)
I want unset '# 801' in cart_seller and cart_product
in cart_product use unset($cart['cart_product'][# 801]);
but in cart_seller it in array [4] what can i do without reference value (4) ?
exapmle unset($cart['cart_seller'][xxxx][# 801]);
Just loop cart seller array til you find #801.
foreach($cart['cart_seller'] as $key => $c){
if(array_key_exists("#801",$c)){
unset($cart['cart_seller'][$key]['#801']);
}
}

PHP match values between 2 arrays not same key

I have made researches and havent fount any solutions for this yet. So final thought is come to Stackoverflow and ask the question.
I have 2 array like below:
BigArray
Array
(
[0] => Array
(
[id] => 1
[category_name] => Accountancy
[category_name_vi] => Kế toán
[category_id] => 1
)
[1] => Array
(
[id] => 2
[category_name] => Armed forces
[category_name_vi] => Quân đội
[category_id] => 2
)
[2] => Array
(
[id] => 3
[category_name] => Admin & Secretarial
[category_name_vi] => Thư ký & Hành chính
[category_id] => 3
)
[3] => Array
(
[id] => 4
[category_name] => Banking & Finance
[category_name_vi] => Tài chính & Ngân hàng
[category_id] => 4
)
)
and SmallArray:
Array
(
[0] => Array
(
[id] => 7
[category_id] => 2
[jobseeker_id] => 1
)
[1] => Array
(
[id] => 8
[category_id] => 3
[jobseeker_id] => 1
)
)
Ok, now I wanted to match each category_id from SmallArray link with respectively category_name from BigArrayand the output I only need matched values between SmallArray and BigArraywhere category_id of SmallArray is key and category_name of BigArray is value like below:
Matched array:
Array
(
[0] => Array
(
[2] => Armed forces
)
[1] => Array
(
[3] => Admin & Secretarial
)
)
So far, I have tried array_intersect, 2 foreach loops but no luck. Any advise would be very appreciated :(
Thanks
This should do that:
foreach ($smallArray as $smallKey => $smallElement) {
foreach ($bigArray as $bigKey => $bigElement) {
if ($bigElement['id'] == $smallElement['category_id']) {
$smallArray[$smallKey] = array(
$bigElement['id'] => $bigElement['category_name'],
);
break; // for performance and no extra looping
}
}
}
After these loops, you have what you want in $smallArray.

how to give grouping in input elements and insert into mysql using php?

<input type="text" id="reference_code" name="reference_code[]" required class="form-control" value="<?php echo set_value("reference_code"); ?>"/>
How to use multidimentional to insert into MySQL using PHP?
Please check with my below code
Array
(
[reference] => asdf
[company_id] => 25
[contact] => Sam Deva
[sales_manager_demand] => 1
[team_leader_demand] => 11
[demand_date] => 11-05-2015
[target_date] => 27-05-2015
[state] => 11
[city] => 328
[description] => asdfasdfsadfdsf
[countdown1] => 485
[reference_code] => Array
(
[0] => 200
[1] => 300
)
[position] => Array
(
[0] => sales manager
[1] => sales manager
)
[pri_skill_id] => Array
(
[0] => 2
[1] => 7
)
[other_primary_tech] => Array
(
[0] =>
[1] =>
)
[sec_skill_id] => Array
(
[0] => 2
[1] => 5
)
[other_secondary_tech] => Array
(
[0] =>
[1] =>
)
[minimum_experience] => Array
(
[0] => 23
[1] => 12
)
How to insert these values into MySQL using a single foreach
$reference_code=$_POST['reference_code'];
$query="insert into tablename (reference,company_id,......,other_primary_tech) values "; //write insert with all the fields
$val="";
foreach($reference_code as $key=>$val){
$ref_code=$reference_code[$key];
$position=$_POST[position][$key];
$pri_skill_id=$_POST[pri_skill_id][$key];
$other_secondary_tech=$_POST[other_secondary_tech][$key];
$minimum_experience=$_POST[minimum_experience][$key];
$val.=" (".$_POST['reference'].",........,".$minimum_experience.")"; // values to insert
}
$query.=$val; //final query
You can try like this.....

Add key and variable to multidimensional Array based on conditions

I wonder if it's possible to add a key and value to an array, based on certain conditions.
This piece of script makes an api-call to retrieve sportresults from multiple teams based on a teamID number.
$length = $numberofTeams
for ($i = 0; $i < $length; $i++) {
$teamID = $objTeamID[$i]['Teamid'];
$teamResults = 'http://api.com/teamresults/' . $Teamid;
$dataResults = file_get_contents($teamResults);
$objResults[] = json_decode($dataResults, true);
}
The result is an array with this structure:
Array (
[0] => Array (
[errorcode] => 9995
[message] => No results
)
[1] => Array (
[errorcode] => 1000
[message] => Ok, Schedule follows
[List] => Array (
[0] => Array (
[MatchID] => 7683403
[Number] => 630
[Result] => 2 - 1
[Datum] => 2013-08-27
[Tijd] => 2000
[CompType] => B )
[1] => Array (
[MatchID] => 7683403
[Number] => 630
[Result] => 4 - 0 [Datum] => 2013-08-27
[Tijd] => 2000
[CompType] => B )
)
)
[2] => Array (
[errorcode] => 9995
[message] => No results )
)
Before saving it in an MySql database, for later use I need to add the teamID-variable to every result so it would become:
Array (
[0] => Array (
[errorcode] => 9995
[message] => No results
)
[1] => Array (
[errorcode] => 1000
[message] => Ok, Schedule follows
[List] => Array (
[0] => Array (
[teamID] => 'value from $teamID'
[MatchID] => 7683403
[Number] => 630
[Result] => 2 - 1
[Datum] => 2013-08-27
[Tijd] => 2000
[CompType] => B )
[1] => Array (
[teamID] => 'value from $teamID' [MatchID] => 7683403
[Number] => 630
[Result] => 4 - 0
[Datum] => 2013-08-27
[Tijd] => 2000
[CompType] => B )
)
)
[2] => Array (
[errorcode] => 9995
[message] => No results )
)
The length of the array varies and also the number of results vary. I have no influence on the result of the api-call itself, because it's been set up by big sport association.
I'm absolutely no programmer, so I'm out of my depth her, but this is a voluntary job for an amateur sportsclub so hiring a programmer is no option.
Rgds, Bonzyx
if(isset($objResults[1]['List'])){
foreach($objResults[1]['List'] as &$listItem){
$listItem['teamID'] = $teamID;
}
unset($listItem); //good practice to unset this reference to last array element
}
You could do the same with php's array_walk() function, but since you said you're not a programmer I think the foreach method is more clear to you.

Categories