I have a query which fetches one table and then the results of that query gets passed though another query.
Then I want to return all the values of that column in each row from that query.
This fetches all of the Product id's with a particular brand ID
// Fetch the Product List
$brandID = 1;
$prodList = Products::whereBrandId($brandID);
// Fetch the catalog ID of the Product
$fetchID = $prodList->lists('id');
Then print_r($fetchID) returns the array.
Array ( [0] => 10011 [1] => 10012 [2] => 10013 [3] => 10014 [4] => 10015 [5] => 10016 [6] => 10017 [7] => 10018 [8] => 10019 [9] => 10020 [10] => 10021 [11] => 10022 [12] => 10023 [13] => 10024 [14] => 10025 [15] => 10026 [16] => 10027 [17] => 10028 [18] => 10029 [19] => 10030 [20] => 10031 [21] => 10032 [22] => 10033 [23] => 10034 [24] => 10035 [25] => 10036 [26] => 10037 [27] => 10038 [28] => 10039 [29] => 10040 [30] => 10041 [31] => 10042 [32] => 10043 [33] => 10044 [34] => 10045 [35] => 10046 [36] => 10047 [37] => 10048 [38] => 10049 [39] => 10050 [40] => 10051 [41] => 10052 [42] => 10053 [43] => 10054 [44] => 10055 [45] => 10056 [46] => 10057 [47] => 10058 [48] => 10059 [49] => 10060 [50] => 10061 [51] => 10062 [52] => 10063 [53] => 10064 [54] => 10065 [55] => 10066 [56] => 10067 [57] => 10068 [58] => 10069 [59] => 10070 [60] => 10071 [61] => 10072 [62] => 10073 [63] => 10074 [64] => 10075 [65] => 10076 [66] => 10077 [67] => 10078 [68] => 10079 [69] => 10080 [70] => 10092 [71] => 10093 [72] => 10128 )
I then have a table with the fields product_id and category_id, so I want to pass the results of $fetchID though and return all values from the category_id using lists()
// Fetch the category_id where is a product_id
$catRelation = Db::table('purple_catalog_prods_cats')->whereProductId($fetchID);
$catRelList = $catRelation->lists('category_id');
This returns as empty in print_r
Finally I want to query the categories table which has id and name, and return everything. So I try to pass the $catRelList through. This doesn't work because in the previous query it returns as empty.
// Fetch the Cat list
$catList = categoryName::whereId($catRelList)->orderBy('id', 'asc');
$this->categoryName = $catList->get();
So my issue is passing though $fetchID to return all rows matching the multiple Product ID's. As when I manually enter a product ID it returns that category fine. Query Below
$catRelation = Db::table('purple_catalog_prods_cats')->whereProductId('10011');
Now where the 10011 is, I somehow want to pass multiple values through, like in the $fetchID array.
Is this possible to do and is there a better method of doing this?
Change your $catRelation query to using whereIn:
$catRelation = Db::table('purple_catalog_prods_cats')->whereIn('product_id', $fetchID);
This should now correctly using your array of ProductIDs and find all matching rows. (Change column name as required).
Related
I have two arrays: $ORGvarvals (ORGvarvals contains the values which needs to be replaced in String) & $varvals (varvals contains the values which needs to be replaced with ORGvarvals in string)
$str (Contains the content).
This is my $ORGvarval:
Array ( [0] => 56,231 [1] => abc [2] => 172.0.0.1 [3] => JMeter [4] => http://172.0.0.1/file/path/ [5] => ${__time(yyyy/MM/dd HH:mm:ss,)} [6] => API [7] => Unique [8] => /home/path/to/file/filename_${__time(yyyy-MM-dd_HH-mm-ss,)}.xls [9] => C:\xls\file\path\results${__time(ddMMyyyyHHmmss,)}.csv [10] => 172.0.0.1 [11] => 9999 [12] => 22 [13] => username [14] => password [15] => /tomcat/path [16] => C:\path\to\testdata [17] => jdbc:oracle:thin:db:details [18] => username1 [19] => password1 [20] => /USSD/API/URL?parameter [21] => /EXTGW/API/URL?parameter [22] => /USSD/API/URL?parameter2 [23] => /EXTGW/API/URL?parameter [24] => /EXTGW/API/URL?parameter5 [25] => /USSD/API/URL?parameter6 [26] => 310 [27] => 1234567890 [28] => 2468 [29] => 1357 [30] => 9876543210 [31] => 100 [32] => CC [33] => 500 [34] => 50 [35] => 100 [36] => 100 [37] => /path/to/logs [38] => 1237896540 [39] => 1357 [40] => abc#7891 [41] => 0 [42] => 0 [43] => 101 [44] => dd/MM/yy [45] => ADMIN [46] => abc#7891 [47] => 1357 [48] => 3 [49] => 10 [50] => jmeter_logs )
I am taking user inputs through form and all the values are saved in another variable $varvals in same format.
Now I want to find the $ORGvarvals values one by one in $str and replace them with $varvals.
I tried the following two codes:
$str=str_replace($ORGvarvals, $varvals, $str);
$str=strtr($str, array_combine($ORGvarvals, $varvals));
Both of them are not working as expected. They are replacing multiple occurrences in string.
I want to find all the ORGvarvals values one by one and replace them without messing up the already replaced values in case of duplicate values
I have the following array
Array ( [0] => **start** [1] => **start** [2] => name [3] => producer [4] => contact name [5] => 03354222271 [6] => fzahid001#gmail.com [7] => day contact name [8] => 03354222271 [9] => venue name [10] => adress [11] => country [12] => city [13] => desc [14] => file [15] => 2016-01-01 [16] => 01:00 [17] => 06:00 [18] => 2 [19] => music_festival [20] => 2000+ [21] => quick [22] => alcohol [23] => quick [24] => 10x10 [25] => ***no*** [26] => ***no*** [27] => ***no*** [28] => 2 [29] => 0 [30] => 4 [31] => $158 [32] => $118.5 [33] => $284 [34] => $960 [35] => **start** [36] => na [37] => producer [38] => con [39] => 1 [40] => fzahid001#gmail.com [41] => nam [42] => 1 [43] => venue [44] => ad [45] => co [46] => ci [47] => description test [48] => download555ssss.png [49] => 2016-12-07 [50] => 13:00 [51] => 19:00 [52] => 2 [53] => manual_selection [54] => ATV [55] => 10x10 [56] => no [57] => no [58] => no [59] => 1 [60] => 1 [61] => 1 [62] => $109.5 [63] => $118.5 [64] => $99.5 [65] => $728 [66] => **start** [67] => Race [68] => Race Club [69] => Faizan [70] => 03354222271 [71] => fzahid001#gmail.com [72] => Faizan Zahid [73] => 03354222271 [74] => DHA [75] => 90-H Tariq Gardens [76] => Pakistan [77] => Lahore [78] => [79] => images.jpg [80] => 2017-01-01 [81] => 00:00 [82] => 07:00 [83] => 2 [84] => cycling_road_race [85] => 1_field [86] => 1_399 [87] => electronic [88] => quick [89] => 10x10 [90] => no [91] => no [92] => no [93] => 1 [94] => 0 [95] => 1 [96] => $109.5 [97] => $118.5 [98] => $99.5 [99] => $796.5 [100] => )
Now what I want to do is, remove the consecutive duplicates of "start". I am currently using following code
foreach ($updateddata as $value){
if($value != $previousvalue){
array_push($finaldata, $value);
}
$previousvalue = $value;
}
but is also removes other consecutive duplicates as well which I don't want to remove. Kindly help me how to do this.
I have highlighted the occurrences of "start" which I want to remove and make it one single entry, while leaving the other string as it is e.g. I don't want to remove the duplicates of string "no"
Try this
if($value != $previousvalue){
array_push($finaldata, $value);
} else if( $value != 'start'){
array_push($finaldata, $value);
}
To remove the consecutive duplicates of "start" value you should restrict the crucial condition to that value. Use the following approach:
// exemplary array
$updateddata = [
'start', 'start', 'producer', 'contact', 'no', 'no'
];
foreach ($updateddata as $k => $v) {
if (isset($updateddata[$k-1]) && $v == 'start' && $v == $updateddata[$k-1]) {
unset($updateddata[$k]);
}
}
The output:
Array
(
[0] => start
[2] => producer
[3] => contact
[4] => no
[5] => no
)
Below is the print_r version of my array ($hamle). As you can see index(80) is empty or there is a end of line or a similar character there. I tried many options to delete it if it exists and is empty but couldn't do it. I strongly think that it is reading an empty line during the 'read while' loop. It doesn't appear in all files. It appears sometimes. Can anyone think of a chic way out?
Array ( [0] => 1.b4 [1] => d5 [2] => 2.Bb2 [3] => Qd6 [4] => 3.a3 [5] => e5 [6] => 4.e3 [7] => a5 [8] => 5.b5 [9] => Nf6 [10] => 6.c4 [11] => Bg4 [12] => 7.Be2 [13] => Bxe2 [14] => 8.Qxe2 [15] => Nbd7 [16] => 9.d4 [17] => dxc4 [18] => 10.Nf3 [19] => e4 [20] => 11.Ne5 [21] => Nb6 [22] => 12.Nd2 [23] => Qe6 [24] => 13.O-O [25] => Bd6 [26] => 14.Nexc4 [27] => Nxc4 [28] => 15.Nxc4 [29] => O-O [30] => 16.Rfc1 [31] => Be7 [32] => 17.a4 [33] => Nd5 [34] => 18.Ba3 [35] => Bb4 [36] => 19.Qb2 [37] => Rfc8 [38] => 20.Bxb4 [39] => axb4 [40] => 21.Nd2 [41] => c6 [42] => 22.b6 [43] => Qe7 [44] => 23.Rc5 [45] => Ra6 [46] => 24.a5 [47] => g6 [48] => 25.Nb3 [49] => Rca8 [50] => 26.Rac1 [51] => Kg7 [52] => 27.Qd2 [53] => Qd6 [54] => 28.R1c4 [55] => Rd8 [56] => 29.Rxb4 [57] => Nxb4 [58] => 30.Qxb4 [59] => Qe7 [60] => 31.h3 [61] => Rd5 [62] => 32.Kf1 [63] => Ra8 [64] => 33.Qa4 [65] => Qd6 [66] => 34.Ke2 [67] => Kf8 [68] => 35.Rxd5 [69] => cxd5 [70] => 36.Nc5 [71] => Ke7 [72] => 37.Qb5 [73] => Rb8 [74] => 38.Kd2 [75] => f5 [76] => 39.Kc3 [77] => g5 [78] => 40.Kb4 [79] => 1-0 [80] => )
The code =>
while(! feof($file))
{
$n=$n+1;
$hamle1= fgets($file);
$hamle1 = str_replace("\n", "", $hamle1);
$hamle1 = str_replace("\r", "", $hamle1);
$hamle1 = trim($hamle1);
$hamle1 = explode(" ", $hamle1);
foreach($hamle1 as $item)
{
$hamle[] = $item;
}
array_filter will remove empty entries
$hamle = array_filter($hamle);
To delete the last element of array if it's empty then use below code.
// get the last element of array
if(trim(end($hamle)) == '')
{
// Remove last element from array
array_pop($hamle) ;
}
Kindly visit below links to understand the functions which are used in above code.
trim
end
array_pop
I have a single dimentional PHP Array that has latitude, longitude, and time data. The data goes eg [lat, long, date, lat, long, date, lat, long, date.... etc etc]
Array ( [0] => -28.0447606 [1] => 153.4340961 [2] => 1424136836118 [3] => -28.0447612 [4] => 153.4340963 [5] => 1424136876189 [6] => -28.0447658 [7] => 153.4340962 [8] => 1424136897993 [9] => -28.0447619 [10] => 153.4340615 [11] => 1424136918045 [12] => -28.0447656 [13] => 153.434057 [14] => 1424136938057 [15] => -28.0447613 [16] => 153.4340484 [17] => 1424136958085 [18] => -28.0447791 [19] => 153.4340959 [20] => 1424136978117 [21] => -28.0447584 [22] => 153.4340501 [23] => 1424136998135 [24] => -28.0447676 [25] => 153.434047 [26] => 1424137018179 [27] => -28.044782 [28] => 153.4340982 [29] => 1424137038185 [30] => -28.0447599 [31] => 153.4340496 [32] => 1424137058214 [33] => -28.0447614 [34] => 153.4340531 [35] => 1424137078589 [36] => -28.0447588 [37] => 153.4340963 [38] => 1424137098731 [39] => -28.0447768 [40] => 153.434098 [41] => 1424137138640 [42] => -28.0447141 [43] => 153.4341097 [44] => 1424137158672 [45] => -28.0447628 [46] => 153.4340962 [47] => 1424137178698 [48] => -28.0447622 [49] => 153.4340962 [50] => 1424137198726 [51] => -28.0447528 [52] => 153.4340936 [53] => 1424137218871 [54] => -28.0447636 [55] => 153.4340472 [56] => 1424137258825 [57] => -28.0447608 [58] => 153.434097 [59] => 1424137279945 [60] => -28.0447656 [61] => 153.4340979 [62] => 1424137300018 )
I am just wondering if there is an easy way to convert this into a two dimensional array so I can access them like $gps[0][1] or $gps[3][0] etc. I've tried a few ways like using a for loop, but surely there's some other way I'm overlooking.
If I'm not wrong this is what you want.
You just need to change the $arr for your variable.
$coords = array_map(function($coords) {
return array(
"lat" => $coords[0], # latitude
"long" => $coords[1], # longitude
"ts" => $coords[2] # timestamp
);
}, array_chunk($arr, 3));
/*
Array (
Array (
[lat] => 12
[long] => 34
[ts] => 56
)
Array (
[lat] => 12
[long] => 34
[ts] => 56
)
... and so on
)
*/
I am attempting to make an array of IDs and use that array in another query to make sure the 2nd query results have the same ids that are in the array.
As per the requirements of the rest of my code, I would prefer not to use the join method.
$topartistsquery = "SELECT main_id FROM pages WHERE catnum = '201' AND top100 = 'y'";
$result = mysql_query($topartistsquery);
$topartistarray = array();
while(($row = mysql_fetch_assoc($result))) {
$topartistarray[] = $row['main_id'];
}
//print_r($topartistarray);
$eventquery = mysql_query("SELECT * FROM TNDB_CSV2 WHERE City = '".$city."' AND PCatID = '2' AND PerformerID IN ($topartistarray) AND DateTime >= CURDATE() ORDER BY DateTime LIMIT 3");
Here is the output of the $topartistarray:
Array ( [0] => 28 [1] => 1126 [2] => 47643 [3] => 2769 [4] => 41512 [5] => 1429 [6] => 163 [7] => 48418 [8] => 198 [9] => 3748 [10] => 10161 [11] => 39890 [12] => 14160 [13] => 25407 [14] => 29219 [15] => 36980 [16] => 1860 [17] => 41299 [18] => 342 [19] => 7468 [20] => 33205 [21] => 1564 [22] => 33911 [23] => 15183 [24] => 540 [25] => 974 [26] => 8358 [27] => 30678 [28] => 4804 [29] => 4266 [30] => 517 [31] => 522 [32] => 1550 [33] => 15989 [34] => 930 [35] => 3383 [36] => 26468 [37] => 5560 [38] => 1063 [39] => 2870 [40] => 4055 [41] => 24917 [42] => 46223 [43] => 973 [44] => 334 [45] => 27970 [46] => 27985 [47] => 356 [48] => 6655 [49] => 201 [50] => 1930 [51] => 1069 [52] => 38053 [53] => 397 [54] => 21713 [55] => 53725 [56] => 4941 [57] => 864 [58] => 41904 [59] => 59233 [60] => 53970 [61] => 50875 [62] => 54175 [63] => 56861 [64] => 53317 [65] => 55258 [66] => 823 [67] => 55704 [68] => 59797 [69] => 27866 [70] => 123 [71] => 59749 [72] => 1567 [73] => 34851 [74] => 3399 [75] => 58674 [76] => 59442 [77] => 60334 )
Any constructive input is greatly appreciated. Thank you in advance
Try this:
if (isset($topartistarray) and count($topartistarray) {
$eventquery = mysql_query("SELECT * FROM TNDB_CSV2 WHERE City = '".$city."' AND PCatID = '2' AND PerformerID IN (".implode(",",$topartistarray).") AND DateTime >= CURDATE() ORDER BY DateTime LIMIT 3");
} else {
$eventquery = false;
}