I have a keyspace in cassandra with columnfamily(let A) which is having composite key
another column family(let B) i am storing an exact number of rows which exist in the A column family. when i am fetching the data using multiget it's not giving the actual sorted data.
A: [1] = 13;
B:
[6014:2:0] = "aaaaaa";
[6014:2:1] = "bbbbbb";
[6014:2:2] = "cccccc";
[6014:2:3] = "dddddd";
[6014:2:4] = "eeeeee";
[6014:2:5] = "ffffff";
[6014:2:6] = "gggggg";
[6014:2:7] = "hhhhhh";
[6014:2:8] = "iiiiii";
[6014:2:9] = "jjjjjj";
[6014:2:10] = "kkkkkkk";
[6014:2:11] = "lllllll";
[6014:2:12] = "mmmmmmm";
my code
require_once(__DIR__.'/phpcassa/lib/autoload.php');
use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
use phpcassa\SystemManager;
use phpcassa\Schema\StrategyClass;
$connection = new ConnectionPool('KEYSPACE', array('XXXX', 'YYYY', 'ZZZZ'));
$numDtls = new ColumnFamily($connection, 'A');
$key = 1;
$num_details = $numDtls->get($key);
$num = $num_details;
$json = '';
$key_array = array();
if(isset($num)){
$str = new ColumnFamily($connection, 'B');
for($i = 0;$i <= $num; $i++){
$key_array[] = array($table, $flag, $i);
}
$detail = $str->multiget($key_array);
$json = json_encode($detail);
}
its giving the output as
6014:2:0
6014:2:6
6014:2:9
6014:2:11
6014:2:4
6014:2:1
6014:2:12
6014:2:8
6014:2:7
6014:2:10
6014:2:3
6014:2:5
6014:2:2
it giving output in jumbled order...
How to get in sorted manner?
And how to get more than 100 rows?
Multiget makes no ordering guarantees, full stop. As to how to get more than 100 rows... you're asking the wrong question, large multigets are an antipattern. You need to denormalize so you can get the data you want with a single slice, instead. Check out my "timeline" example here: http://www.datastax.com/dev/blog/schema-in-cassandra-1-1
Related
each line of The file $wordFile [which is over hundreds of lines],consists of a word + 'space' + decimal number + '\n'.
I need to extract the number and word from the file and add them to the $indivFreq array. But am getting error
Notice: Undefined offset: 1 in /.....
This is my code:
$wordFile = file_get_contents("wordFile.txt");
$termArr = explode("\n", $wordFile);
$wordFile2 = file_get_contents("wordFile2.txt");
$termArr2 = explode("\n", $wordFile2);
$cityFreqsArr = array($termArr, $termArr2);
$twoTerms = array();
$decimalScore = number_format("0", 8);
$indivFreqs = array("", $decimalScore);
$twoDimFreqArr = [[]];
for($i=0; $i<count($termArr); $i++){
for($i2=0; $i2 < count($cityFreqsArr[$i]); $i2++){
$currentTerm = $cityFreqsArr[$i][$i2];
$currentTerm = trim($cityFreqsArr[$i][$i2]);
$twoTerms = explode(' ', $currentTerm); //separating each string of term and its frequency into the 2 parts
$indivFreqs[0] = $twoTerms[0];
$indivFreqs[1] = $twoTerms[1]; //**error is here
$twoDimFreqArr[$i][$i2] = $indivFreqs; //for each city and each term there is an array with the term and its frequency
//i.e.,
}
}
I tried printing out the contents of $twoTerms after the explode and am getting arrays with index 0 => word and index 1 => number, Which is why I don't understand why php won't see that it has an index of 1 also?
You've defined $cityFreqsArr as a 2-dimensional array with number of rows equal to files being read(which for this particular example is 2. first row(with $index=0) is holding $wordFile.txt/$termArr's data. And second row for the second one.
Now for storing data for each city, each term & their respective term & frequency you should first traverse $cityFreqsArr by its rows (as files) & then second level $index (as content of each file). So, I updated first & second for's conditions regarding this.
$wordFile = file_get_contents("wordFile.txt");
$termArr = explode("\n", $wordFile);
$wordFile2 = file_get_contents("wordFile2.txt");
$termArr2 = explode("\n", $wordFile2);
$cityFreqsArr = array($termArr, $termArr2);
$twoTerms = array();
$decimalScore = number_format("0", 8);
$indivFreqs = array("", $decimalScore);
$twoDimFreqArr = [[]];
for ($i = 0; $i < count($cityFreqsArr); $i++) { //Traversing file by file
for ($i2 = 0; $i2 < count($cityFreqsArr[$i]); $i2++) { //Traversing in each file
$twoTerms = explode(' ', trim($cityFreqsArr[$i][$i2]));
$indivFreqs[] = $twoTerms[0];
$indivFreqs[] = $twoTerms[1];
$twoDimFreqArr[$i][$i2] = $indivFreqs;
}
}
// Test it:
echo '<pre>';
print_r($twoDimFreqArr);
echo '</pre>';
I'm looking to increment a number after a certain letter.
I have a list of own Ids and i would like to increment it without write it manually each time i add a new id.
$ids = array('303.L1', '303.L2', '303.L3', '303.L4');
so i use the END() function to extract the last id from this array.
this is what i've tried but i cannot get a result.
$i = 0;
while($i <= count($ids)){
$i++;
$new_increment_id = 1;
$final_increment = end($last_id) + $new_increment_id;
}
echo $final_increment;
New method, but it is adding me double dot between number and letter.
$i = 0;
while($i <= count($ids)){
$i++;
$chars = preg_split("/[0-9]+/", end($ids));
$nums = preg_split("/[a-zA-Z]+/", end($ids));
$increment = $nums[1] + 1;
$final_increment = $nums[0].$chars[1].$increment;
}
//i will use this id to be inserted to database as id:
echo $final_increment;
Is there another way to increment the last number after L ?
Any help is appreciated.
If you don't want a predefined list, but you want a defined number of ids returned in an $ids variable u can use the following code
<?php
$i = 0;
$number_of_ids = 4;
$id_prefix = "303.L";
$ids = array();
while($i < $number_of_ids){
$ids[] = $id_prefix . (++$i); // adds prefix and number to array ids.
}
var_dump($ids);
// will output '303.L1', '303.L2', '303.L3', '303.L4'
?>
I'm a bit confused because you say "without write it manually". But I think I have a solution:
$ids = array('303.L1', '303.L2', '303.L3', '303.L4');
$i = 0;
while($i <= count($ids)){
++$i;
//Adding a new item to that array
$ids[] = "303.L" . $i;
}
This would increment just that LAST number, starting at zero. If you wanted to continue where you left off, that'd be simple too. Just take $i = 0; and replace with:
//Grab last item in array
$current_index = $ids[count($ids) - 1];
//Separates the string (i.e. '303.L1') into an array of ['303', '1']
$exploded_id = explode('.L', $current_index);
//Then we just grab the second item in the array (index 1)
$i = $exploded_id[1];
I basically want to scan a load of comments for illegal words and then replace those illegal words with a clean version.
I have two arrays, one array has all the comments to check, the other array has all of the illegal words to look for.
The first for loop gets the comments, the nested for loop then scans the comments for each of the illegal words and replaces them. The thing is though - it doesn't actually seem to work. Could you please advise if it is a problem with my loop structure, or the actual update logic?
$numComments = count($commentsToCheck);
$numIllegalWords = count($illegalWords);
for($i = 0; $i <= $numComments; $i++)
{
$message = $commentsToCheck[$i]['message'];
$commentId = $commentsToCheck[$i]['id'];
//error_log($message.'-'.$commentId);
for($j = 0; $j <= $numIllegalWords; $j++)
{
//Get word to replace with
$word = $illegalWords[$j]['word'];
//error_log($word);
$length = strlen($word);
$first = substr($word,0);
$last = substr($word,-1);
$starLength = $length - 2;
$replacement = $first.str_repeat('*',$starLength).$last;
$newMessage = preg_replace('/\b'.$word.'\b/i', $replacement, $message);
//Update the comment
$sql = "UPDATE ow_base_comment SET message = $newMessage WHERE id = $commentId LIMIT 1";
OW::getDbo()->query($sql);
}
}
Shouldnt your query not be what I placed below, since it wont see now the actual variables in the query. It will technincally just update nothing, cause there is no actual variable set.
$sql = "UPDATE ow_base_comment SET message = '".$newMessage."' WHERE id = '".$commentId."' LIMIT 1";
It's a common error to forget the quotes within PHP.
Right now I access each row and cells of a table like this:
$f = phpQuery::newDocumentFile('test.html');
// access row #1
$o = $f['#id tr:eq(0)'];
$d = phpQuery::newDocument($o);
// get cells from row #1
$arr[0]['c1'] = $d['td:eq(0)'];
$arr[0]['c2'] = $d['td:eq(1)'];
// access row #2
$o = $f['#id tr:eq(1)'];
$d = phpQuery::newDocument($o);
// get cells from row #2
$arr[1]['c1'] = $d['td:eq(0)'];
$arr[1]['c2'] = $d['td:eq(1)'];
I was wondering if there's a more efficient way of doing this? Like maybe if there's a way to find out the last index number then I can probably do something like this:
$f = phpQuery::newDocumentFile('test.html');
$last_index = 10;
for ($i = 0; $i <= $last_index; $i++)
{
$o = $f['#id tr:eq($i)'];
$d = phpQuery::newDocument($o);
$arr[$i]['c1'] = $d['td:eq(0)'];
$arr[$i]['c2'] = $d['td:eq(1)'];
}
Anybody knows how to find the last index (total number of rows in a table) ?
You can use the size() method.
$last_index = pq('#id tr')->size() - 1;
I use php array_rand to select 1 random record from array, Example:
$style_class = array("st_style1","st_style2","st_style3","st_style4");
$random_class = array_rand($style_class, 1);
$div_class = $style_class[$random_class];
The issue is that sometimes it gives a same record several times, and as I use only 4 records it happens quiet often (using "array_rand" is not neccesary) .
Example:
st_style1,
st_style2,
st_style2,
st_style2,
st_style4,
st_style2 ...
Is there a way to solve this issue, so two same record would not get displayed two times in a row.
For example
st_style2, st_style4, st_style2, st_style1, st_style3, st_style2, st_style1 ...
The simpliest solution is to keep track of the latest one and keep calling random until you get something different. Something like:
$style_class = array("st_style1","st_style2","st_style3","st_style4");
$styles = array()
$lastStyle = -1
for($i = 0; $i < 5; $i++) {
while(1==1) {
$newStyle = array_rand($style_class, 1);
if ($lastStyle != $newStyle) {
$lastStyle = $newStyle;
break;
}
}
$div_class = $style_class[$lastStyle]
$styles[] = $div_class
}
Then use the $styles[] array in order. It should not have any duplicates
Basically same as James J. Regan IV's answer, but using a do-while loop:
Set up the array like this:
$style_class = array("st_style1","st_style2","st_style3","st_style4");
$prev_class = -1;
And then, to obtain a random class:
do {
$random_class = array_rand($style_class, 1);
} while ($random_class == $prev_class);
$div_class = $style_class[$prev_class = $random_class];
Edit: Alternative solution, with no loops:
$style_class = array("st_style1","st_style2","st_style3","st_style4");
$random_class = array_rand($style_class);
To obtain a new random class:
$random_class += rand(1, count($style_class)-1);
$div_class = $style_class[$random_class % count($style_class)];
This works as long as the array keys are consecutive integers starting from zero (as is the case if you define it with array() and don't explicitly specify any keys).
Save the last style in a var, then make a loop till the new style differs from last style. And then you will have a different from the last on every execution.