raDiO_acTivE Posted December 6, 2008 Share Posted December 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/ Share on other sites More sharing options...
raDiO_acTivE Posted December 6, 2008 Author Share Posted December 6, 2008 <?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! Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707715 Share on other sites More sharing options...
raDiO_acTivE Posted December 6, 2008 Author Share Posted December 6, 2008 Its been bugging me all day, anyone please care to relieve me! Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707747 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707755 Share on other sites More sharing options...
raDiO_acTivE Posted December 6, 2008 Author Share Posted December 6, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707779 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 Where is $this coming from on line 4? should it be $that? Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707789 Share on other sites More sharing options...
raDiO_acTivE Posted December 6, 2008 Author Share Posted December 6, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707796 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 I do not think $this is a goo variable name to use, due to OOP Try something like $this_2 and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707805 Share on other sites More sharing options...
raDiO_acTivE Posted December 6, 2008 Author Share Posted December 6, 2008 Yeah i just read up on it, server was just upgraded and in PHP5 $this is a self destruct or something so i renamed and that worked. Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707809 Share on other sites More sharing options...
raDiO_acTivE Posted December 6, 2008 Author Share Posted December 6, 2008 $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. Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707821 Share on other sites More sharing options...
raDiO_acTivE Posted December 6, 2008 Author Share Posted December 6, 2008 Ive attempted many things, im going to do it a long way to see if it works, being selecting the random ID, then a query to put that ID to users table, then a query to select username from that ID Quote Link to comment https://forums.phpfreaks.com/topic/135802-must-be-something-really-small-that-i-can-see-missing/#findComment-707915 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.