To fetch Random/different records each time - mysql

I have 3 thumbnails which i want to rotate each time a user refresh the page. Therefore want to show different thumbnail each time. I have image name stored in the database. I am using this SQL query to fetch "select thumbnail from tablename order by rand() limit 1". It is fetching one thumbnail perfectly.
The problem is that sometime same images comes up. For example first time i refresh the page thumbnail 1.jpg shows but second time again 1.jpg comes up.
So what i am looking for is to have different thumbnail each time i refresh the page.
Can anyone provide me some suggestions to achieve this ?

this is not possible with random selection, as the same image can come again.
For the effect you want you need to use a cookie to store the last displayed image name and NOT display that image for the next request.
As you have confirmed in your comment you do not want to display the last displayed thumbnail. IN that case you can do this like this using sessions:
// get last shown from session if any
$last_shown = '';
if(isset($_SESSION['last_shown_thumb']))
$last_shown = $_SESSION['last_shown_thumb'];
// select a thumbnail randomly which is not the one last shown
$query = "SELECT thumbnail FROM tablename WHERE thumbnail <> '" . $last_shown . "' order by rand() limit 1";
$res = mysql_query($query);
$row = mysql_fetch_row($res);
$curr_thumb = $row[0];
// set the current to last shown in session
$_SESSION['last_shown_thumb'] = $curr_thumb;

Assuming you are using PHP, you store the previous thumbnail in $current_thumbnail.
Then, you can do it like this
SELECT thumbnail FROM tablename WHERE thumbnail <> '$current_thumbnail' ORDER BY rand() LIMIT 1
Your query just needs to know what thumbnail you are currently displaying so you can filter it out.

Related

I want to set the image to match with Email

I create a table to show a picture of my friend and me but I can't get my friend picture in data_source and show my picture only.
First, I get picture form Datasource of google (Gmail profile picture) and I use filters like this
var empsDS = app.datasources.CurrentUser;
var email = widget.parent.datasource.item.Email;
empsDS.query.filters.PrimaryEmail._equals= email;
empsDS.load();
but program show my picture only
P.S. This is code to get a picture
#datasources.CurrentUser.item.ThumbnailPhotoUrl !== null ? #datasources.CurrentUser.item.ThumbnailPhotoUrl : window.DEFAULT_PROFILE_IMAGE_URL
Image Ex.
enter image description here
The reason it shows only your picture is because your datasource points to only the current user. you need to get the directory and find matches to the emails in order to get the each picture.

Limit 1 page display only 5 data using yii

I have a problem regarding setting limit for specific page. For example i have 8 data. In page 1, only 5 data are allowed to display. the remaining data will display at the next page. How to make this possible to if the total data i get is from database. How to make this condition in sql??
Is not clear what you are asking
but you can limite the row showd in gridView with $dataProvider->pagination->pageSize
eg:
$dataProvider->pagination->pageSize=5;
thank you for the response. i already found an answer..:)
//$i refer to total of page.
$start_from = ($i-1)*5;
$RsPermitSkvLim = RsPermitSkv::model()->findAllBySql("SELECT * FROM rs_permit_skv WHERE id_permit_info = $id_permit_info ORDER BY id ASC LIMIT $start_from,5");
foreach($RsPermitSkvLim as $rsskvinfo) {
//coding here.
}

SSRS - Column Group - Table Horizontally Expanded

There was a requirement, to have data horizontally expanded in the Rows. So I have created Child Row group in Column group of Matrix as shown in this link.
Reference Link
It is working fine. And it displays result as below.
Requirement was : Show Thumbnails of images uploaded for a building as above.
But now the problem is, when there will be number of images, this is going to be expanded horizontally.
I want to repeat this Row after 8 or 10 images.
Any Idea how can I achieve this in SSRS ?
Thank you,
Mittal.
Not quite sure about your requirement, do you want those images wrap in your report, each row has maximum 8 images? If so, we need to make each 8 images into one group. In this scenario, we can create a list. If you have a index field (like a specific id for each image) in your dataset, we can put in the group expression with this:
=ceiling(Fields!Index.Value/8)
If you don't have this kind of index column, we can make it manually. Embed the custom code below:
Dim CountNumber As Integer = 0
Public Shared Previous as Object
Public Function GroupNumber(ByVal category As Object) As Integer
If Category <> Previous then
CountNumber = CountNumber + 1
Return CountNumber
Else
Return CountNumber
End If
End Function
Then replace the group expression with this:
=ceiling(Code.GroupNumber(Fields!Image.Value)/8)
I have tested in my local environment and it works. But I can't share the screenshot due to low reputation.

