PDA

View Full Version : Spotlight script [SELECT ONLY VIA USERS IN DATABASE]



Independent
22-07-2008, 07:15 PM
Right, I was bored and got "inspired" by Jack's thread.. This collects the image from Habbo still, but spotlights a random Habbo inserted into the database table.



<?php
// MySQL Connect code.

$grab = mysql_query("select * from `habbos`");
$amount = mysql_num_rows($grab);

$random = rand(1, $amount);

$getusers = mysql_query("SELECT * FROM `habbos` WHERE `id`='$random'");
$showuser = mysql_fetch_array($getusers);
$showusername = $showuser['username'];

echo '<img border="0" src="http://www.habbo.' . $showuser['habbohotel'] . '/habbo-imaging/avatarimage?user='. $showuser['habboname'] .'&action=&direction=4&head_direction=3&gesture=sml&size=l&img_format=png"><br />Hello, '.$showuser['habboname'] .' plays Habbo.' . $showuser['habbohotel'] . '</a>';

?>
MySQL Code:


CREATE TABLE IF NOT EXISTS `habbos` (
`id` int(3) NOT NULL auto_increment,
`habboname` varchar(30) NOT NULL,
`habbohotel` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
If you wish to insert users, here's the SQL code;



INSERT INTO `habbos` (`id`, `habboname`, `habbohotel`) VALUES
(1, 'Xedon', 'co.uk'),
(2, 'Nooblah', 'co.uk');
Like that..

Hope this helps, I'd have made a inserter as well but I'm not sure if people would have wanted that, this is best for staff pages, like "Random staff".

I'll make an inserter file if you request it.

I know this is badly coded, but I made it in 10mins.

Edit: This may show the same image twice, but after another refresh it shouldnt.

Moh
22-07-2008, 07:38 PM
Theres only one problem with your random.

What if you have to delete an entry, that means the id's could be like:

id | habbo
1 | Habbo 1
2 | Habbo 2
4 | Habbo 3
5 | Habbo 4
7 | Habbo 5

So if the random picks up id 3, there's no entry for it.

Decode
22-07-2008, 07:41 PM
Theres only one problem with your random.

What if you have to delete an entry, that means the id's could be like:

id | habbo
1 | Habbo 1
2 | Habbo 2
4 | Habbo 3
5 | Habbo 4
7 | Habbo 5

So if the random picks up id 3, there's no entry for it.

You could use mysql_affected_rows() to check if it exists, then if it doesn't choose another random one.

--ss--
22-07-2008, 07:47 PM
Wouldn't it just be easier to have the query as:


$random = mysql_query("SELECT habbonames FROM habbos ORDER BY RAND() LIMIT 1");
$habbo = mysql_fetch_array($random);
With the array variable $habbo[0] being the actual Habbo?

Source
22-07-2008, 07:59 PM
I was just about to post exactly the same thing ss.

Dentafrice
22-07-2008, 08:07 PM
I was too.. LOL

Decode
22-07-2008, 08:35 PM
Wouldn't it just be easier to have the query as:


$random = mysql_query("SELECT habbonames FROM habbos ORDER BY RAND() LIMIT 1");
$habbo = mysql_fetch_array($random);
With the array variable $habbo[0] being the actual Habbo?
Im not sure im right, but when you use rand it makes a seprate temp table, which can slow down the execution time of a script if there are too many rows in the table.

--ss--
22-07-2008, 08:42 PM
Im not sure im right, but when you use rand it makes a seprate temp table, which can slow down the execution time of a script if there are too many rows in the table.
Yep, you seem to be correct. But then again you'd have to have quite a large table for it to make much of an difference and seeing as it's going to be to show the name off a staff member or something it shouldn't have that many entries anyway ;).

Moh
22-07-2008, 08:59 PM
Im not sure im right, but when you use rand it makes a seprate temp table, which can slow down the execution time of a script if there are too many rows in the table.
Looks like I may have to redo our random spotlight. We have 3,374 entries :P

lSnowie
22-07-2008, 10:35 PM
I think this is much more appropriate for a spotlight script

http://clubhabboforum.net/showthread.php?p=1021573&posted=1#post1021573

Independent
22-07-2008, 11:02 PM
Thanks for your help.



<?php
// MySQL Connect code.

$grab = mysql_query("select * from `habbos`");
$amount = mysql_num_rows($grab);

$random = rand(1, $amount);

$random = mysql_query("SELECT habbonames FROM habbos ORDER BY RAND() LIMIT 1");
$showuser = mysql_fetch_array($random);
$showusername = $showuser['username'];

echo '<img border="0" src="http://www.habbo.' . $showuser['habbohotel'] . '/habbo-imaging/avatarimage?user='. $showuser['habboname'] .'&action=&direction=4&head_direction=3&gesture=sml&size=l&img_format=png"><br />Hello, '.$showuser['habboname'] .' plays Habbo.' . $showuser['habbohotel'] . '</a>';

?>
+REP for trying to help, which is everyone.. but shane (I think that's his name) got there first

Query is *bobba*'d, I'll fix tomorrow morning.. I'm going to bed, night habboxforum xD

Decode
23-07-2008, 08:46 AM
Thanks for your help.



<?php
// MySQL Connect code.

$grab = mysql_query("select * from `habbos`");
$amount = mysql_num_rows($grab);

$random = rand(1, $amount);

$random = mysql_query("SELECT habbonames FROM habbos ORDER BY RAND() LIMIT 1");
$showuser = mysql_fetch_array($random);
$showusername = $showuser['username'];

echo '<img border="0" src="http://www.habbo.' . $showuser['habbohotel'] . '/habbo-imaging/avatarimage?user='. $showuser['habboname'] .'&action=&direction=4&head_direction=3&gesture=sml&size=l&img_format=png"><br />Hello, '.$showuser['habboname'] .' plays Habbo.' . $showuser['habbohotel'] . '</a>';

?>
+REP for trying to help, which is everyone.. but shane (I think that's his name) got there first

Query is *bobba*'d, I'll fix tomorrow morning.. I'm going to bed, night habboxforum xD


For Jack that will still take a long time to execute, i would do;



$ammount = mysql_query ( "SELECT * FROM habbos" );
$ammount = mysql_affected_rows ();
$randomnumber = rand ( "1", $ammount );
$mysql = mysql_query ( "SELECT * FROM habbos WHERE id = '" . $randomnumber );
$showuser = mysql_fetch_array ( $mysql );

echo '<img border="0" src="http://www.habbo.' . $showuser['habbohotel'] . '/habbo-imaging/avatarimage?user='. $showuser['habboname'] .'&action=&direction=4&head_direction=3&gesture=sml&size=l&img_format=png"><br />Hello, '.$showuser['habboname'] .' plays Habbo.' . $showuser['habbohotel'] . '</a>';

Luckyrare
23-07-2008, 09:58 AM
You may be better using the MySQL rand function rather than the PHP function

Would be less code, worth a try...

Decode
23-07-2008, 10:03 AM
If you use Mysql rand its slower to execute.

Dentafrice
23-07-2008, 12:24 PM
The first way is actually better if you have a larger database, RAND is slow on larger rowsets.

It only returns ad hoc random results anyway.

Luckyrare
23-07-2008, 04:34 PM
Well if you wanted to remove one of the Habbos, and didnt replace it with the same id - it would be not found...

Dentafrice
23-07-2008, 04:36 PM
Well yeah, that is using the first method with auto increment.

You should be able to use RAND() just fine here without it slowing down, but on bigger applications.. no.

Want to hide these adverts? Register an account for free!