Yii Datatprovider remove rows - php

I am in the position where I need to remove records form a dataprovider. Long story shot, I have encrypted data in my db that is decrypted using the afterfind method. I need to ensure the decrypted string is 20 characters long and show the result in a cgrid view.
I have tried the visible option, but this hides the data on a per column basis. I have tried the rowCssClassExpression which successfully hides the rows on the screen using display:none, however, it still shows 1 of 1200 results even though only 10 match and also page 1 has one result, page 2 no results, page 3 2 results etc.
Currently I am able to get this working by doing a cdbcommand queryAll and then looping through and calling the object like so:
foreach($data as $key=>$d)
{
$lengthCheck = Data::model()->findByPk($d['id'])->checkLength;
if($lengthCheck !== true)
{
unset($data[$key]);
}
etc.
I can then use the resulting array in an arrayDataProvider, so effectively I can get the information I need using this methods, however, It is taking over 2 seconds per record, so effectively this would be over 3 minutes for 100 records.
Does anyone have an idea of how I could do this in a smarter/faster way?

Related

inRandomOrder() query with ajax return same elements

I have a query with Laravel
$query = Work::inRandomOrder()->where('orientation', $request->get('orientation')->paginate(11);
And I have a "load more" button who call 11 others works with each click.
But, I would like display my data in a random order. But with this query, he repeats to me several data. So in my list of works, I have 2 or 3 times the same and obviously, it's not good. Is there a way with RandomOrder () to avoid duplicates?
Thank you
I believe data is randomized each time the script is loaded, including each time loading dynamicaly 11 more data fields. One way to achieve this it to randomize, store the data in a user's session variable and then get next elements from it. However this might be very heavy.
I would go to a some easily predictable mathemtics, which will look 'random' to the end-user. For example you can get each N-th entry of a query like 1-st, 5-th, 9-th, 13-th ... if N=4 and store just the number N in the user's session.

How to force propel to return Null after last page in pagination?

I use Propel to get result from MySQL and use paginate.
When for a result that have for example 100 rows and I use
->paginate(6, 20);
it must return NULL because we past the last page but it returns content of the last page even I use paginate(10000, 20)!!!!
In the other word how can I understand I passed the last page if I can not force it to return NULL for pages after last page?
Actually you can count the number of total elements by a simple count() function and then calculate how many page you have and what is your last page.
If you go inside of the php files of propel you would see that when the result is empty it feeds the last page as the result and returns it back.
I don't think that you have any other option.

echo specific item from multidimensional array

Been hammering away at this for weeks. I think it's actually simple...but I just can't get it.
I want to pull info from the database and use it in divs later on in my html. I've been successful in other cases looping through and displaying the full results of a query in the past. In this case I just want to echo a specific team name or team ID in my html. All the data gets used...so I'd like to just have one query, but the data gets used in different divs in different places...so being able to echo specific parts of the query is important.
The query I have returns 10 rows and 4 columns.
<?php
$playoffs = mysqli_query($con, "SELECT playoffseed, ttName, teamID, ttsUID
FROM standings
WHERE ttsUID = $season_view
AND playoffseed > 0
ORDER BY playoffseed
LIMIT 10");
?>
Now, I think I've learned that this query returns a result set and not an array. It needs to be processed using some php to turn it into an array. Correct? And given the fact it's got multiple columns and rows once it is processed it would be a multidimensionsal array. Ok I've scoured this site and google...tried many ideas using mysqli_fetch_array, mysqlifetch_assoc, and any other mysqli_fetch that I'd come across.
In my mind anyway, after processing the query with some php I'm thinking the array would look like this...
playoffseed ttName teamID ttsUID
1 Buffalo 13 1993
2 Miami 19 1993
3 Detroit 8 1993
4 Denver 3 1993
5 Chicago 26 1993
...and so on. 10 rows total
After that I'd like to be able to call on (echo) various items from the above in my html. Let's say in one div I'd like to be able to echo the ttName "Detroit" and then to call and image associated with the teamID I'd like to be able to call that too (it'd be something like this in my html...
IMG SRC="../images/<?php echo "teamID" ?>.jpeg
...where in this case Detroits image is "8.jpg").
How can that be done? How do I correctly fetch the array and then echo a specific item of data from it?
I'm thinking if the above is an associated array I'd be able to echo something like this to return "Detroit"...
echo $playoffs[3]['ttName'];
[3] = 3rd row and ['ttName'] = column. no? I realize that [3] would actually refer to the 4th row if it's actually pointing to rows that start with zero and not one, but I'd like to be able to call on the item by using the "playoffseed" identifier, but I'm just too confused at this point.
I already feel I've made this question too confusing in itself. Thanks a ton for your help.
kevinabelita:
this could shed some light to you us2.php.net//manual/en/mysqli-result.fetch-assoc.php
OP:
been there a 100 times. i'm just not getting something. long story short in case the above is too much....take query and process it with php to create an array...then echo....say..."detroit" from that array
And still didn't notice this simple way?
while ($row = mysqli_fetch_assoc($playoffs)) {
$values[]=$row;
echo $row["ttName"];
//print_r($row); // if you want to see all the data this row contains
}
mysqli_free_result($result);
}
Now you can use $values with an index like you wish. i.e.
echo $values[0]["ttName"];
Edit
I mistakenly was trying to execute the query again. Fixed in the code above. Now for a little explanation of what you are confused with. Here's how the flow goes roughly
Connect to the database
Prepare/Run a query (mysqli_query in this case)
Store the result resource In a variable ($playoffs in this case)
Either fetch all rows from that result all together or fetch one by one. In this case mysqli_fetch_assoc in a loop is fetching the rows one by one till all of the result set has been fetched.
Provide that fetched row as an array for you to use, in that case its stored in $row variable.

Using mysqli query to display second query while still displaying first query

I would like to have two separate queries. The first query I would like to display a bunch of numbers in descending order in a table (pretty simple to do). Then what I would like to do is a second query which for each of those matching numbers I would like to display the corresponding data. The reason I want to do it separately instead of just combing it all into one query is so that I can display all of the results of the first query whether it has data or not. IE:
1 - (blank)
2 - I have data
3 - I also have data
4 - (blank)
5 - I have more data
So far the code I am using is:
// Perform number query
$number_query = mysqli_query($con,$num_query);
while($numbers = mysqli_fetch_array($number_query)) {
// Do data query
$data_query = mysqli_query($con,$d_query);
while($data = mysqli_fetch_array($data_query)) {
if ($numbers['number'] == $data['number']) {
echo $numbers['number'];
echo $data['content'];
echo '<br>';
}
else {
echo $numbers['number'];
echo '<br>';
}
}
}
I dont think I'm taking the right approach but it does seem that I am getting partially the right results as it is displaying the data next to the numbers with content. The problem is that for each piece of data in the database the $numbers['number'] is being replicated. So if $data['content'] has 5 database entries then each entry of the $numbers['number'] is being replicated 5 times.
-------Edit-----
Ok I realized why my question wasnt making sense (maybe). I'm going to use an example of these tables:
Table 1
unique_id | number | identifier | etc
Database Table 1 might be updated once a month, and all the numbers and identifiers must be visible on the page at all times. The query needs to be kept separate because it is used multiple times (more then 10) on other pages so I've included it outside of the page in a header file.
Table 2
unique_id | number | description | etc.
Database Table 2 needs to be referenced via the number columns, this content is changing on an interval of about every 10-15 minutes, now Table 1 might only have 50 rows of data while Table 2 probably has close to a 100+ new rows a day. The query is embedded on the page because it will be the only page that actually uses this specific query.
The idea with the layout is something like this:
<table>
<tr>
<td>Number & Identifier</td> (taken from Table 1)
<td>Description</td> (content from table 2)
<td>(more content from table 2)</td>
etc
So looking back at the top with my question is the problem is that for every description and other content that is being displayed on the page with the corresponding number and identifier information, all the number and identifiers without descriptions or content are being replicated the same amount of times as there are entries with descriptions and content.
If I remove the else conditional statement then the issue becomes that the blank entries do not display at all, which doesnt meet my requirements. The reason that these blank entries need to be in there is that this page can be printed off, so people not using computers can fill out the blank fields with pen/pencil and it can be manually entered in after the fact.
I do understand your question now, but i still think that the solution using join as i posted earlier is your best bet.
http://www.sqlfiddle.com/#!2/26854/2

Split data received into separate fields

Problem : Data supplied is not ok for me to handle directly.
Description : I'm using curl to get data I need from another website.
However the data supplied on this website is formatted in a way that I need to split it to be able to work with it.
Example:
I'm scraping a div (soccer competition), but the match is supplied in one field, I need to have the 2 teams separated into 2 fields.
MysqlDB Field holds after the scrape:
team 1 - team 2
I need to separate that DB info into 2 columns
preg_match would be able to do this, but there are many opponents, so its a hassle like this.
Right now, I added (manually which is a pain) a team_id column and added myteam to the games which involve my team to the db table and did a where team_id = myteam
This way I can at least sort the competition table and just get my teams games.
This is the data I'm talking about getting with curl:
$value['Wedstrijd'] = htmlentities($value['Wedstrijd'], ENT_QUOTES);
So right now I'm scraping the field and putting it in the db.
I'm wondering, how can I separate the content of $value['Wedstrijd'] into 2 writes.....
Is this even possible?
I'm not able to post the entire code here, the formatting gets screwed.
Using substr and strrpos I was able to get the first team out the field,
I thought setting it to -1 would give me the other way around, which it actually does, but it doesn't go back till the - symbol, it just gives me 1 letter/symbol then.
substr($wedstrijd,0,strrpos($wedstrijd,'-')); this would return the first team, but I'm not sure how to use this to get the team after the - symbol.
use strpos + substr or preg_match

Categories