Lunarpages Web Hosting Forum

Author Topic: Random Item script  (Read 6480 times)


Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #31 on: March 08, 2004, 03:02:05 PM »
Thanks Kata, I'm having a great day, so far.

Ripta,  here is what I have with adding what you posted:

Quote

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

use DBI;

print "Content-type:text/html\n\n";
print "Before connection.\n";

$dbhost = "localhost";
$dbname = "erotic6_items";
$dbtable = "Item_Rotate";
$dbuser = "name";
$dbpass = "pass";

$dbh = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost",$dbuser,$dbpass) or dienice("Can't connect: ",$dbh::errstr);
print "Now connected.\n";

$sth = $dbh->prepare("SELECT * FROM $dbtable") or dienice("Can't select from table: ",$dbh->errmsg);
$sth->execute;
my $index = int(rand($sth->rows));
my $row;
if ($index) {
   for (my $counter = 0; $counter < $index; $counter++) {
       $row = $sth->fetchrow_hashref;
   }
}

$row->{SKU}
$row->{Name}
$row->{Price}
$row->{Image}
$row->{Link}

$dbh->disconnect;
print "Disconnected from database.\n";

sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}



this doesn't seem to print anything. I get a error "syntax error at mrt.cgi line 30, near "$row" "

I have also tried differnt ways of printing such as:

print "$row{SKU}\n";

and

print "$SKU - $Name - $Price - $Image - $Link\n";

but only get the "before connect,connect, disconect" but nothing else.  Did I add what you posted wrong?  I don't mean to keep bothering you, but if you have some time, please let me know.  Thanks for all your help.

Thanks again,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline Ed

  • Berserker Poster
  • *****
  • Posts: 5156
    • Joke A Whenever
Random Item script
« Reply #32 on: March 08, 2004, 03:13:01 PM »
$row->{SKU}
$row->{Name}
$row->{Price}
$row->{Image}
$row->{Link}

I've never used perl to connect to SQL so I"m guessing here. But what if you were to assign one of those to a var:

$test = $row->{SKU};
(Each one of those lines should have a ; at the end should it not?

then print $test;

Also, if that doesn't get you anything.. manually enter data into the DB and hard code the row # it should access (knowing already that there is content there).

Also, try printing out the variable for the row number after you establish it so you can manually see if there is even any data in there!

- Ed

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #33 on: March 08, 2004, 03:16:24 PM »
Hey, Happy B'day! :D

About the script, you'll need to print the results out. So, all you need to do is change:

Code: [Select]

$row->{SKU}
$row->{Name}
$row->{Price}
$row->{Image}
$row->{Link}


into something like:

Code: [Select]

print $row->{SKU};
print $row->{Name};
print $row->{Price};
print $row->{Image};
print $row->{Link};


:thumb:
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #34 on: March 08, 2004, 03:22:21 PM »
Also,

Code: [Select]

print "$row{SKU}\n";


did not work, because $row{SKU} takes an element a hash instead of a hashref (hash reference). The -> (which looks like an arrow), un-references (the proper term is "dereferencing")  the hash.

Alternatively, if you want to use:

Code: [Select]

print "$SKU - $Name - $Price - $Image - $Link\n";


instead of the nasty looking $row->{SKU} and stuff, you could do so by changing:

Code: [Select]

$row = $sth->fetchrow_hashref;

into something like:

Code: [Select]

($SKU, $Name, $Price, $Image, $Link) = $sth->fetchrow_array;


Hope it helps! :thumb:
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #35 on: March 08, 2004, 03:23:32 PM »
Quote

print $row->{SKU};
print $row->{Name};
print $row->{Price};
print $row->{Image};
print $row->{Link};


That's it.  :yey:

Thanks so much.  I last question.  The Image is a link to the image.  To show the image I would use:

print "<img src=\"{Image}\">";

Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #36 on: March 08, 2004, 03:25:57 PM »
Ok so by chnaging it into a

$sth->fetchrow_array;

I could show the image like this:

print "<img src=\"$Image\">";

Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #37 on: March 08, 2004, 03:30:44 PM »
One more thing.  I am asuming that I can change the way it looks, prints out, by using html like

print <<EndOfHTML;

and use the html to get it to look the way I want.

Correct?

Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #38 on: March 08, 2004, 03:41:14 PM »
Quote from: MagicMike304
One more thing.  I am asuming that I can change the way it looks, prints out, by using html like

print <<EndOfHTML;

and use the html to get it to look the way I want.

Correct?

Thanks,
Mike


Yep! :D Instead of doing five prints:

Code: [Select]

print <<EndOfHTML;
$row->{SKU}
$row->{Name}
$row->{Price}
$row->{Image}
$row->{Link}
EndOfHTML


will do the same. And you can use HTML in it. Just remember that both EndOfHTML need to be the same case and that the last EndOfHTML needs to start at the first column. That's where I usually make a mistake. :)
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #39 on: March 08, 2004, 03:45:01 PM »
Ripta, thanks so much for all the help.  I really learned a lot from this.  That is the main reason I wanted to write the script myself instead of getting one on hotscripts or somewhere.  Thanks again for all the help.

Thanks,
Mike :thumb:
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #40 on: March 08, 2004, 03:50:01 PM »
No problem, Mike. That's what I'm here for :D. Enjoy your birthday :).
GetAFreelancer! (This service is not affiliated with Lunarpages)


Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #42 on: March 08, 2004, 10:16:01 PM »
I found an alternate solution that is faster on large number of records, using the MySQL function RAND(). To use this on the script above, simply replace:

