MySql Query to Array to MySql IN Query - php

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;
}

Related

Remove consecutive occurrences of string "start" from array PHP ARRAY

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
)

How to generate unique, non repeating combinations of letters from a given string in PHP

I read through about 15 other resources and answers related to this subject on Stackoverflow, and only found one code that was close to what I wanted, but I don't know how to refine its generation further.
This is what I have so far:
$combos = array_unique($letters);
$lastres = $letters;
for ($i = 1; $i < count($letters); $i++) {
$newres = array();
foreach ($lastres as $r) {
foreach ($letters as $let) {
$new = $r . $let;
if (!in_array($new, $newres)) {
$newres[] = $new;
// Action
$combos[] = $new;
}
}
}
$lastres = $newres;
}
print_r($combos);
?>
The above code generates 1365 results from the input "apple", but it includes unwanted results.
I will paste here the first 86 results:
[0] => a [1] => p [3] => l [4] => e [5] => aa [6] => ap [7] => al [8] => ae [9] => pa [10] => pp [11] => pl [12] => pe [13] => la [14] => lp [15] => ll [16] => le [17] => ea [18] => ep [19] => el [20] => ee [21] => aaa [22] => aap [23] => aal [24] => aae [25] => apa [26] => app [27] => apl [28] => ape [29] => ala [30] => alp [31] => all [32] => ale [33] => aea [34] => aep [35] => ael [36] => aee [37] => paa [38] => pap [39] => pal [40] => pae [41] => ppa [42] => ppp [43] => ppl [44] => ppe [45] => pla [46] => plp [47] => pll [48] => ple [49] => pea [50] => pep [51] => pel [52] => pee [53] => laa [54] => lap [55] => lal [56] => lae [57] => lpa [58] => lpp [59] => lpl [60] => lpe [61] => lla [62] => llp [63] => lll [64] => lle [65] => lea [66] => lep [67] => lel [68] => lee [69] => eaa [70] => eap [71] => eal [72] => eae [73] => epa [74] => epp [75] => epl [76] => epe [77] => ela [78] => elp [79] => ell [80] => ele [81] => eea [82] => eep [83] => eel [84] => eee [85]
The world apple contains 1 A, 1 L, and 1 E, yet it includes results like "aa", "ll", "ee", "aaa", "lll", "eee". I do not want these included in my results.
Another issue I am having is I only need 1 instance of a combination of letters (regardless of order) for a specific size. So for instance: when it generates 3 letter combinations I only need "ple" and not "pel", "lpe", "lep", "epl" or "elp", the same for 4 letters, 5, etc.
I am not sure if I am even approaching this correctly or if I am even explaining what I mean properly.
Any help is greatly appreciated.
What you are looking for IMO is a power set:
$ cat a.php
<?php
$result = array_values(array_unique(str_split('apples')));
function getPowerSet($str_array) {
$set_size = count($str_array);
$pow_set_size = pow(2, $set_size);
$return = array();
for($counter = 0; $counter < $pow_set_size; $counter++) {
$tmp_str = "";
for($j = 0; $j < $set_size; $j++) {
if($counter & (1 << $j)) {
$tmp_str .= "$str_array[$j]";
}
}
$return[] = $tmp_str;
}
return $return;
}
print_r(getPowerSet($result));
?>
$ php a.php
Array
(
[0] =>
[1] => a
[2] => p
[3] => ap
[4] => l
[5] => al
[6] => pl
[7] => apl
[8] => e
[9] => ae
[10] => pe
[11] => ape
[12] => le
[13] => ale
[14] => ple
[15] => aple
[16] => s
[17] => as
[18] => ps
[19] => aps
[20] => ls
[21] => als
[22] => pls
[23] => apls
[24] => es
[25] => aes
[26] => pes
[27] => apes
[28] => les
[29] => ales
[30] => ples
[31] => aples
)
If you do want letters to repeat, for examples 2 'p's in apples, then make a small change:
$result = str_split('apples');
Then the result would be:
$ php index.php
Array
(
[0] =>
[1] => a
[2] => p
[3] => ap
[4] => p
[5] => ap
[6] => pp
[7] => app
[8] => l
[9] => al
[10] => pl
[11] => apl
[12] => pl
[13] => apl
[14] => ppl
[15] => appl
[16] => e
[17] => ae
[18] => pe
[19] => ape
[20] => pe
[21] => ape
[22] => ppe
[23] => appe
[24] => le
[25] => ale
[26] => ple
[27] => aple
[28] => ple
[29] => aple
[30] => pple
[31] => apple
[32] => s
[33] => as
[34] => ps
[35] => aps
[36] => ps
[37] => aps
[38] => pps
[39] => apps
[40] => ls
[41] => als
[42] => pls
[43] => apls
[44] => pls
[45] => apls
[46] => ppls
[47] => appls
[48] => es
[49] => aes
[50] => pes
[51] => apes
[52] => pes
[53] => apes
[54] => ppes
[55] => appes
[56] => les
[57] => ales
[58] => ples
[59] => aples
[60] => ples
[61] => aples
[62] => pples
[63] => apples
)
Read more about the algorithm here: http://www.geeksforgeeks.org/power-set/

