I am using flat file db, not mysql so I can't use $limit
Function getbyfieldsw()
/*!
* #function getbyfieldsw
* #abstract retrieves records in the database whose field matches the
* given regular expression.
* #param fieldname the field which to do matching on
* #param regex the regular expression to match a field on.
* Note: you should include the delimiters ("/php/i" for example).
* #param orderby order the results. Set to the field name to order by
* (as a string). If left unset, sorting is not done and it is a lot faster.
* If prefixed by "!", results will be ordered in reverse order.
* If orderby is an array, the 1st element refers to the field to order by,
* and the 2nd, a function that will take two take two parameters A and B
* - two fields from two records - used to do the ordering. It is expected
* that the function would return -ve if A < B and +ve if A > B, or zero
* if A == B (to order in ascending order).
* #param includeindex if true, an extra field called 'FFDB_IFIELD' will
* be added to each record returned. It will contain an int that specifies
* the original position in the database (zero based) that the record is
* positioned. It might be useful when an orderby is used, and an future
* operation on a record is required, given it's index in the table.
* #result matching records in an array, or false on failure
*/
function getbyfieldsw($fieldname, $orderby = NULL, $includeindex = false) {
if (!$this->isopen)
{
user_error("Database not open.", E_USER_ERROR);
return false;
}
// Check the field name
if (!$this->key_exists_array($fieldname, $this->fields))
{
user_error(
"Invalid field name for getbyfield: $fieldname",
E_USER_ERROR
);
return false;
}
// If there are no records, return
if ($this->records == 0)
return array();
if (!$this->lock_read())
return false;
// Read the index
$index = $this->read_index();
// Read each record and add it to an array
$rcount = 0;
foreach($index as $offset)
{
// Read the record
list($record, $rsize) = $this->read_record($this->data_fp, $offset);
// See if the record matches the regular expression
// Add the index field if required
if ($includeindex)
$record[FFDB_INDEX_RECORDS_OFFSET] = $rcount;
$result[] = $record;
++$rcount;
}
$this->unlock();
// Re-order as required
if ($orderby !== NULL)
return $this->order_by($result, $orderby);
else
return $result;
}
Function show_record()
function show_record($record){
$month = $record["lmonth"];
$status = $record["lstatus"];
$year = $record["lyear"];
}
if (($status == ON) && ($month >= $current_month) && ($year >= $current_year)){
echo "foo";
}
Call records - here is the problem - this works except for the break; - I need to explode the flat file to read each record individually, then add to the foreach():
$result = $db->getbyfieldsw(lp_month);
$i = 0;
foreach ($result as $item){
show_record($item);
if ($i >= 2)
break;
}
Since this is flat file, when calling the function getbyfieldsw() the file comes flat as a single file, no matter how many records on it, records = 1 or records = 100 is the same at this point.
Break; does not work because all records come as single record - therefore nothing to break.
What I want to do is split/explode the records, count them, and based on my if statement post:
if $i == 0 echo "content1";
if $i == 1 echo "content2";
if $i > 1 echo "content 3";
I got held here for the past 3 days...exhausted all solutions.
HELP glad appreciated
Thanks
Sorry for not helping your code directly, but you should consider switching to SQLITE instead of using your own database system. SQLITE is flat, open-source, efficient and easier to work with. PHP's sqlite module has everything you need. You should check it out!
http://www.sqlite.org/about.html
http://php.net/manual/en/book.sqlite.php
Related
I have a function which contains a query and a percentile calculation (below) on the query's results/collection.
This function is needed in the following way:
I have an Athlete model, with many Tests. Each Test also has many Results. For every result (displayed in a table), I need to display a calculated percentile (which is not stored). It's at this point (the table view) where I need the function to calculate the percentile for every result.
I have detailed the models here. The answer of that post also shows the query used.
If I stick that function in the Result model, it allows me easy access to it from the view in this manner {{$result->getThisRank($test->age, $an_athlete->gender, $result->battery_id, $result->score)}}
If I put it in a Controller, I would need some dynamic and complex way to call a route for each specific Result in the table. Not even sure how to do this.
Question: Is it correct to have this query and calculation in the Result model?
An earlier post here, seems to also go with model.
The calculation performed on the query collection:
for ($i = 1; $i < 100; $i++ ){
$rank = 0.0;
$p = 0.0;
$rank = $i/100 * ($count + 1);
$p = $rank;
//get integer and decimal for interpolation http://onlinestatbook.com/lms/introduction/percentiles.html
$intpart = floor($p);
$fraction = $p - $intpart;
//dd($intpart . ' '.$fraction);
if($fraction > 0){ //part of percentile formula - see article
//test for min array index to not be out of bound. Test negative, most used case.
if($intpart != 0){
$scoreLow = $all_scores[$intpart - 1];
if($intpart == $count){ //test for max array index to not go over bound
$scoreHigh = $all_scores[$intpart - 1];
} else{
$scoreHigh = $all_scores[$intpart];
}
} else{
$scoreLow = $all_scores[0];
$scoreHigh = $all_scores[$intpart];
}
//scoreLow and scoreHigh has been determined, now final step for decimal rank
$scoreHigh = $scoreHigh * 1.0;
$scoreLow = $scoreLow * 1.0;
$rank = ($fraction * ($scoreHigh - $scoreLow)) + $scoreLow;
} else{
$rank = ($all_scores[$intpart - 1] * 1.0);//no decimal rank, plain rank calculation
}
if($sortorder == 'asc'){
$rankings[$i.'th %'] = $rank;
}else{
$rankings[100 - $i.'th %'] = $rank;
}
//$rankings->add(['rank'.$i => $rank]);
}
//dd($rankings);
//reverse rankings
$rev_rankings = array_reverse($rankings);
if ($battery == 111){
dd($rev_rankings);
}
$view_rank = null;
foreach($rev_rankings as $key => $rank){
if($athlete_score == $rank){
$view_rank = $key;
break;
}
if($athlete_score > $rank){
$view_rank = $key;
break;
}
}
return($view_rank);
}
else{
return ('Not available');
}
The type of calculations you describe certainly belong in the domain (i.e. - model). Depending on how you're structuring your application you may place the function within an existing Eloquent model or even as part of a new entity which doesn't extend the ORM. Additionally, I would standardize the type of return value it provides, perhaps restricting it to numerical values or possibly numerical values as well as NULL. Then I would replace in the view (blade template) any NULL values, etc. with "Not available" as I notice at the bottom of your function.
I have an mobile app that request a key in my server, The key structure contains 7 characters as follows:
# + [0-9] + [0-9] + [0-9] + [A-Z] + [A-Z] + [0-9]
#876EU8, #668KI2 .......
Whereas the key initially has seven characters, in this case three numbers, two letters and one number, doing the math this gives a maximum of 676,000 keys.
To gerate this keys I'm using this code in PHP:
function generateRandomString($length = 2) {
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$randomKeyNumber = rand(100,999);
$randomKeyLetter = generateRandomString();
$randomKeyLast = rand(0,9);
$randomKey = "#".$randomKeyNumber.$randomKeyLetter.$randomKeyLast;//Returns a key like #876TG9
The next code check if the key exists inside the database, If exists he random another key, if not he insert the key in database and return this key to my app
This code works perfectly, but assuming the system has already generated a total of 650,000 keys, in the case of this code it would always generate the same keys, and the likelihood of it generate a key that does not exist yet is very small.
How can I solve this problem and avoid future problems? (There is no problem in creating the keys in an orderly manner, eg 000AA0, 000AA1, 000AA2, 000AA3 .... 999ZZ9)
What you can do is make a PDO::query() to issue a SELECT COUNT(*) or simply a SELECT * statement with all the keys you already have added, and then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned (i.e. in this case, all of them)
This is a manual example
<?php
$sql = "SELECT COUNT(*) FROM Keys";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* Issue the real SELECT statement and work with the results */
$sql = "SELECT name FROM fruit WHERE calories > 100";
foreach ($conn->query($sql) as $row) {
print "Name: " . $row['NAME'] . "\n";
}
}
/* No rows matched -- do something else */
else {
print "No rows matched the query.";
}
}
$res = null;
$conn = null;
?>
This is the code you need for your case:
<?php
$sql = "SELECT * From Keys";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* and then you get the id of the last one on the list, and to that one you add 1 */
$last_id = $conn->lastInsertId();
$new_id = $last_id + 1;
/* then you insert that in some place inside the key itself, that way you don't need to worry than two keys can be equal */
}
else {
/* No rows matched, just create a key and add to the database here */
}
}
¿>
Alternatively you can make a query SELECT statement combined with the countRows in PDO, it doesn't work all the times in the portable apps and/or databases, but like we don't know more about your app we can't know if this is gonna work.
PS. Don't use rand(). Use mt_rand() instead. It is more efficient with the resources of the server ;)
I am using flat file db, not mysql so I can't use $limit
I need to limit the number of records to 1, if more than 1 then echos something else:
$result = $db->getall(lmonth);
foreach($result as $item)
show_record($item);
}
Function getall()
/*!
* #function getall
* #abstract retrieves all records in the database, each record in an array
* element.
* #param orderby order the results. Set to the field name to order by
* (as a string). If left unset, sorting is not done and it is a lot faster.
* If prefixed by "!", results will be ordered in reverse order.
* If orderby is an array, the 1st element refers to the field to order by,
* and the 2nd, a function that will take two take two parameters A and B
* - two fields from two records - used to do the ordering. It is expected
* that the function would return -ve if A < B and +ve if A > B, or zero
* if A == B (to order in ascending order).
* #param includeindex if true, an extra field called 'FFDB_IFIELD' will
* be added to each record returned. It will contain an int that specifies
* the original position in the database (zero based) that the record is
* positioned. It might be useful when an orderby is used, and an future
* operation on a record is required, given it's index in the table.
* #result all database records as an array
*/
function getall($orderby = NULL, $includeindex = false)
{
if (!$this->isopen)
{
user_error("Database not open.", E_USER_ERROR);
return false;
}
// If there are no records, return
if ($this->records == 0)
return array();
if (!$this->lock_read())
return false;
// Read the index
$index = $this->read_index();
// Read each record and add it to an array
$rcount = 0;
foreach($index as $offset)
{
// Read the record
list($record, $rsize) = $this->read_record($this->data_fp, $offset);
// Add the index field if required
if ($includeindex)
$record[FFDB_IFIELD] = $rcount++;
// Add it to the result
$result[] = $record;
}
$this->unlock();
// Re-order as required
if ($orderby !== NULL)
return $this->order_by($result, $orderby);
else
return $result;
}
Function show_record()
function show_record($record){
$month = $record["lmonth"];
$status = $record["lstatus"];
$year = $record["lyear"];
}
if (($status == ON) && ($month >= $current_month) && ($year >= $current_year)){
echo "foo";
}
I tried to use break but it comes back 0(zero) records.
I tried using $i = 0...but it returned all or nothing
Any ideas?
Thanks
What about something like this?
function getall($orderby = null, $includeindex = false, $limit = null) {
...
if ($orderby !== null) {
$result = $this->order_by($result, $orderby);
}
if ($limit) {
return array_slice($result, 0, $limit);
}
return $result;
}
SOLUTION
Print_r did the trick, and I was using echo:
print_r (show_record($row));
Here is how the final code is working for me:
$result = $db->getall(lp_month,lp_year);
$i = 0;
foreach ($result as $row){
print_r (show_record($row));
if ($i >= 1)
break;
$i++;
}
Now looking forward to solve other minor problems, thanks
i have a function (below) which is used in my mysql abstraction class and converts table names in fields like "tableName.fieldName" and replaces them with the specified variables (it's useful for joins). the array of fields is very mixed, and so i need it to support recursion so that it can change the table names in an array(array(array("tableName.fieldName"))) but also for the standard array("tableName.fieldName","tableName.field2Name",...)
however, after debugging, i see that the variables $i and $fields_arr are maintaining the same values, even though i pass on a new value for $fields_arr and $i is set at the begining of the loop. how can i make this work so that $i and $fields_arr take the new values i pass for them?
/**
* #param mixed $fields_arr standard array ("table.field1","table.field2",...)
* #param mixed $tables_and_vars eg. ("table1" => "tableVar1", "table2" => "tableVar2", ...)
* replaces all tablenames with desired tablevarnames
*/
private function tablesToTableVars($fields_arr, $tables_and_vars) {
// loop through every string
$numFields = count($fields_arr);
for($i = 0; $i < $numFields; $i++) {
$field = $fields_arr[$i];
if(is_numeric($field)) continue; // don't replace numbers
if(is_array($field)) {
$fields_arr[$i] = $this->tablesToTableVars($field, $tables_and_vars); // *** RECURSION ***
} else {
$tableNameLen = strpos($field, "."); // pos of period in string
if(strpos($field, ".") === false) continue; // don't replace fields that dont have tablenames
$searchTableName = substr($field, 0, $tableNameLen); // take 'table' from 'table.field'
// see if field contains a table
foreach($tables_and_vars as $tableName => $tableVar) {
if($searchTableName === $tableName) { // if it is the table name we're looking for
$fields_arr[$i] = $tableVar . substr($field, $tableNameLen); // change it to the variable name
break;
}
}
}
}
return $fields_arr;
}
can't use integer indexes for an associative array =)
I have never had a need to do a random SELECT on a MySQL DB until this project I'm working on. After researching it seems the general populous says that using RAND() is a bad idea. I found an article that explains how to do another type of random select.
Basically, if I want to select five (5) random elements, I should do the following (I'm using the Kohana framework here)?
<?php
final class Offers extends Model
{
/**
* Loads a random set of offers.
*
* #param integer $limit
* #return array
*/
public function random_offers($limit = 5)
{
// Find the highest offer_id
$sql = '
SELECT MAX(offer_id) AS max_offer_id
FROM offers
';
$max_offer_id = DB::query(Database::SELECT, $sql)
->execute($this->_db)
->get('max_offer_id');
// Check to make sure we're not trying to load more offers
// than there really is...
if ($max_offer_id < $limit)
{
$limit = $max_offer_id;
}
$used = array();
$ids = '';
for ($i = 0; $i < $limit; )
{
$rand = mt_rand(1, $max_offer_id);
if (!isset($used[$rand]))
{
// Flag the ID as used
$used[$rand] = TRUE;
// Set the ID
if ($i > 0) $ids .= ',';
$ids .= $rand;
++$i;
}
}
$sql = '
SELECT offer_id, offer_name
FROM offers
WHERE offer_id IN(:ids)
';
$offers = DB::query(Database::SELECT, $sql)
->param(':ids', $ids)
->as_object();
->execute($this->_db);
return $offers;
}
}
If not, what is a better solution?
That approach will work, as long as your offer_id's are sequential and all continuous - if you ever remove an offer, you might have gaps in the id's that would then be a problem.
I've read the same things about the MySQL rand() function on large table sets, but I would think you could do it faster by counting the table rows, then using PHP's built in rand(0, count) to generate a few index ID's you can grab in a SELECT. I suspect it would have the same affect but without all the performance concerns.