dtjwov4984 2013-05-29 18:33
浏览 43

PHP和MySQL投票系统OOP [关闭]

To begin with we're beginners to PHP, we're studying Multimedia Design and we have been assigned to make a website in plain HTML. Furthermore we also have to include some PHP (which must be object-oriented). Our idea is to call the URL from our Youtube videos in our database and each video should have a vote button attached.

We can easily call our videos to a specific page in a div box on our website. This is our video_class.php:

<?php class Video {
private $db;    
public function insertVideo($videoId) {
    $row = $this->db->query("SELECT url FROM video WHERE id = ".$videoId);
    $ost = $this->db->loadRows($row);

    echo '<iframe width="200" height="200" src="https://www.youtube.com/embed/' . $ost[0]['url'] . '" frameborder="0" allowfullscreen></iframe>';
}

public function setDatabaseConnection($db) {
    $this->db = $db;
} } ?>

And the page we're loading it to:

<?php // Create database connection

// Load Database class file
require_once 'db_class.php'; 

//Creating new object instance from Database class
$db = new event(); 

// Run initiate function and provide credentials.
$db->initiate("localhost","root","","event"); 


$db->connect();                 // Connect to MySQL database

// Load Video class file
require_once 'video_class.php'; 


$video  = new Video;
$video->setDatabaseConnection($db);

$row=$db->query("SELECT url FROM video WHERE id = 1");
$ost=$db->loadRows($row);
//var_dump($ost);
$row1=$db->query("SELECT url FROM video WHERE id = 2");
$ost1=$db->loadRows($row1);
//var_dump($ost1);
$row2=$db->query("SELECT url FROM video WHERE id = 3");
$ost2=$db->loadRows($row2);
//var_dump($ost2); ?>

HTML:

    <center><div class="video_clip">

<?php echo '<iframe width="200" height="200" src="https://www.youtube.com/embed/' . $ost[0]['url'] . '" frameborder="0" allowfullscreen></iframe>'; ?>

        <a href="events_vote.php?userid=1&videoid=1"><img src="images/vote.png"></a>

        </div><!--video_clip end-->

But the real problem is next:

We have 3 videos you can vote on by clicking on the vote button, under each video. Each button must count the clicks and store it in our database. We have absolutely no clue how to make this possible. Our teacher told to link to a subpage (for example, "vote.php"). On that page we should use:

  • $_GET[id]
  • fetch id from $get
  • get current votes from video where id = 1/2/3
  • add+1
  • save votes in video where id=1
  • and finish with a redirect

Can someone help us? We have found a few possible solutions on the forums, but still no luck! Sorry for the long post and too much text :)

DATABASE STRUCTURE:

Table name:
users

Table comments: users

Column  Type    Null    Default Comments    MIME

id  int(11) No          
videoId int(11) No          

Table name:
video

Table comments: video

Column  Type    Null    Default Comments    MIME

id  int(11) No          
url varchar(50) No       
  • 写回答

2条回答 默认 最新

  • doubu7425 2013-05-29 18:56
    关注

    This is quite open-ended, so I'll add some clues to get you going.

    Firstly, since your votes will affect the database, you should use post and not get (see here for more details)1. Once you've dealt with the operation, you can then do your redirect.

    So, under your <iframe>, set up a <form> with a post method. In it, add three input tags each having a type of submit, and each having a different name attribute. For your action, you can aim it at a different page if you want to, but since it is simple I would point it at itself. Thus, use <?php echo $_SERVER[ 'PHP_SELF' ] ?> for the time being.

    OK, so this will send the post data to the same page. Thus, in your page PHP, just after your database initialisation, catch the post op like so:

    // Your existing code
    $db->connect();                 // Connect to MySQL database
    
    // New code
    if ($_POST) {
        print_r($_POST);
        exit();
        // @todo Parse the result in your POST array
        // @todo Save the result in the database
        // @todo Redirect to self
    }
    
    // Load Video class file
    require_once 'video_class.php'; 
    

    What that will do is dump the post data on the screen, and then exit immediately. This is a good prototyping approach to see that you're on the right track.

    Adding @todo notes is quite a good approach too - do these in order, and delete the comment when that piece is written and tested. Don't forget to add new comments explaining code, if appropriate.

    1 If using the $_GET array is an essential component of the exercise, then you could use three post forms, each with their own button, and with the action containing a separate query string that will appear in the $_GET array. However I'd argue that's a bit convoluted, and probably not the best way to achieve this in practice.

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码