How to delete the last element of an array if it is empty

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

How to generate a range of numbers in PHP with increment of 0.25 ( +6.00 at -15.00 )?

I need to generate a grid ( +6.00 at -15.00 ) ! Somebody can help me ? PHP CODE please.
6.00
5.75
5.50
5.25
5.00
.
.
.
.
-15.00
"Actual Credit Goes To #Mark Baker"
But Here I just put it in a good shape and here is exactly how you can do it :
<?php
$grid = range(6, -15, 0.25);
echo "<pre>";
print_r($grid);
echo "</pre>";
?>
Here is Output of the Code :
Array
(
[0] => 6
[1] => 5.75
[2] => 5.5
[3] => 5.25
[4] => 5
[5] => 4.75
[6] => 4.5
[7] => 4.25
[8] => 4
[9] => 3.75
[10] => 3.5
[11] => 3.25
[12] => 3
[13] => 2.75
[14] => 2.5
[15] => 2.25
[16] => 2
[17] => 1.75
[18] => 1.5
[19] => 1.25
[20] => 1
[21] => 0.75
[22] => 0.5
[23] => 0.25
[24] => 0
[25] => -0.25
[26] => -0.5
[27] => -0.75
[28] => -1
[29] => -1.25
[30] => -1.5
[31] => -1.75
[32] => -2
[33] => -2.25
[34] => -2.5
[35] => -2.75
[36] => -3
[37] => -3.25
[38] => -3.5
[39] => -3.75
[40] => -4
[41] => -4.25
[42] => -4.5
[43] => -4.75
[44] => -5
[45] => -5.25
[46] => -5.5
[47] => -5.75
[48] => -6
[49] => -6.25
[50] => -6.5
[51] => -6.75
[52] => -7
[53] => -7.25
[54] => -7.5
[55] => -7.75
[56] => -8
[57] => -8.25
[58] => -8.5
[59] => -8.75
[60] => -9
[61] => -9.25
[62] => -9.5
[63] => -9.75
[64] => -10
[65] => -10.25
[66] => -10.5
[67] => -10.75
[68] => -11
[69] => -11.25
[70] => -11.5
[71] => -11.75
[72] => -12
[73] => -12.25
[74] => -12.5
[75] => -12.75
[76] => -13
[77] => -13.25
[78] => -13.5
[79] => -13.75
[80] => -14
[81] => -14.25
[82] => -14.5
[83] => -14.75
[84] => -15
)

PHP array keys to a string

Hi I have an array that looks something like this
Array ( [0] => T [1] => T [2] => T [3] => T [4] => T [5] => T [6] => T [7] => T [8] => T [9] => T [10] => T [11] => T [12] => T [13] => T [14] => T [15] => T [16] => T [17] => T [18] => T [19] => T [20] => T [21] => T [22] => T [23] => T [24] => T [25] => T [26] => T [27] => T [28] => T [29] => T [30] => T [31] => T [32] => T [33] => T [34] => T [35] => T [36] => T [37] => T [38] => T [39] => T [40] => T [41] => T [42] => T [43] => T [44] => T [45] => T [46] => T [47] => T [48] => T [49] => T [50] => T [51] => T [52] => T [53] => T [54] => T [55] => T [56] => T [57] => T [58] => T [59] => T [60] => T [61] => T [62] => T [63] => T [64] => T [65] => T [66] => T [67] => T [68] => T [69] => T [70] => T [71] => T [72] => 0 [73] => 0 [74] => 0 [75] => 0 [76] => 0 [77] => 0 [78] => 0 [79] => 0 [80] => 0 [81] => 0 [82] => 0 [83] => 0 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 0 [97] => 0 [98] => 0 [99] => 0 )
What I need to do is group keys [0]-[49], [50]-[99], and [100]-[149] into three string variables so tht they can be inserted into the database fields 'answ1-50', 'answ51-100', and 'answ101-150. Im a little bit lost on this one as I am still somewhat of a php novice. How do i break up this array into three groups as a string?
While not good practice, the answer to your posted question is to:
list($group1, $group2, $group3) = array_map("implode", array_chunk($arr, 50));
(assumes that you always have 150 answers. if you don't, skip the list and you will have an array of results $groups[0], $groups[1], $groups[2])
I'm not sure why you want to do this, but this should do the trick.
//assuming content in $myarray
$newarr = array();
for($i = 0; $i < count($myarray); $i++ {
$j = floor($i / 50);
if($j == $i / 50) {
$newarr[$j] = "";
}
$newarr[$j] += (string)$myarray[$i];
}
Each element of $newarr should be a string with 50 answers in it.
You can just simply write it under the loop and concatenate the values to a string.
for($i = 0; $i < count($yourarray); $i++) {
if(i>=0 && $i<50)
{
$str1=$str1.$yourarray[i];
}
else if(i>=50 && $i<100)
{
$str2=$str2.$yourarray[i];
}
else if(i>=100 && $i<150)
{
$str3=$str3.$yourarray[i];
}}

Categories