Jump to content

Must be something really small that i can see missing!


raDiO_acTivE

Recommended Posts

Hello, i have been working on a script for a while and there is this one part which is causing an annoyance to me.

 

I have made a log and when ever my cron goes off (each week) i insert three sets of data into that log. Then i want each log to display in the table.

 

First off the code so you may understand a bit better.

 

<?php
$select = $db->query('SELECT * FROM `raffle`');

$that = $db->fetch_row($select);
$win = rand(1,$this);
$win1 = sprintf("SELECT `userid` FROM `raffle` WHERE `id` = '%d'",$win);
$win2 = $db->query($win1);
$user = $db->fetch_row($win2);


$query = "select `username` from `users` where `userid`='{$user['userid']}'";
$result = $db->query($query); 

$earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",
$that['pot'],
$user['userid']);
$db->query($earnings);

event_add($user['userid'],"You won the weekly lottery and were credited \${$that['pot']}",$c);  

$insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES ('', unix_timestamp(),%u,%d)",
$result,
$that['pot']);
$db->query($insert);


$db->query("TRUNCATE TABLE `raffle`");
$db->query("UPDATE `users` SET `lottery` = 0");

?>

That is part of the cron.

 

<?php
function view()
{
global $ir,$h,$userid,$db;



$table = "
<div align='center'>
	<h1>Previous Raffle Winners</h1></center>
<table width='70%' class='table' align='center' border='1' cellpadding='2' cellspacing='1'>
<tr>
	<th width='15%'>Date</th>
	<th width='8%'>Winner</th>
	<th width='15%'>Amount</th>
</tr>
";
echo($table);
$view = db->query("SELECT * FROM `rafflelog`  ORDER BY rDATE DESC");
   $row = $db->fetch_row($view);



$u = "
<tr>
<td>". date("F j,<br /> Y, g:i:s a",$row['rDATE']) ."</td>
<td>%s [%d]</td>
<td>\$".number_format($row['rAMNT'])."</td>
</tr>
";
printf($u,$row['rUSER'],($r['userid']);



echo ("</table>");

}

?>

 

And that is the function..

 

Whats happening is its inserting into the DB the date (Unix timestamp which works fine), the amount won in the raffle(works fine and yes this is a raffle script) and then for the user it inserts a random number when im trying to get it to insert there username. If i can insert there ID then make the ID show there username that would be even better.

 

Then the second problem is when displaying the logs on the table, its only showing the most recent LOG when i want it to show all logs.

 

Any help would be appreciated  :)

 

 

Edit: Put the whole cron in.

<?php
function view()
{
global $ir,$h,$userid,$db;



$table = "
<div align='center'>
      <h1>Previous Raffle Winners</h1></center>
<table width='70%' class='table' align='center' border='1' cellpadding='2' cellspacing='1'>
   <tr>
      <th width='15%'>Date</th>
      <th width='8%'>Winner</th>
      <th width='15%'>Amount</th>
   </tr>
";
echo($table);
   $view = $db->query("SELECT * FROM `rafflelog`  ORDER BY rDATE DESC");
      $row = $db->fetch_row($view);



$u = "
<tr>
   <td>". date("F j,<br /> Y, g:i:s a",$row['rDATE']) ."</td>
   <td>%s [%d]</td>
   <td>\$".number_format($row['rAMNT'])."</td>
</tr>
";
printf($u,$row['rUSER'],($r['userid']));



echo ("</table>");

}

?>

 

I edited it in the little box and forgot to add a $ in front of the db query!

In this section $result contains a resource link, no "username" like you want it to.

$query = "select `username` from `users` where `userid`='{$user['userid']}'";
$result = $db->query($query); 

$earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",
$that['pot'],
$user['userid']);
$db->query($earnings);

event_add($user['userid'],"You won the weekly lottery and were credited \${$that['pot']}",$c);  

$insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES ('', unix_timestamp(),%u,%d)",
$result,
$that['pot']);
$db->query($insert);

 

Try this:

 

$query = "select `username` from `users` where `userid`='{$user['userid']}'";
$result = $db->query($query); 
$username = $db->fetch_row($result);
$username = $username['username'];

$earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",
$that['pot'],
$user['userid']);
$db->query($earnings);

event_add($user['userid'],"You won the weekly lottery and were credited \${$that['pot']}",$c);  

$insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES ('', unix_timestamp(),%u,%d)",
$username,
$that['pot']);
$db->query($insert);

 

 

That inserts the value of 0 into the DB field rUSER. hmmm

 

Maybe it could be come across via a different way.

 

Right now a random ID is selected from the raffle.

The following will insert the users ID into the DB field when they win.

 

<?php
$select = $db->query('SELECT * FROM `raffle`');

$that = $db->fetch_row($select);
$win = rand(1,$this);
$win1 = sprintf("SELECT `userid` FROM `raffle` WHERE `id` = '%d'",$win);
$win2 = $db->query($win1);
$user = $db->fetch_row($win2);

$earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",
$that['pot'],
$user['userid']);
$db->query($earnings);

event_add($user['userid'],"You won the weekly lottery and were credited \${$that['pot']}",$c);  

$insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES ('', unix_timestamp(),%u,%d)",
$user,
$that['pot']);
$db->query($insert);


$db->query("TRUNCATE TABLE `raffle`");
$db->query("UPDATE `users` SET `lottery` = 0");

?>

 

Meaning if i win, id 1 will be inserted into the DB. Im really confused, lack of sleep!

<?php
$select = $db->query('SELECT * FROM `raffle`');
$this = $db->num_rows($select);
$that = $db->fetch_row($this);
$win = rand(1,$this);
$win1 = sprintf("SELECT `userid` FROM `raffle` WHERE `id` = '%d'",$win);
$win2 = $db->query($win1);
$user = $db->fetch_row($win2);

$earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",
$that['pot'],
$user['userid']);
$db->query($earnings);

event_add($user['userid'],"You won the weekly lottery and were credited \${$that['pot']}",$c);  

$insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES ('', unix_timestamp(),%u,%d)",
$user,
$that['pot']);
$db->query($insert);


$db->query("TRUNCATE TABLE `raffle`");
$db->query("UPDATE `users` SET `lottery` = 0");

?>

 

Woops, i removed that today when i was messing around with it.

 

Thats spitting out an error that it ($this) cannot be re-assigned.

$select = $db->query('SELECT * FROM `raffle`');
$nh= $db->num_rows($select);
$nj= $db->fetch_row($select);
$win = rand(1,$nh);
$win1 = sprintf("SELECT `userid` FROM `raffle` WHERE `id` = '%d'",$win);
$win2 = $db->query($win1);
$user = $db->fetch_row($win2);

$query = "select `username` from `users` where `userid`='{$user['userid']}'";
$q2 = $db->query($query);
$username = $db->fetch_row($q2);
$userna = $username['username'];

$earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",
$nj['pot'],
$user['userid']);
$db->query($earnings);

event_add($user['userid'],"You won the weekly lottery and were credited \${$nj['pot']}"); 

$insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES ('', unix_timestamp(),%u,%d)",
$username,
$nj['pot']);
$db->query($insert);


$db->query("TRUNCATE TABLE `raffle`");
$db->query("UPDATE `users` SET `lottery` = 0");

?>

 

Update on the cron, this inserts the winners userid into the rUSER row, credits correctly etc.

 

However i need it to show the username of the winner in the function provided in the first post.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.