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;
Related
I am saving some input data into database. Using for loop I am trying to save the input data in different rows.
for ($i=0; $i < $day ; $i++) {
$activity->event_id = $request->event_id;
$activity->created_by = $id;
$activity->index = $request->index[$i];
$activity->activity_title = $request->activity_title[$i];
$activity->activity_details = $request->activity_details[$i];
$activity->save();
}
The problem is, it only saves the last value. Suppose if day = 2 it saves the information of the second index of my arrays. Ignores the first index value. Where am I doing wrong?
Instantiate your $activity within the loop, e.g.
for ($i=0; $i < $day ; $i++) {
$activity = new Activity();
$activity->event_id = $request->event_id;
$activity->created_by = $id;
$activity->index = $request->index[$i];
$activity->activity_title = $request->activity_title[$i];
$activity->activity_details = $request->activity_details[$i];
$activity->save();
}
Right now, you're just overwriting the same instance every iteration, which explains why only the last value remains.
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've created a for loop, but it does not print out the output according to the for loop conditions.
for( $c=0; $c < $total; $c++ )
{
$latestID = AccessControlEntry::orderBy('AccessControlID', 'desc')->first();
$numberID = explode("AC", $latestID->AccessControlID);
$numberID[1] = $numberID[1] + 1;
$newID = "AC";
$newID = $newID.$numberID[1];
}
I grabbed the last ID value (AccessControlID) from the database, and subsequently add 1 ($numberID[1] + 1) for every new entry in AccessControlID.
For eg: $total = 3.
From my understanding of for-loops logic, shouldn't it print out 2 sets of $newID?
Can anyone tell me where/what did I went wrong?
The solution is to bring outside the loop the lines of code which extract out from the DB the value.
$latestID = AccessControlEntry::orderBy('AccessControlID', 'desc')->first();
$numberID = explode("AC", $latestID->AccessControlID);
for( $c=0; $c < $total; $c++ ) {
$numberID[1] = $numberID[1] + 1;
$newID[] = $numberID[1];
}
print_r($newID);
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