So I'm trying to fetch a points table for users to add points in by garnering their total points and adding it with the installation points, however by trying to fetch their "latest" points, new users who do not have an existing row in the table will throwback an error "InvalidArgumentException; Data Missing". This would be my code below, and are there any other ways around this?
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
$points['points_total'] = $currentpoint + $battery['point_installation'];
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first(); of your code return an object and you are try to perform math (addition) operation on that object which is not possible. You can update your code like below.
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
if($currentpoint != null)
{
$points['points_total'] = $currentpoint->points_total + $battery['point_installation'];
}
else {
$points['points_total'] = $battery['point_installation'];
}
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
I have some problem with getting results from VFP database results... The time which is taking by my php script is veeeery long - about 30 minutes, but it should be much shorter...
So there is the code which is responsible for taking database results:
$connect = odbc_connect("RIS", "", "", SQL_CUR_USE_DRIVER) or die("Error in connection ". odbc_errormsg($connect));
$array = array();
$info = array();
$from = $_GET['from'];
$to = $_GET['to'];
$rez_nap = odbc_exec($connect, "
select
pacientid as pac_pk,
datanapr,
ocered,
rtgapparat,
doctor as doc_naprav,
pk_na,
data_opis,
arst_opis as id_arst_opis
from
napravlenie
where
not arst_opis == '0000' and
not arst_opis == '' and
datanapr between {^".$from."} and {^".$to."}
") or die(odbc_errormsg());
while($row_nap = odbc_fetch_array($rez_nap))
{
echo "<pre>";
var_dump($row_nap);
$rez_opis = odbc_exec($connect, "
select
opis as opisanie,
doctor as doc_opis
from
opisanie_rtg
where
pacientid = '".$row_nap["pac_pk"]."' and
data = '".$row_nap["data_opis"]."' and
ocered = '".$row_nap["ocered"]."'
") or die(odbc_errormsg());
while($row_desc = odbc_fetch_array($rez_opis))
{
$op = explode('##$', $row_desc['opis']);
$opisanie = iconv( "cp1257", "utf-8", trim( addslashes($op[1]) ) );
$t_nr_i = substr($op[0], 12);
$ttt = explode("[", $t_nr_i);
if ( strlen($ttt[0]) > 12 )
{
$tt_nr_is = explode("-", $ttt[0]);
$t_nr_is_t = substr($tt_nr_is[0], 0, 8);
$t_nr_is_t_2 = substr($tt_nr_is[0], 8, 5);
$row_desc["nr_is"] = $t_nr_is_t."-".$t_nr_is_t_2;
}
else
{
$t_nr_is_t = substr($ttt[0], 0, 8);
$t_nr_is_t_2 = substr($ttt[0], 8, 5);
$row_desc["nr_is"] = $t_nr_is_t."-".$t_nr_is_t_2;
}
$row_desc['opis'] = $opisanie;
/////////////////////////////////////////////////////////////////////////////////////////////////////
$rez_apparat = odbc_exec($connect, "
select
kod,
gruppa,
imy,
prefiks
from
apparatura
where
kod = '".$row_nap["rtgapparat"]."'
") or die("Error in apparatura.dbf - ". odbc_errormsg($connect));
while($row_apparat = odbc_fetch_array($rez_apparat))
{
$rez_app_gruppa = odbc_exec($connect, "
select
gruppa,
prefiks,
apparati
from
apparat_gruppa
where
gruppa = '".$row_apparat["gruppa"]."'
") or die(error_logs("Error in apparat_gruppa.dbf - ".odbc_errormsg($connect)));
while($row_app_gruppa = odbc_fetch_array($rez_app_gruppa))
{
$rez_personal = odbc_exec($connect, "
select
s_name as doc_opis_name,
f_name as doc_opis_surname
from
personal
where
number = '".$row_nap["id_doc_opis"]."'
") or die(error_logs("Error in personal.dbf - ".odbc_errormsg($connect)));
while($row_personal = odbc_fetch_array($rez_personal))
{
$row_personal["doc_opis_name"] = iconv("cp1257", "utf-8", trim($row_personal["doc_opis_name"]));
$row_personal["doc_opis_surname"] = iconv("cp1257", "utf-8", trim($row_personal["doc_opis_surname"]));
$row_personal["filial"] = "r15";
$info[] = array_merge($row_nap, $row_desc, $row_apparat, $row_app_gruppa, $row_personal);
}
}
}
}
}
echo json_encode($info);
odbc_close($connect);
the database has about ~80-90k rows with data, and I'm using odbc driver to connect to VFP database, also the database is on the remote server... Any advices what I'm doing wrong? Because I can't figure what I'm doing wrong... Thanks for any advice and help!
P.S Also I need to convert that response into JSON and then save it to MySQL database...
EDIT 1
I have the second indexes:
pacientid, data_opis, ocered into the pacient table, opisanie and napravlenie
I'm the data with the following code.
$parametre = mysql_query("select * from faturaparametre where musteri='$musteri' and
urungrubu='$urungrubu' and tasimasekli='$tasimasekli' and donem<= '$tarih' and donem2>= '$tarih' and teslimnoktasi='$teslimnoktasi1' and $agirlik BETWEEN min and max");
while($parametresonuc = mysql_fetch_array($parametre)) {
$fatparametreid = $parametresonuc[id];
$ynadresno = $parametresonuc[yuklemenokta];
Skip this procedure if you do not have the following variables: the next one in the list if there is no break in the last one in the search process, how can I do this?
$adresil1 = mysql_query("select * from adresler where id='$yuklemenokta'");
$adressonuc1 = mysql_fetch_array($adresil1);
$yuklemeadresno = $adressonuc1[adresno];
$yuklemeil = $adressonuc1[noktail];
$yuklemeilce = $adressonuc1[noktailce];
$yuklemeilb = "il".$yuklemeil;
This code does not run more than one priority finding. and do not find exactly the same thing
if(eregi($ynadresno, "$yuklemeadresno")) {
$ftid = $fatparametreid;
}elseif(eregi($ynadresno, "$yuklemeilce")) {
$ftid = $fatparametreid;
}elseif(eregi($ynadresno, "$yuklemeilb")) {
$ftid = $fatparametreid;
}else {
}
The array keys should be quoted,
$yuklemeadresno = $adressonuc1['adresno'];
$yuklemeil = $adressonuc1['noktail'];
$yuklemeilce = $adressonuc1['noktailce'];
$yuklemeilb = "il".$yuklemeil;
I have an array containing several variables from my sql database.
{"gold":"0","silver":"0","bronze":"0","gdp":"12959563902","population":"3205000","country_name":"Albania"}, {"gold":"1","silver":"0","bronze":"0","gdp":"188681000000","population":"35468000","country_name":"Algeria"}
I have an additional variable called $score that uses information from the database to calculate this score. I want to know how I can loop through and add the correct score to each country in the array.
My Original Code:
$row = $res->fetchRow();
$resGold = $row['gold'];
$resSilver = $row['silver'];
$resBronze = $row['bronze'];
$resGdp = $row['gdp'];
$resPopulation = $row['population'];
$resCountry = $row['country_name'];
$gold_score = ($resGold * $gold_value);
$silver_score = ($resSilver * $silver_value);
$bronze_score = ($resBronze * $bronze_value);
if($population == true){
$score = (($gold_score + $silver_score + $bronze_score)/$resPopulation);
}
else if($gdp == true){
$score = (($gold_score + $silver_score + $bronze_score)/$resGdp);
}
$result = $res->fetchAll();
$result[] = array('score' => $score);
echo json_encode($result);
Your code will be something like this:
$json_data = '{"gold":"0","silver":"0","bronze":"0","gdp":"12959563902","population":"3205000","country_name":"Albania"},
{"gold":"1","silver":"0","bronze":"0","gdp":"188681000000","population":"35468000","country_name":"Algeria"}';
$countries_info_new = array();
$countries_info = json_decode($json_data);
foreach($countries_info as $country_info){
$country_info['score'] = Get_country_score($country_info['country_name']);
$countries_info_new[]=$country_info;
}
$new_json_data = json_encode($countries_info_new);
I have a PHP MySQL fetch while loop as shown below in my script:
$result2211 = mysql_query("select * from products where is_config = 'yes' ");
while($row2211 = mysql_fetch_assoc($result2211))
{
$sn = $row2211['sn'];
$allparrentselectq = mysql_query("SELECT * FROM parrentpro where parrentsn = $sn");
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
$childarr = unserialize($allparrentselect['childsn']);
$subpro = '{"catname":"'.$allparrentselect['childname'].'",';
$i = 0;
foreach($childarr as $childarr):
$subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
endforeach;
$subpro1[] = substr($subpro, 0, -1)."}";
$subproa = "[".implode(",",$subpro1)."]";
}
$prodObj2 = new ProductDetails();
$prodObj2->productname = $row2211['productname'];
$prodObj2->price = $row2211['productprice'];
$prodObj2->discount = $row2211['discount'];
$prodObj2->discountprice = $row2211['discountprice'];
$prodObj2->imageURL = $row2211['productimageurl'];
$prodObj2->category = $row2211['productcat'];
$prodObj2->configurablepone = $subproa;
$prodObj2->configurable = 'yes';
array_push($totArr, $prodObj2);
}
In that I have a problem. I get the result as like below (JSON):
[
{
"productname":"Veg.Pizaa",
"price":"350",
"discount":"",
"discountprice":"350",
"imageURL":"http:\/\/farm8.staticflickr.com\/7154\/6694188161_9ee692d854_s.jpg",
"category":"Pizaa",
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}],
"configurable":"yes"
},
{
"productname":"Core i7 Pc",
"price":"48000",
"discount":"2",
"discountprice":"47040",
"imageURL":"http:\/\/www.4to40.com\/images\/science\/Basic_Computer_Parts\/Computer.jpg",
"category":"Pc",
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},{"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"},{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}],
"configurable":"yes"
}
]
But I need the result as like below:
[
{
"productname":"Veg.Pizaa",
"price":"350",
"discount":"",
"discountprice":"350",
"imageURL":"http:\/\/farm8.staticflickr.com\/7154\/6694188161_9ee692d854_s.jpg",
"category":"Pizaa",
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}],
"configurable":"yes"
},
{
"productname":"Core i7 Pc",
"price":"48000",
"discount":"2",
"discountprice":"47040",
"imageURL":"http:\/\/www.4to40.com\/images\/science\/Basic_Computer_Parts\/Computer.jpg",
"category":"Pc",
"configurablepone":[{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}],
"configurable":"yes"
}
]
As you can see configurablepone JSON is getting repeated for every loop so I am getting the value of the first product in second product also but I need to seperate like below:
First Product As Like Below
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},{"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}]
Second Product As Like Below
"configurablepone":[{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}]
I have tried changing the loop but I haven't found any solutions. Kindly help me to solve this.
I think, your problem in line $subpro1[] = substr($subpro, 0, -1)."}";.
So, first call of this line save data from "Veg.Pizaa" into $subpro1[0].
Second call of this line save data from "Core i7 Pc" into $subpro1[1].
Then, line $subproa = "[".implode(",",$subpro1)."]"; merged all array elements.
Just Use unset($subpro1); after the array_push($totArr, $prodObj2);.
Below Is An example Source
Try This One This May Work
$result2211 = mysql_query("select * from products where is_config = 'yes' ");
while($row2211 = mysql_fetch_assoc($result2211))
{
$sn = $row2211['sn'];
$allparrentselectq = mysql_query("SELECT * FROM parrentpro where parrentsn = $sn");
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
$childarr = unserialize($allparrentselect['childsn']);
$subpro = '{"catname":"'.$allparrentselect['childname'].'",';
$i = 0;
foreach($childarr as $childarr):
$subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
endforeach;
$subpro1[] = substr($subpro, 0, -1)."}";
$subproa = "[".implode(",",$subpro1)."]";
}
$prodObj2 = new ProductDetails();
$prodObj2->productname = $row2211['productname'];
$prodObj2->price = $row2211['productprice'];
$prodObj2->discount = $row2211['discount'];
$prodObj2->discountprice = $row2211['discountprice'];
$prodObj2->imageURL = $row2211['productimageurl'];
$prodObj2->category = $row2211['productcat'];
$prodObj2->configurablepone = $subproa;
$prodObj2->configurable = 'yes';
array_push($totArr, $prodObj2);
unset($subpro1);
}
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
$childarr = unserialize($allparrentselect['childsn']);
$subpro = '{"catname":"'.$allparrentselect['childname'].'",';
$i = 0;
foreach($childarr as $childarr):
$subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
endforeach;
$subpro1[] = substr($subpro, 0, -1)."}";
$subproa = "[".implode(",",$subpro1)."]";
$subpro1 = array();
}
try this in second while loop