Die zocht ik! die mysql manual is zo onoverzichtelijk als de netequote:Op woensdag 26 augustus 2009 22:28 schreef GlowMouse het volgende:
SELECT MAX(id) FROM table
Wel zorgen dat id een PK is of een index heeft.
Je moet gewoon weten wat je zoektquote:Op woensdag 26 augustus 2009 22:32 schreef Swetsenegger het volgende:
[..]
Die zocht ik! die mysql manual is zo onoverzichtelijk als de nete
een pk?quote:Op woensdag 26 augustus 2009 22:28 schreef GlowMouse het volgende:
SELECT MAX(id) FROM table
Wel zorgen dat id een PK is of een index heeft.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | /* tabellen: tinyPoll id (int) title (varchar * 64) comment (text) date_started (date) date_ended (date) answers (tinyint) votes (int) views (int) tinyPollAnswers id (int) pollID (int) answer (varchar * 64) votes (int) tinyPollVotesIp id (int) ip (varchar * 64) hostname (varchar * 128) unique ON ip & hostname tinyPollVotes ipID (int) pollID (int) answerID (int) votedate (date) unique on pollID & ipID */ class tinyPoll { private $db; var $dateFormat = '%y %M %d'; function __construct($db) { $this->db = $db; } public function loadPoll($id = 0, $ip = '', $hostname = '') { $poll = false; $sql = "SELECT tinyPoll.* FROM tinyPoll WHERE UNIX_TIMESTAMP(date_started) < NOW() "; if ($id != 0) { $sql .= " AND `id` = '" . $this->db->escape($id) . "'"; } else { $sql .= " ORDER BY `id` DESC"; } $sql .= " LIMIT 1"; $query = $this->db->query($sql); $items = $this->db->num_rows($query); if ($items == 1) { // load poll $poll = $this->db->fetch_assoc($query); $poll['canvote'] = 1; // load answers $query = $this->db->query("SELECT * FROM tinyPollAnswers WHERE pollID = '" . $this->db->escape($id) . "' LIMIT " . $poll['answers']); $items = $this->db->num_rows($query); if ($items == $poll['answers']) { for ($x = 0; $x < $items; $x++) { // save answers to array! $tmp = $this->db->fetch_assoc($query); $poll['ansers'][$tmp['id']] = array("answer" => $tmp['answer'], "votes" => $tmp['votes']); } } // check for current user if ($ip != '' && $hostname != '') { $query = $this->db->query("SELECT tinyPollVotes.answerID FROM tinyPollVotes LEFT JOIN tinyPollVotesIp ON tinyPollVotesIp.id = tinyPollVotes.ipID WHERE tinyPollVotesIp.ip = '" . $this->db->escape($ip) . "' AND tinyPollVotesIp.hostname = '" . $this->db->escape($hostname) . "' AND tinyPollVotes.pollID = '" . $this->db->escape($id) . "' LIMIT 1"); $items = $this->db->num_rows($query); if ($items != 0) { // vote found, user can't vote! $poll['canvote'] = 0; } } } return $poll; } public function listPolls($start = 0, $num = 10) { // list polls $start = !is_numeric($start) ? 0 : $start; $num = !is_numeric($num) ? 10 : $num; $arr = array(); // make query for loading polls $query = $this->db->query("SELECT tinyPoll.id, tinyPoll.title, tinyPoll.comment, tinyPoll.votes, UNIX_TIMESTAMP(tinyPoll.date_ended) AS closedDate FROM tinyPoll WHERE UNIX_TIMESTAMP(date_started) < NOW() LIMIT " . $start . ", " . $num); $items = $this->db->num_rows($query); if ($items > 0) { // loop number of found polls for ($x = 0; $x < $items; $x++) { $tmp = $this->db->fetch_assoc($query); // save poll settings to array $arr[$tmp['id']] = array("title" => $tmp['title'], "comment" => $tmp['comment'], "votes" => $tmp['votes'], "closed" => ($tmp['closedDate']>time()) ? 0 : 1); } } // return polls array return $arr; } public function votePoll($pollID, $answerID, $ip, $hostname) { // check if poll exists and is open $query = $this->db->query("SELECT tinyPoll.id FROM tinyPoll LEFT JOIN tinyPollAnswers ON tinyPollAnswers.pollID = tinyPoll.id WHERE tinyPoll.id = '" . $this->db->escape($pollID) . "' AND tinyPollAnswers.id = '" . $this->db->escape($answerID) . "' AND UNIX_TIMESTAMP(date_ended) > NOW() LIMIT 1"); $items = $this->db->num_rows($query); // vote not found? if ($items == 0) { // insert vote IP into database $query = $this->db->query("INSERT INTO `tinyPollVotesIp` (`id`, `ip`, `hostname`) VALUES ('', '" . $this->db->escape($ip) . "', '" . $this->db->escape($hostname) . "') ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id);"); $ipID = $this->db->insert_id(); // try to insert the vote in the poll table, if exists, return same ID $query = $this->db->query("INSERT INTO `tinyPollVotes` (`ipID`, `pollID`, `answerID`, `votedate`) VALUES ('" . $this->db->escape($ipID) . "', '" . $this->db->escape($pollID) . "', '" . $this->db->escape($answerID) . "', NOW()) ON DUPLICATE KEY UPDATE ipID = LAST_INSERT_ID(ipID);"); if ($this->db->affected_rows()) { // update poll $this->db->query("UPDATE tinyPoll SET votes = votes +1 WHERE id = '" . $this->db->escape($pollID) . "' LIMIT 1"); // update poll answers $this->db->query("UPDATE tinyPollAnswers SET votes = votes +1 WHERE id = '" . $this->db->escape($answerID) . "' AND pollID = '" . $this->db->escape($pollID) . "' LIMIT 1"); return true; } } return false; } } ?> |
Nou mijn poll script is 70 regels lang. Dit is bijna 2 keer zo lang Maar ik heb niet inhoudelijk naar verschillen gekeken.quote:Op donderdag 27 augustus 2009 11:55 schreef Chandler het volgende:
@Swetsenegger: Lang? eerder heel kort zul je bedoelen?
Precies mijn gedachte, alsof je je eigen website steeds gaat verhuizen qua database types al is het wel handig als je een script schrijft voor 100.000.000 mensen zoals wordpress oid...quote:
Ookal verhuis je het wel ben je alsnog screwed want je queries zullen toch niet meer werkenquote:Op donderdag 27 augustus 2009 12:21 schreef Chandler het volgende:
[..]
Precies mijn gedachte, alsof je je eigen website steeds gaat verhuizen qua database types al is het wel handig als je een script schrijft voor 100.000.000 mensen zoals wordpress oid...
@Swets; 70 regels? mag ik die eens zien?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | function show_poll($value){ if ($value==''){ $query="SELECT Poll_Id FROM Polls WHERE Archive=0 ORDER BY Poll_id DESC LIMIT 1"; $result=mysql_query($query); $row=mysql_fetch_assoc($result); $value=$row['Poll_Id']; } $query="SELECT COUNT(*)as aantal FROM Poll_Answers a JOIN Polls as p ON(a.Poll=p.Poll_id) WHERE Poll=".$value." AND (ip='".$_SERVER['REMOTE_ADDR']."' OR archive=1)"; $result=mysql_query($query); $row=mysql_fetch_assoc($result); if($row['aantal']!=0){ return display_poll_results($value); }else{ return display_poll_questions($value); } } function display_poll_results($poll){ $query="SELECT Poll_Name FROM Polls WHERE Poll_Id=".$poll; $result=mysql_query($query); $row_name = mysql_fetch_assoc($result); $query="SELECT COUNT(*) as votes FROM Poll_Answers WHERE Poll=".$poll; $result=mysql_query($query); $row_votes = mysql_fetch_assoc($result); $query="SELECT q.Question as name, q.Poll_Question_Id as question, COUNT(a.Question) as vote FROM Poll_Questions q LEFT JOIN Poll_Answers a ON(q.Poll_Question_Id=a.Question) WHERE q.Poll=".$poll." GROUP BY q.Question ORDER BY q.Poll_Question_Id ASC"; $result=mysql_query($query); $display = "<span class=\"highlight\">Poll - ".$row_name['Poll_Name']." - ". $row_votes['votes']." stemmen - Dank je wel voor je stem!</span>"; if (!empty($error)){ $display.=" ".$error."\r\n<hr />\r\n"; }else{ $display.="\r\n<hr />\r\n"; } $display.= "<table style=\"width:95%;\">\r\n"; while ($row = mysql_fetch_assoc($result)){ $background[$row['question']]=round(200-($row['vote']/($row_votes['votes']/100)*2)); //200 en *2 ivm 200px brede bar ipv 100 $percentage[$row['question']]=round($row['vote']/($row_votes['votes']/100)); $votes[$row['question']]=$row['vote']; $display.= "<tr><td class=\"highlight2\">- ".$row['name']."</td>\r\n"; $display.= "<td><div style=\"width:200px;height:20px;border:solid 1px rgb(29,40,99);background-image:url(image/progress_front.jpg);background-repeat:no-repeat;background-position:-"; if($background[$row['question']]===NULL){ $display .="200"; }else{ $display .= $background[$row['question']]; } $display .="px 0;text-align:center;line-height:20px;color:white;\">"; if($percentage[$row['question']]!=''){ $display .= $percentage[$row['question']]; }else{ $display .= "0"; } $display .="%</div></td>\r\n<td>"; if($votes[$row['question']]===NULL){ $display .="0"; }else{ $display .= $votes[$row['question']]; } $display .=" stemmen</td></tr>\r\n"; } $display.= "</table>\r\n"; return $display; } function display_poll_questions($poll){ $query="SELECT Poll_Name FROM Polls WHERE Poll_Id=".$poll; $result=mysql_query($query); $row_name = mysql_fetch_assoc($result); $query="SELECT * FROM Poll_Questions WHERE Poll=".$poll." ORDER BY Poll_Question_Id ASC"; $result=mysql_query($query); $display = "<span class=\"highlight\">Poll - ".$row_name['Poll_Name']."</span>\r\n<hr />\r\n"; $display.= "<form style=\"margin:0\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\r\n"; while ($row = mysql_fetch_assoc($result)){ $display.= "<input type=\"radio\" name=\"poll_question\" value=\"".$row['Poll_Question_Id']."\" id=\"label".$row['Poll_Question_Id']."\" /> <label class=\"highlight2\" for=\"label".$row['Poll_Question_Id']."\">".$row['Question']."</label><br />\r\n"; } $display.= "<input type=\"hidden\" name=\"poll\" value=\"".$poll."\" />\r\n"; $display.= "<p class=\"highlight2\" style=\"line-height:22px;height:22px;\">Type het getal over : <img style=\"border:solid 1px rgb(09,20,79);vertical-align:middle;\" src=\"/system/php_captcha.php\" alt=\"captcha\" /> <input class=\"input\" style=\"height:20px;vertical-align:middle;font-size:18px;\" type=\"text\" name=\"captcha\" id=\"captcha\" size=\"5\" maxlength=\"5\" /></p>\r\n"; $display.= "<p><input type=\"submit\" name=\"submit\" value=\"Stem!\" /></p></form>"; return $display; } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | if($_SERVER['REQUEST_METHOD']=='POST'){ //Captcha $key=substr($_SESSION['key'],0,5); if($_POST['captcha']!=$key){ die('vergeet het getal niet over te tikken!'); } If(is_numeric($_POST['poll']) && is_numeric($_POST['poll_question'])){ $query="INSERT INTO Poll_Answers (Question,Poll,ip) VALUES(".$_POST['poll_question'].",".$_POST['poll'].",'".$_SERVER['REMOTE_ADDR']."')"; if($result=mysql_query($query)){ header('location:'.$_SERVER['PHP_SELF']); }else{ $error=' - U heeft al gestemd'; } }else{ die('Zonder keuze kan je niet stemmen'); } } ?> |
Mjah ik ben niet van de template engines.quote:Op donderdag 27 augustus 2009 12:44 schreef Xcalibur het volgende:
Oh, wat ranzig al die html in je script
iemand?quote:Op donderdag 27 augustus 2009 11:38 schreef Swetsenegger het volgende:
Ik vind het wat lang voor een simpel polletje maar ik heb er nog niet echt inhoudelijk naar gekeken
Ik heb een vraagje. Ik heb een site die wat tweets naar twitter verstuurt. En i9n die tweets staat een euro teken. Nu doe ik dat met & euro; zodat ik een crossplatform euroteken krijg. Maar.... voor twitter zijn ook allerlei desktop applicaties en die zijn in de regel NIET webbased. En in plaats van een mooi teken zie die nu dus de html entity.
Zend dan gewoon het euroteken daadwerkelijk mee, maar dan heb ik het probleem dat een ¤ er op bv OSX of Linux er als ć ofzo uitziet.
Hoe krijg ik een cross platform, cross applicatie euroteken?
Ik ook niet, maar het minste wat je kunt doen is je echo's helemaal na je php-code zetten.quote:Op donderdag 27 augustus 2009 12:50 schreef Swetsenegger het volgende:
[..]
Mjah ik ben niet van de template engines.
Ik ben gek op Smartyquote:Op donderdag 27 augustus 2009 12:50 schreef Swetsenegger het volgende:
[..]
Mjah ik ben niet van de template engines.
eh, ik zet alle output in een var en return die.quote:Op donderdag 27 augustus 2009 12:52 schreef GlowMouse het volgende:
[..]
Ik ook niet, maar het minste wat je kunt doen is je echo's helemaal na je php-code zetten.
Mjah, in zend staat het bij mij keurig met wit regels en idented. Blijkbaar staan de tabs hier anders ingesteld En dat scheelt zeker geen 60 regels.quote:Op donderdag 27 augustus 2009 12:53 schreef HuHu het volgende:
Ik denk toch dat ik moet zeggen dat de extra regels die Chandler gebruikt vooral bestaan uit dingen die de leesbaarheid ten goede komen: witregels.
Verder plaatst Chandler ook de { op een eigen regel, terwijl Swetsenegger die op de vorige regel aan het einde laat staan. Al met al dus weinig verschil.
Je vergeet dat hij 40 regels database comment heeft bovenaan zijn script.quote:Op donderdag 27 augustus 2009 12:56 schreef Swetsenegger het volgende:
[..]
eh, ik zet alle output in een var en return die.
[..]
Mjah, in zend staat het bij mij keurig met wit regels en idented. Blijkbaar staan de tabs hier anders ingesteld En dat scheelt zeker geen 60 regels.
Beter: je returnt de data die je nodig hebt voor de output. Serieus, als ik zo'n script tegenkom als het jouwe dan begin ik liever opnieuw.quote:Op donderdag 27 augustus 2009 12:56 schreef Swetsenegger het volgende:
[..]
eh, ik zet alle output in een var en return die.
35quote:Op donderdag 27 augustus 2009 12:57 schreef Moozzie het volgende:
[..]
Je vergeet dat hij 40 regels database comment heeft bovenaan zijn script.
eh sorryquote:Anyways shut it
Forum Opties | |
---|---|
Forumhop: | |
Hop naar: |