Mediawiki inline template creation

Is it possible to create a wiki page, where you mark a single piece of text as a placeholder which can be put anywhere else on the wiki?
Let's say I have a wiki page containing a simple list. The first item in list must be always shown in the Main Page but the editing user should not edit two pages for that, just one page.
The list page:
Pineapples
{{SaveThisText|TodaysMeal|Dumplings}}
Beans
Oranges
Main Page:
Today, we'll have {{GetSavedText|TodaysMeal}}
...Main Page will result to "Today, we'll have Dumplings"
I know that it is possible to do this using templates but I want to avoid them, I want to edit the template like it's a part of page.
You can do this without writing any custom PHP, see:
http://www.mediawiki.org/wiki/Extension:Variables
This is definitely possible if you write a MediaWiki extension for it. This means that you could place a hook on GetSavedText and SaveThisText so their behaviour can be customized.
If you have a small wiki, you could just cycle through every page on the occurance of GetSavedText an search for {{SaveThisText|TodaysMeal|. Getting every page is easy:
// get existing pages
$db = wfGetDB ( DB_MASTER );
$results = $db->resultObject ( $db->query(
"select distinct page_title from {$wgDBprefix}page " )
);
$existing_pages = array();
while ( $r = $results->next() )
$title = Title::newFromText( $r->page_title );
$article = new Article ( $title );
$content = $article->getContent();
A more efficient approach would be to place a hook on the update of a page. If SaveThisText is present, you could update a line in a database table.

Optimize 2 mysql queries into one

Using php and mysql 5.x. I currently load a banner image in a certain section of my site like so:
SELECT * FROM banners WHERE section = 1 AND pageid = 2
But if no results found I run a second query:
SELECT * FROM banners WHERE section = 1 AND pageid = 0
Basically what Im doing is trying to find banner images assigned to that section for that page. If no results found then I look for any default banner images in the second query. Is there a better way where I can do this in one query?
EDIT
To clarify a little bit more. I want to check if there is any banners assigned to the page, if not then see if there is any banners assigned to 0 (Default). I dont want a mix of both either it shows all banners assigned to that page or show all banners assigned to pageid 0 and there could be a possibility of multiple rows returned not just one.
ADDITIONAL EDIT
To better explain what this is for. In an admin tool I allow someone to assign a banner image to section on the website. In the admin tool they can select the section and the page they want the banner image to show. They can also set the default banner image(s) for that section. So if there were no banner images assigned to a section by default it will load the banner image(s) assigned to 0 for that section throughout the website. So instead of assigning a default banner image to 50 different pages they can just do it one time and it will load the default banner image or images for that section. Just trying to find a way to do this in a more optimal way, instead of 2 queries could it be done in one?
The OR operator will make the conditional (pageid = 2 OR pageid = 0) return true immediately if just the first value is true and since there is a LIMIT 1 I think it should always fetch one with pageid = 2 first since the order of pageid is DESC and 2 is bigger than 0.
SELECT * FROM banners WHERE section = 1 AND (pageid = 2 OR pageid = 0) ORDER BY pageid DESC LIMIT 1
EDIT: I'm not positive if the ORDER BY is necessary, I'd love to see some comments on that
Maybe it's not the most elegant way, but you can try this:
SELECT *
FROM banners
WHERE section = 1
AND pageid = IF(
(select count(*) from banners where section = 1 and pageid = 2) = 0,
0, 2
);