Please understand this is not how I would have designed this, nor is it the proper way to do something like this.
However, since I was given this task, and have absolutely no way around the way the data is structured, I am tasked with working with what I was given.
I have a table structured as such:
ID
UniqueName
DisplayName
Tag1
Tag...
Tag27
Yes, I have 27 columns, Tag1 - Tag27
NOTE: This cannot be changed due to the data coming from 15 year old software that is not updated, nor is there any chance of it being updated. (Hence my dilema)
I am getting my resultset utilizing PDO's fetchAll(PDO::FETCH_ASSOC)
How can I loop over these Tag* columns, and just the Tag* columns, without having to type out rs[$i]['Tag#'] for each individual one, also noting that there may be instances where I could have 14 Tag columns` instead of the 27 I state in this example...
COLUMN CODE:
// prod_opts_value_fields_prefix = "Tag"
foreach ($rs[$i] as $key => $val) {
if ( strpos ( strtolower ( $key ), strtolower ( $prod_opts_value_fields_prefix ) ) !== false ) {
$vals .= $val . '|';
echo $val . '<br />';
}
}
Nothing is getting echo'd
You could run a foreach over the rs[$i] and check if the key contains 'Tag' with strpos and go from there.
Alternatively you could use just a for loop running on 1 to 27 (or any maximum Tag value) and have an if condition checking if rs[$i]['Tag$N'] (where N is the for loop counting variable) isset, if not break from the loop as that means you've gone through all possible Tags.
The second solution here does have the assumption Tags are always numbered sequentially and in ascending order.
If you need it in multiple places, I'd probably add a setting to a preferences array or a constant that you can easily re-use:
const TAG_COLUMNS = [
'Tag1',
...
'Tag27',
];
You can use that wherever you need it to loop over a result set.
Related
With the updated code below, my search is working, but only using the last word in the Array. Is there a way to search the MySQL column using all words the user inputted?
Note: All input sanitization and escaping is completed in my code but not shown here.
I have two PHP arrays: $search_exploded (user inputted search terms) and $metaphoneArr (metaphones of keywords in MySQL).
I'm cycling through $search_exploded and $metaphoneArr, and if the Levenshtein is less than 2, then I'm adding the metaphone element to a third array called $levenResultsArr.
In MySQL, I'm joining two tables, and if there's a result in my third array ($levenResultsArr) that matches a row in my metaphone_col, then I want the results printed. Somehow, though, I am not referencing the third array correctly in the MySQL statement.
Any advice? Here is part of my PHP code.
$levenResultsArr = array();
foreach ($search_exploded as $search_each => $searchWord) {
$search_each2 = metaphone($searchWord);
echo $search_each2 . "<br/>";
foreach ($metaphoneArr as $metaword => $val) {
$lev = levenshtein($search_each2, $val);
if ($lev < 2) {
array_push($levenResultsArr, $search_each2);
}
}
}
// And shown below is the MySQL statement
$constructs = "
SELECT vt.idvideolist,
vt.videotitle,
vt.videodescription
FROM videolist_tbl vt
INNER JOIN keyword__video k2v ON (vt.idvideolist = k2v.video_id)
INNER JOIN keywords_tbl k ON (k2v.keyword_id = k.idkeywords_tbl)
WHERE k.metaphone_col = '$search_each2'
";
It's only searching using the last word in the array instead of all words in the array.
You're correct that you aren't referencing your array correctly in your query. You are simply referencing the variable '$search_each2'. You want to check the entire array. The best way to do this would to be to convert the array to a string using implode and using the MySQL IN clause. We need to modify the array first to format it correctly:
foreach($levenResultsArr as &$foo){
$foo = "'".$foo."'"; //add single quotes around each object in the array
}
$levenAsStr = implode(",", $levenResultsArr);
Then simply change your last line to the following:
WHERE k.metaphone_col IN (".$levenAsStr.")";
Did this from memory because I'm not in a testing environment, please let me know if there are any syntax errors. Should work for you!
Without seeing more of the code its a bit difficult to determine what is going on. A couple of thoughts though.
What var is the array containing all search terms?
I noticed you are pushing items into $levenResultsArr. I don't see it being used in the query though.
If it is $search_each_2 you may try using the IN syntax:
WHERE k.metaphone_col IN '$search_each2';
This will only match exact values however. i.e.:
some value does not return if the IN array contains value. The array would have to contain some value
If you need it to match partial searches you may try using LIKE with wildcards: %
WHERE k.metaphone_col LIKE "%searchTerm1%"
OR k.metaphone_col LIKE "%searchTerm2%"
OR k.metaphone_col LIKE "%searchTerm3%"
You could potentially use the implode function to create the sql string from the array: http://php.net/manual/en/function.implode.php
I'm working on some stat tracking code for a game (yes, it's runescape. sue me).
I want to pull information from the high scores using an api which produces an array looking like this (captured using print_r)
Array
(
[getHiscore] => Array
(
[overall] => Array
(
[rank] => 61995
[lvl] => 2273
[totalxp] => 193310588
)
[attack] => Array
(
[rank] => 93406
[lvl] => 97
[totalxp] => 11747494
)
...and so on.
My question is how can i take what this api gives me and place it into a database table; I want to get an array such as this for a particular user and update their stats with it.
Would i use explode? It seems like the right idea to me but how would i actually use it to split the separate numbers and words?
My database is not totally realised yet, however each record will include a user name and then the level and total xp in each of 27 "skills". This almost certainly not best practice for database design but i'm as novice as they come so it's the best i can do.
You don't need to explode. It's an array and therefore it's already exploded. You need to iterate through it to build meaningful queries.
I'm not familiar with runescape so I don't know if your example is showing just the relevant fields of you need to discard some part. If it's the former, then you should try something like
$query="insert into stats_table (description, rank, lvl, totalexp) values (:description, :rank, :lvl, :totalext);";
$Statement = $DBConn->prepare($query);
foreach($fullArray as $description=>$subArray) {
$Statement->bindValue(':description',$description, PDO::PARAM_STR);
foreach($subArray as $key=>$value) {
$Statement->bindValue($key,$value, PDO::PARAM_STR);
}
$Statement->execute();
}
That will insert two rows containing
overall 61995 2273 193310588
attack 93406 97 11747494
PD: $DBConn is a PDO connection instance to whatever DB engine you're using.
Let's say this is all stored in the variable $ary:
$ary['getHiscore']['overall']['rank'] = 61995;
$ary['getHiscore']['overall']['lvl'] = 2273;
$ary['getHiscore']['overall']['totalxp'] = 193310588;
$ary['getHiscore']['attack']['rank'] = 93406;
$ary['getHiscore']['attack']['lvl'] = 97;
$ary['getHiscore']['attack']['totalxp'] = 11747494;
So, to get those values, it's:
$attackLevel = $ary['getHiscore']['attack']['lvl'];
If you
echo $attackLevel;
it would be 97.
Just pull the arrays out with a foreach loop and write some sql to insert the data. I don't know anything about your DB so if you want help there update your question please.
Simplest way to do this:
foreach($highscores as $highScoreType => $highScoreValues){
$rank = $highScoreValues['rank'];
$lvl = $highScoreValues['lvl'];
$totalXp = $highScoreValues['totalxp'];
//then it's up to you to put it into a table somehow.
}
$highScoreType will contain the values 'overall','attack',etc
This format of the foreach loop is extremely handy when iterating over an associative array (hash map/hash table, which is actually what all PHP arrays are underneath) will allow you to not only grab the value of the current pointed to entry, but also the key for that entry.
So, assume we have a single entry hashmap with key 'foo' and value 'bar'. If I want to show 'bar' I would simple
echo $map['foo'];//will output 'bar'
and if I put this into the above type of foreach loop
foreach($map as $fooKey => $barValue) {
echo $fooKey;//outputs 'foo'
echo $barValue;//outputs 'bar'
}
I have a working script, but I'm sure that my method of managing arrays could be better. I've searched for a solution and haven't found one, but I'm sure that I should be using the functionality of associative arrays to do things more efficiently.
I have two arrays, one from a CSV file and one from a DB. I've created the CSV array as numeric and the DB array as associative (although I'm aware that the difference is blurry in PHP).
I'm trying to find a record in the DB array where the value in one field matches a value in the CSV array. Both arrays are multi-dimensional.
Within each record in each array there is a reference number. It appears once in the CSV array and may appear in the DB array. If it does, I need to take action.
I'm currently doing this (simplified):
$CSVarray:
('reference01', 'blue', 'small' ),
('reference02', 'red', 'large' ),
('reference03', 'pink', 'medium' )
$Dbarray:
(0 => array(ref=>'reference01',name=>"tom",type=>"mouse"),
(1 => array(ref=>'reference02',name=>"jerry",type=>"cat"),
(2 => array(ref=>'reference03',name=>"butch",type=>"dog"),
foreach ($CSVarray as $CSVrecord) {
foreach ($Dbarray as $DBrecord) {
if ($CSVarray[$numerickey] == $DBrecord['key'] {
do something with the various values in the $DBrecord
}
}
}
This is horrible, as the arrays are each thousands of lines.
I don't just want to know if matching values exist, I want to retrieve data from the matching record, so functions like 'array_search ' don't do what I want and array_walk doesn't seem any better than my current approach.
What I really need is something like this (gibberish code):
foreach ($CSVarray as $CSVrecord) {
WHERE $Dbarray['key']['key'] == $CSVrecord[$numerickey] {
do something with the other values in $Dbarray['key']
}
}
I'm looking for a way to match the values using the keys (either numeric or associative) rather than walking the arrays. Can anyone offer any help please?
use a hash map - take one array and map each key of the record it belongs to, to that record. Then take the second array and simply iterate over it, checking for each record key if the hashmap has anything set for it.
Regarding your example:
foreach ($DBarray as $DBrecord){
$Hash[$record[$key]] = $DBrecord;
}
foreach ($CSVarray as $record){
if (isset($Hash[$record[$CSVkey]])){
$DBrecord = $Hash[$record[$CSVkey]];
//do stuff with $DBrecord and $CSVrecord
}
}
this solution works at O(n) while yours at O(n^2)...
You can use foreach loops like this too:
foreach ($record as $key => $value) {
switch($key)
{
case 'asd':
// do something
break;
default:
// Default
break;
}
}
A switch may be what you are looking for also :)
Load CSV into the db, and use db (not db array) if possible for retrieval. Index the referenceid field.
i have two arrays i.e$ar1=array("Mobile","shop","software","hardware");and$arr2=arry("shop","Mobile","shop","software","shop")
i want to compare the elements of arr2 to arr1 i.e
foreach($arr2 as $val)
{
if(in_array($val, $arr1))
{
//mycode to insert data into mysql table
variable++; // here there should be a variable that must be increamented when ever match found in $arr2 so that it can be saved into another table.
}
$query="update table set shop='$variable',Mobile='$variable'.......";
}
the $variable should be an integer value so that for later use i can use this variable(shop i.e in this example its value should be 3) to find the match.
My question is how can i get the variable that will increamented each time match found.
Sorry, I don't fully understand the purpose of your code. You can use array_intersect to get common values and array_diff to get the unique values when comparing two arrays.
i want to compare the elements of arr2 to arr1 i.e
Then you are essentially doing the same search for shop three times. It's inefficient. Why not sort and eliminate duplicates first?
Other issues. You are comparing arr2 values with the ones in arr1, which means the number of repetation for "shop" will not be 3 it will be one. Doing the opposite might give you the number of repetation of arr1[1] in arr2 =3.
There are multitude of ways to solve this problem. If efficiency is required,you might wish to sort so you don't have to go beyond a certain point (say s). You can learn to use indexes. Infact the whole datastructure is revolved around these kinds of things - from quick and dirty to efficient.
Not sure I understand the connection between your two arrays. But you can use this to count how many items are in your second array:
$items = array("shop","Mobile","shop","software","shop");
$count = array();
foreach($items as $item)
{
if(isset($count[$item]))
{
$count[$item]++;
}
else
{
$count[$item] = 1;
}
}
print_r($count); // Array ( [shop] => 3 [Mobile] => 1 [software] => 1 )
Is it possible to store PHP syntax in variables for later use and repetition like this:
$ifStart = "if(";
$ifEnd = "){ echo 'Test'; }";
$ifStart 'A' == 'B' $ifEnd;
Edit: What I'm trying to accomplish is this:
I have 3 form fields, and when the PHP script is loaded, any of the three can be set. None can be set, two, one... So I need some way to determine how many are set and what to output according to that. That's why.
Edit: Right, so I have one HTML Select and two text input fields. My script checks if those fields are set (isset) and does some code accordingly, putting information into arrays etc. What I want to do now though, is to check if the variables have been set one by one, so I can output the correct results which I have stored in arrays.
New edit: This is obviously so hard to explain. But imagine a search engine where you decide which fields you'd like to fill out and then the script checks which fields are set and loops through the array with all the results, only gathering out the elements with sub-elements corresponding to the requested search, that's what I'm trying to achieve.
Here's the array design with AGE and COUNTY selected/set in the POST (hence why there's no [city] elements:
Array
(
[1] => Array
(
[id] => 1
[age] => 19
[county] => 4353
)
[2] => Array
(
[id] => 2
[age] => 20
[county] => 4020
)
[3] => Array
(
[id] => 3
[age] => 30
[county] => 4020
)
)
Still trying to figure out how to only select out a specific array element depending on -its- contents. For example, I have an array like this:
Array ( 1: [age][county], 2: [age][county], 3: [age], 4: [county], 5: [age][county] )
I'd then like to only select the IDs containing both age and county, in this example ID 1, 2 and 5.
Edit: It'll be similar to a SQL query: WHERE age AND county, only this is in an array
It is possible...
BUT
if you have to do it, there's definitely something wrong with your design!
[Edit after your edit]
Your edit shows me that I was right. What you're trying to do, can be accomplished in a better way!
So if I understand you correctly, you want to alter your output according to which form fields have been filled/answered by the user. So far you are storing some values from the $_POST array in another array. In order to generate your output, it would be best to loop over that array and concatenate strings, depending on what has been filled.
foreach ($myArray as $formField => $value)
{
//do something for each $formField, depending on the $value
}
If that still leaves you puzzled, comment here.
The way you wrote it, it would not work, you would need to use eval(). The use of eval() , is in most cases bad practice. That would not be the main problem though, the main problem is, that such code is hard to read, hard to debug as well as maintain and hard to document. All it all, it is bad practice and will lead to a bad solution and more problems.
One clean way (clean because it avoids eval()) to do relatively dynamic code would be to store either a function name or, after php 5.3, a function reference.
E.g. something like:
$callback = "truth_check";
$condition_result = ($a == $b);
if(is_callable($callback)){
$callback($condition_result);
}
See a running example here: http://ideone.com/1SBYS
In your case the callback could be a result to run, e.g. "print_message_on_true_input" as some comparison function and the input could be the result of a conditional tested anywhere in your code, e.g. $print = (false || $a == $b);
Give your specific use cases, though, because 90% of the time intended behavior can be acheived much less fragily without resorting to any dynamic code at all.
Is it possible? Yes, using eval.
Should you do it? NO NO NO NO PLEASE DON'T.
No. You can use functions instead.
function my_compare($a, $b, $symbol = '==') {
eval('$result = $a ' . $symbol . ' $b;');
if ($result) echo 'Test';
}
// ...
my_compare('A', 'B');
Keep in mind the comments above that warn you about this idea!
But you could probably do this:
$ifStart = "if(";
$ifEnd = "){ echo 'Test'; }";
eval( " {$ifStart} 'A' == 'B' {$ifEnd} " );
Ok, so what you need has little relation to the dynamic code involved in your original question, so I'm going to start over and propose a different approach entirely:
//INITIALIZE
//get your input variables, defaulting to null
$inputs = array('select'=>#$_REQUEST['select_name'], 'input1'=>#$_REQUEST['input_1_name'], 'input2'=>#$_REQUEST['input_2_name']);
// initialize your output variables
$out1 = $out2 = $out3 = null;
//MANIPULATIONS
//perform actions based on the presence of variables in the array
if($inputs['select'] == 'whatever'){
$out1 = 'select was '.htmlentities($inputs['select']);
}
// .... perform more manipulations, setting output strings ...
// OUTPUT SECTION
echo $out1.$out2.$out3;