Code: [Select]

$sth = $dbh->prepare("SELECT * FROM $dbtable") or dienice("Can't select from table: ",$dbh->errmsg);
$sth->execute;
my $index = int(rand($sth->rows));
my $row;
if ($index) {
for (my $counter = 0; $counter < $index; $counter++) {
$row = $sth->fetchrow_hashref;
}
}


with:

Code: [Select]

$sth = $dbh->prepare("SELECT * FROM $dbtable ORDER BY RAND() LIMIT 1") or dienice("Can't select from table: ",$dbh->errmsg);
$sth->execute;
my $row = $sth->fetchrow_hashref;


It's faster and shorter. However, there has been reports that this does not work on if the servers run Windows. But since we run Linux all the way, there shouldn't be any problem :D. I even tested it on LP's eros server.

Of course, you can still use $sth->fetchrow_array instead of $sth->fetchrow_hashref and everything should work out alright.
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #43 on: March 09, 2004, 04:01:02 AM »
Thanks Ripta.  I will try this out.  It seems that this will work correctly with ot returning a "0" becuase the table is being sorted to a random "1".  I have been using the fethcrow_array in my script.  Just becuase I can manipulate the print out easier than using hashes.  I question about the print out.  In the HTML part, what is the html code for making the print out from:

Quote

aaaaaa

aaaaaa

aaaaaa


to this

Quote

aaaaaa
aaaaaa
aaaaaa


I can't seem to get rid of the spaces.  I tried using the <p style="margin-top: 0; margin-bottom: 0"> but it didn't work.  What is the correct way to do this?  Here is what I have.

Quote

print <<EndOfHTML;
<p style="margin-top: 0; margin-bottom: 0"><center><a href="$Link"><img border="0" src="$Image"></a></p>
<p style="margin-top: 0; margin-bottom: 0"><center><font size="2"><a style="text-decoration: none" href="$Link">$Name</font></a></p>
<p style="margin-top: 0; margin-bottom: 0"><center><font size="2">$SKU  -  \$$Price</font></p>
EndOfHTML



Thanks again for all your help.
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Priest

  • Guest
Random Item script
« Reply #44 on: March 09, 2004, 04:37:39 AM »
You can take out the multiple <p> tags and just use one at the beginning, and then use <BR> to represent a new line.

Code: [Select]
print <<EndOfHTML;
<p style="margin-top: 0; margin-bottom: 0">
     <center><a href="$Link"><img border="0" src="$Image"></a><BR>
     <center><font size="2"><a style="text-decoration: none" href="$Link">$Name</font></a><BR>
     <font size="2">$SKU - \$$Price</font><BR>
</p>
EndOfHTML